lightflow-engine 0.2.7 → 0.2.9

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.
@@ -12,18 +12,19 @@
12
12
  */
13
13
  import { Engine, registerWorkflow } from "../index.js";
14
14
  import { workflowIdFor } from "./workflow.js";
15
- let sharedEngine = null;
16
- let sharedStore = null;
15
+ const g = globalThis;
16
+ if (!g.__lightflowCompat)
17
+ g.__lightflowCompat = { engine: null, store: null };
17
18
  /** Configure the compat layer with a store/engine (call once at boot). */
18
19
  export function initWorkflowApi(store, engine) {
19
- sharedStore = store;
20
- sharedEngine = engine ?? new Engine(store);
20
+ g.__lightflowCompat.store = store;
21
+ g.__lightflowCompat.engine = engine ?? new Engine(store);
21
22
  }
22
23
  export function getEngine() {
23
- if (!sharedEngine) {
24
+ if (!g.__lightflowCompat.engine) {
24
25
  throw new Error("workflow/api not initialized — call initWorkflowApi(store) at startup");
25
26
  }
26
- return sharedEngine;
27
+ return g.__lightflowCompat.engine;
27
28
  }
28
29
  /**
29
30
  * Register a workflow function under a stable id and start it. Mirrors
@@ -31,7 +32,7 @@ export function getEngine() {
31
32
  */
32
33
  function makeRunHandle(runId) {
33
34
  const engine = getEngine();
34
- const store = sharedStore;
35
+ const store = g.__lightflowCompat.store;
35
36
  /** One DB read -> Vercel status vocabulary. */
36
37
  const readStatus = async () => {
37
38
  const row = await store.getRun(runId);
package/dist/index.js CHANGED
@@ -26,15 +26,29 @@ export class FatalError extends Error {
26
26
  this.name = "FatalError";
27
27
  }
28
28
  }
29
- const workflows = new Map();
30
- const steps = new Map();
29
+ const reg = globalThis;
30
+ if (!reg.__lightflowWorkflows)
31
+ reg.__lightflowWorkflows = new Map();
32
+ if (!reg.__lightflowSteps)
33
+ reg.__lightflowSteps = new Map();
34
+ const workflows = reg.__lightflowWorkflows;
35
+ const steps = reg.__lightflowSteps;
31
36
  export function registerWorkflow(id, fn) {
32
37
  workflows.set(id, fn);
33
38
  }
34
39
  export function registerStep(id, fn) {
35
40
  steps.set(id, fn);
36
41
  }
37
- export let current = null;
42
+ export let current = globalThis.__lightflowCurrent ?? null;
43
+ if (!globalThis.__lightflowCurrent) {
44
+ globalThis.__lightflowCurrent = null;
45
+ }
46
+ // Keep the live binding in sync: `current` assignments below must write both.
47
+ function setCurrent(v) {
48
+ current = v;
49
+ globalThis.__lightflowCurrent = v;
50
+ }
51
+ // Rewrite plain assignments on `current` below to setCurrent(...)
38
52
  export function getWorkflowMetadata() {
39
53
  if (!current)
40
54
  throw new Error("getWorkflowMetadata() outside a workflow");
@@ -364,7 +378,7 @@ export class Engine {
364
378
  },
365
379
  };
366
380
  const prev = current;
367
- current = ctx;
381
+ setCurrent(ctx);
368
382
  try {
369
383
  const output = await fn(...args);
370
384
  await Promise.all(ctx.inflight);
@@ -392,7 +406,7 @@ export class Engine {
392
406
  return fail(err instanceof Error ? err.message : String(err));
393
407
  }
394
408
  finally {
395
- current = prev;
409
+ setCurrent(prev);
396
410
  await claimer.releaseRun?.(runId);
397
411
  }
398
412
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lightflow-engine",
3
- "version": "0.2.7",
3
+ "version": "0.2.9",
4
4
  "description": "A tiny durable workflow engine for Node.js and Postgres — with a Vercel Workflow drop-in compat layer.",
5
5
  "license": "MIT",
6
6
  "type": "module",