dsh-live-compact 1.1.13 → 1.1.15

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/gate.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { createRequire } from "node:module";
2
- import { STOCK_ENGINE as STOCK } from "./adapter.js";
2
+ import { contextProperty, entryOf, rootContext, STOCK_ENGINE as STOCK } from "./adapter.js";
3
3
  import { configSnapshot, engineConfig } from "./settings.js";
4
4
 
5
5
  const OURS = "dsh-live-compact";
@@ -16,16 +16,16 @@ export const PRESET_ENGINE_CONFIG = Object.freeze(engineConfig());
16
16
  * exposes every live fiber through `ctx.registry.values()`; a package on disk
17
17
  * or a failed/unloading fiber is not an active compressor.
18
18
  */
19
- export function activeCompactionPeers(ctx) {
19
+ export function activeCompactionPeers(ctx, { includeManual = true } = {}) {
20
20
  const peers = [];
21
- if (process.env.SPINE_STAND_DOWN === "1") peers.push("SPINE_STAND_DOWN");
22
- const registry = ctx?.registry;
21
+ if (includeManual && process.env.SPINE_STAND_DOWN === "1") peers.push("SPINE_STAND_DOWN");
22
+ const registry = contextProperty(ctx, "registry");
23
23
  if (!registry || typeof registry.values !== "function") return peers;
24
24
  try {
25
25
  for (const runtime of registry.values()) {
26
26
  const name = runtime?.name;
27
27
  if (!COMPETING_COMPRESSORS.has(name)) continue;
28
- const active = [...(runtime?.fibers || [])].some((fiber) => COMPETING_FIBER_STATES.has(fiber?.state));
28
+ const active = [...(runtime?.fibers || [])].some((fiber) => COMPETING_FIBER_STATES.has(contextProperty(fiber, "state")));
29
29
  if (active && !peers.includes(name)) peers.push(name);
30
30
  }
31
31
  } catch {
@@ -40,10 +40,10 @@ export function compactionStandDown(ctx) {
40
40
  return activeCompactionPeers(ctx).length > 0;
41
41
  }
42
42
 
43
- export function warnCompactionStandDown(ctx, logger = ctx?.logger) {
43
+ export function warnCompactionStandDown(ctx, logger = contextProperty(ctx, "logger")) {
44
44
  const peers = activeCompactionPeers(ctx);
45
45
  if (!peers.length) return peers;
46
- const root = ctx?.root || ctx;
46
+ const root = rootContext(ctx);
47
47
  const key = peers.join(",");
48
48
  if (root && standDownNotices.get(root) === key) return peers;
49
49
  if (root) standDownNotices.set(root, key);
@@ -89,7 +89,7 @@ function takeoverSelected(config, entry, selector) {
89
89
  entry?.id,
90
90
  entry?.presetId,
91
91
  ...(Array.isArray(entry?.presetIds) ? entry.presetIds : []),
92
- entry?.options?.id,
92
+ contextProperty(entry, "options")?.id,
93
93
  ].filter((id) => typeof id === "string");
94
94
  return ids.some((id) => selector.includes(id));
95
95
  }
@@ -101,27 +101,26 @@ function outerPresetIds(fiber) {
101
101
  const push = (value) => {
102
102
  if (value && (typeof value === "object" || typeof value === "function") && !seen.has(value)) queue.push(value);
103
103
  };
104
- try { push(fiber?.parent); } catch {}
105
- try { push(fiber?.fiber?.parent); } catch {}
104
+ push(contextProperty(fiber, "parent"));
105
+ push(contextProperty(contextProperty(fiber, "fiber"), "parent"));
106
106
  while (queue.length) {
107
107
  const current = queue.shift();
108
108
  if (seen.has(current)) continue;
109
109
  seen.add(current);
110
- let ids = [];
111
- try {
112
- ids = [
113
- current?.entry?.options?.id,
114
- current?.fiber?.entry?.options?.id,
115
- current?.config?.id,
116
- current?.id,
117
- ];
118
- } catch {}
110
+ const inner = contextProperty(current, "fiber");
111
+ const parent = contextProperty(current, "parent");
112
+ const ids = [
113
+ contextProperty(entryOf(current), "options")?.id,
114
+ contextProperty(entryOf(inner), "options")?.id,
115
+ contextProperty(current, "config")?.id,
116
+ contextProperty(current, "id"),
117
+ ];
119
118
  const candidate = ids.find((id) => typeof id === "string" && id.length > 0);
120
119
  if (candidate && !found.includes(candidate)) found.push(candidate);
121
- try { push(current.parent); } catch {}
122
- try { push(current.fiber); } catch {}
123
- try { push(current.parent?.fiber); } catch {}
124
- try { push(current.fiber?.parent); } catch {}
120
+ push(parent);
121
+ push(inner);
122
+ push(contextProperty(parent, "fiber"));
123
+ push(contextProperty(inner, "parent"));
125
124
  }
126
125
  return found;
127
126
  }
@@ -188,21 +187,21 @@ const installedGates = new WeakMap();
188
187
 
189
188
  function entryDepth(ctx) {
190
189
  let depth = 0;
191
- for (let fiber = ctx.fiber; fiber?.runtime; fiber = fiber.parent.fiber) depth++;
190
+ for (let fiber = contextProperty(ctx, "fiber"); contextProperty(fiber, "runtime");
191
+ fiber = contextProperty(contextProperty(fiber, "parent"), "fiber")) depth++;
192
192
  return depth;
193
193
  }
194
194
 
195
- export function installPresetGate(ctx, config = {}, { legacy = false } = {}) {
195
+ export function installPresetGate(ctx, config = {}, { legacy = false, deferCommit = false } = {}) {
196
196
  const depth = entryDepth(ctx);
197
- const previous = installedGates.get(ctx.root);
197
+ const root = rootContext(ctx);
198
+ if (!root) throw new Error("spine: Cordis root context unavailable");
199
+ const previous = installedGates.get(root);
198
200
  if (previous) {
199
201
  if (legacy && !previous.legacy) return;
200
202
  if (legacy === previous.legacy && (depth > previous.depth || (depth === previous.depth && previous.active))) return;
201
- previous.active = false;
202
- previous.disposeListener?.();
203
203
  }
204
- const gate = { depth, legacy, active: true };
205
- installedGates.set(ctx.root, gate);
204
+ const gate = { depth, legacy, active: false };
206
205
  ctx.effect(() => () => { gate.active = false; });
207
206
  const enabled = () => {
208
207
  const value = config.takeoverPresets ?? true;
@@ -211,15 +210,15 @@ export function installPresetGate(ctx, config = {}, { legacy = false } = {}) {
211
210
  const seen = new Set();
212
211
  gate.disposeListener = ctx.on("internal/config", function (fiber, next) {
213
212
  const resolved = next();
213
+ if (!gate.active) return resolved;
214
214
  if (compactionStandDown(ctx)) {
215
215
  warnCompactionStandDown(ctx);
216
216
  return resolved;
217
217
  }
218
- if (!gate.active) return resolved;
219
218
  const selector = enabled();
220
219
  if (!selector || (Array.isArray(selector) && selector.length === 0)) return resolved;
221
220
  const loadable = packageLoadable();
222
- const entry = fiber?.entry?.options;
221
+ const entry = contextProperty(entryOf(fiber), "options");
223
222
  const presetIds = outerPresetIds(fiber);
224
223
  const selectedEntry = presetIds.length ? { ...entry, presetId: presetIds[0], presetIds } : entry;
225
224
  const rewritten = rewritePresetConfig(resolved, loadable, config, selectedEntry, selector);
@@ -236,6 +235,16 @@ export function installPresetGate(ctx, config = {}, { legacy = false } = {}) {
236
235
  }
237
236
  return rewritten;
238
237
  }, { global: true });
238
+ const commit = () => {
239
+ installedGates.set(root, gate);
240
+ gate.active = true;
241
+ if (previous) {
242
+ previous.active = false;
243
+ try { previous.disposeListener?.()?.catch?.(() => {}); } catch {}
244
+ }
245
+ };
246
+ if (deferCommit) return commit;
247
+ commit();
239
248
  }
240
249
 
241
250
  // Legacy entry for existing compositions; new installs only register compaction-spine.