@goodandready/dsh-model-sync 0.2.4 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +23 -12
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -22,7 +22,8 @@ export const Config = Schema.object({
|
|
|
22
22
|
|
|
23
23
|
export function apply(ctx, config) {
|
|
24
24
|
const baseConfig = structuredClone(config || {})
|
|
25
|
-
|
|
25
|
+
const resolveConfig = (value) => Config(structuredClone(value || {}))
|
|
26
|
+
let liveConfig = resolveConfig(baseConfig)
|
|
26
27
|
let reconfigure = () => {}
|
|
27
28
|
let saveConfigImpl
|
|
28
29
|
// ctx.inject starts a child fiber; keep a stable function reference for the
|
|
@@ -34,27 +35,37 @@ export function apply(ctx, config) {
|
|
|
34
35
|
return saveConfigImpl(patch)
|
|
35
36
|
}
|
|
36
37
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const scope =
|
|
41
|
-
|
|
38
|
+
let settingsScope
|
|
39
|
+
const bindSettings = (settings, ownerCtx) => {
|
|
40
|
+
if (settingsScope || !settings?.register) return false
|
|
41
|
+
const scope = settings.register(NS, Config, { base: baseConfig })
|
|
42
|
+
settingsScope = scope
|
|
43
|
+
liveConfig = resolveConfig(scope.get() || baseConfig)
|
|
42
44
|
const stop = scope.watch((next) => {
|
|
43
|
-
liveConfig =
|
|
45
|
+
liveConfig = resolveConfig(next || baseConfig)
|
|
44
46
|
reconfigure()
|
|
45
47
|
})
|
|
46
48
|
saveConfigImpl = async (patch) => {
|
|
47
49
|
const result = await scope.update(patch)
|
|
48
|
-
|
|
49
|
-
// synchronously as well so a following request sees the new allowlist.
|
|
50
|
-
liveConfig = Config(scope.get() || baseConfig)
|
|
50
|
+
liveConfig = resolveConfig(scope.get() || baseConfig)
|
|
51
51
|
return result
|
|
52
52
|
}
|
|
53
|
-
|
|
53
|
+
ownerCtx.effect(() => () => {
|
|
54
|
+
settingsScope = undefined
|
|
54
55
|
saveConfigImpl = undefined
|
|
55
56
|
stop()
|
|
56
57
|
}, 'dsh-model-sync: settings')
|
|
57
|
-
|
|
58
|
+
return true
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let settings
|
|
62
|
+
try { settings = ctx.get('settings') } catch {}
|
|
63
|
+
if (!bindSettings(settings, ctx)) {
|
|
64
|
+
ctx.inject(['settings'], (sctx) => {
|
|
65
|
+
bindSettings(sctx.settings, sctx)
|
|
66
|
+
})
|
|
67
|
+
}
|
|
68
|
+
|
|
58
69
|
|
|
59
70
|
const service = createProviderService(ctx)
|
|
60
71
|
const synchronizer = createModelSynchronizer(ctx, { getConfig: () => liveConfig, saveConfig })
|