@@ -6,14 +6,14 @@ import {
66} from "@t3tools/contracts" ;
77import { fromLenientJson } from "@t3tools/shared/schemaJson" ;
88import * as Context from "effect/Context" ;
9+ import * as Crypto from "effect/Crypto" ;
910import * as Data from "effect/Data" ;
1011import * as Effect from "effect/Effect" ;
1112import * as FileSystem from "effect/FileSystem" ;
1213import * as Layer from "effect/Layer" ;
1314import * as Option from "effect/Option" ;
1415import * as Path from "effect/Path" ;
1516import * as PlatformError from "effect/PlatformError" ;
16- import * as Random from "effect/Random" ;
1717import * as Schema from "effect/Schema" ;
1818import * as SynchronizedRef from "effect/SynchronizedRef" ;
1919
@@ -222,10 +222,10 @@ const writeSettings = Effect.fn("desktop.settings.writeSettings")(function* (inp
222222 readonly settingsPath : string ;
223223 readonly settings : DesktopSettings ;
224224 readonly defaultSettings : DesktopSettings ;
225+ readonly suffix : string ;
225226} ) : Effect . fn . Return < void , PlatformError . PlatformError | Schema . SchemaError > {
226227 const directory = input . path . dirname ( input . settingsPath ) ;
227- const suffix = ( yield * Random . nextUUIDv4 ) . replace ( / - / g, "" ) ;
228- const tempPath = `${ input . settingsPath } .${ process . pid } .${ suffix } .tmp` ;
228+ const tempPath = `${ input . settingsPath } .${ process . pid } .${ input . suffix } .tmp` ;
229229 const encoded = yield * encodeDesktopSettingsJson (
230230 toDesktopSettingsDocument ( input . settings , input . defaultSettings ) ,
231231 ) ;
@@ -240,6 +240,7 @@ export const layer = Layer.effect(
240240 const environment = yield * DesktopEnvironment . DesktopEnvironment ;
241241 const fileSystem = yield * FileSystem . FileSystem ;
242242 const path = yield * Path . Path ;
243+ const crypto = yield * Crypto . Crypto ;
243244 const settingsRef = yield * SynchronizedRef . make ( environment . defaultDesktopSettings ) ;
244245
245246 const persist = (
@@ -251,13 +252,18 @@ export const layer = Layer.effect(
251252 return Effect . succeed ( [ settingsChange ( settings , false ) , settings ] as const ) ;
252253 }
253254
254- return writeSettings ( {
255- fileSystem,
256- path,
257- settingsPath : environment . desktopSettingsPath ,
258- settings : nextSettings ,
259- defaultSettings : environment . defaultDesktopSettings ,
260- } ) . pipe (
255+ return crypto . randomUUIDv4 . pipe (
256+ Effect . map ( ( uuid ) => uuid . replace ( / - / g, "" ) ) ,
257+ Effect . flatMap ( ( suffix ) =>
258+ writeSettings ( {
259+ fileSystem,
260+ path,
261+ settingsPath : environment . desktopSettingsPath ,
262+ settings : nextSettings ,
263+ defaultSettings : environment . defaultDesktopSettings ,
264+ suffix,
265+ } ) ,
266+ ) ,
261267 Effect . mapError ( ( cause ) => new DesktopSettingsWriteError ( { cause } ) ) ,
262268 Effect . as ( [ settingsChange ( nextSettings , true ) , nextSettings ] as const ) ,
263269 ) ;
0 commit comments