@warp-drive/react 5.9.0-alpha.9 → 5.9.0-beta.1
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/declarations/index.d.ts +179 -6
- package/declarations/index.d.ts.map +1 -0
- package/declarations/install.d.ts +21 -3
- package/declarations/install.d.ts.map +1 -0
- package/dist/index.js +216 -251
- package/dist/index.js.map +1 -0
- package/dist/install.js +90 -94
- package/dist/install.js.map +1 -0
- package/dist/reactive-context-DNq02nr1.js +122 -0
- package/dist/reactive-context-DNq02nr1.js.map +1 -0
- package/dist/unpkg/dev/index.js +260 -299
- package/dist/unpkg/dev/index.js.map +1 -0
- package/dist/unpkg/dev/install.js +82 -92
- package/dist/unpkg/dev/install.js.map +1 -0
- package/dist/unpkg/dev/reactive-context-C9-Fc8qx.js +218 -0
- package/dist/unpkg/dev/reactive-context-C9-Fc8qx.js.map +1 -0
- package/dist/unpkg/dev-deprecated/index.js +260 -299
- package/dist/unpkg/dev-deprecated/index.js.map +1 -0
- package/dist/unpkg/dev-deprecated/install.js +82 -92
- package/dist/unpkg/dev-deprecated/install.js.map +1 -0
- package/dist/unpkg/dev-deprecated/reactive-context-DhpCE2Tv.js +218 -0
- package/dist/unpkg/dev-deprecated/reactive-context-DhpCE2Tv.js.map +1 -0
- package/dist/unpkg/prod/index.js +199 -226
- package/dist/unpkg/prod/index.js.map +1 -0
- package/dist/unpkg/prod/install.js +56 -48
- package/dist/unpkg/prod/install.js.map +1 -0
- package/dist/unpkg/prod/reactive-context-CvfXAZqb.js +92 -0
- package/dist/unpkg/prod/reactive-context-CvfXAZqb.js.map +1 -0
- package/dist/unpkg/prod-deprecated/index.js +199 -226
- package/dist/unpkg/prod-deprecated/index.js.map +1 -0
- package/dist/unpkg/prod-deprecated/install.js +56 -48
- package/dist/unpkg/prod-deprecated/install.js.map +1 -0
- package/dist/unpkg/prod-deprecated/reactive-context-CvfXAZqb.js +92 -0
- package/dist/unpkg/prod-deprecated/reactive-context-CvfXAZqb.js.map +1 -0
- package/package.json +14 -15
- package/declarations/-private/reactive-context.d.ts +0 -18
- package/declarations/-private/request.d.ts +0 -142
- package/declarations/-private/store-provider.d.ts +0 -19
- package/dist/reactive-context-ClTRYXie.js +0 -154
- package/dist/unpkg/dev/reactive-context-CTtwoaBx.js +0 -290
- package/dist/unpkg/dev-deprecated/reactive-context-sqyJMb7I.js +0 -290
- package/dist/unpkg/prod/reactive-context-DKimDkR3.js +0 -110
- package/dist/unpkg/prod-deprecated/reactive-context-DKimDkR3.js +0 -110
package/dist/install.js
CHANGED
|
@@ -1,104 +1,100 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Signal } from
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
1
|
+
import { n as WatcherContext } from "./reactive-context-DNq02nr1.js";
|
|
2
|
+
import { Signal } from "signal-polyfill";
|
|
3
|
+
import { use } from "react";
|
|
4
|
+
import { getGlobalConfig, macroCondition } from "@embroider/macros";
|
|
5
|
+
import { setupSignals } from "@warp-drive/core/configure";
|
|
6
6
|
|
|
7
|
+
//#region src/install.ts
|
|
7
8
|
/**
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
* {@include ./install.md}
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
12
|
function tryConsumeContext(signal) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
} catch {
|
|
30
|
-
// eslint-disable-next-line no-console
|
|
31
|
-
console.error = logError;
|
|
32
|
-
// if we are not in a React context, we will Error
|
|
33
|
-
// so we just ignore it.
|
|
34
|
-
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_REACT_SIGNAL_INTEGRATION)) {
|
|
35
|
-
if (getGlobalConfig().WarpDrive.debug.LOG_REACT_SIGNAL_INTEGRATION || globalThis.getWarpDriveRuntimeConfig().debug.LOG_REACT_SIGNAL_INTEGRATION) {
|
|
36
|
-
// eslint-disable-next-line no-console
|
|
37
|
-
console.log(`[WarpDrive] No Context Available To Consume Signal`, signal);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
13
|
+
const logError = console.error;
|
|
14
|
+
try {
|
|
15
|
+
console.error = () => {};
|
|
16
|
+
const watcher = use(WatcherContext);
|
|
17
|
+
console.error = logError;
|
|
18
|
+
watcher?.watcher.watch(signal);
|
|
19
|
+
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_REACT_SIGNAL_INTEGRATION)) {
|
|
20
|
+
if (getGlobalConfig().WarpDrive.debug.LOG_REACT_SIGNAL_INTEGRATION || globalThis.getWarpDriveRuntimeConfig().debug.LOG_REACT_SIGNAL_INTEGRATION) console.log(`[WarpDrive] Consumed Context Signal`, signal, watcher);
|
|
21
|
+
}
|
|
22
|
+
} catch {
|
|
23
|
+
console.error = logError;
|
|
24
|
+
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_REACT_SIGNAL_INTEGRATION)) {
|
|
25
|
+
if (getGlobalConfig().WarpDrive.debug.LOG_REACT_SIGNAL_INTEGRATION || globalThis.getWarpDriveRuntimeConfig().debug.LOG_REACT_SIGNAL_INTEGRATION) console.log(`[WarpDrive] No Context Available To Consume Signal`, signal);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
41
28
|
}
|
|
42
29
|
let pending;
|
|
30
|
+
/**
|
|
31
|
+
* Resolves once all pending requests started via WarpDrive's React signal
|
|
32
|
+
* integration have settled. Only tracks requests while `TESTING` is enabled;
|
|
33
|
+
* a no-op otherwise.
|
|
34
|
+
*
|
|
35
|
+
* @public
|
|
36
|
+
*/
|
|
43
37
|
async function settled() {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
38
|
+
if (macroCondition(getGlobalConfig().WarpDrive.env.TESTING)) {
|
|
39
|
+
if (!pending || !pending.length) return;
|
|
40
|
+
const current = pending ?? [];
|
|
41
|
+
pending = [];
|
|
42
|
+
await Promise.allSettled(current);
|
|
43
|
+
await Promise.resolve();
|
|
44
|
+
await Promise.resolve();
|
|
45
|
+
await Promise.resolve();
|
|
46
|
+
return settled();
|
|
47
|
+
}
|
|
55
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* Builds the {@link SignalHooks} implementation backed by the
|
|
51
|
+
* [Signal Polyfill](https://github.com/proposal-signals/signal-polyfill),
|
|
52
|
+
* used to wire WarpDrive's reactivity primitives into React.
|
|
53
|
+
*
|
|
54
|
+
* @public
|
|
55
|
+
*/
|
|
56
56
|
function buildSignalConfig(options) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
pending.push(newPromise);
|
|
96
|
-
return newPromise;
|
|
97
|
-
}
|
|
98
|
-
return promise;
|
|
99
|
-
},
|
|
100
|
-
willSyncFlushWatchers: () => false
|
|
101
|
-
};
|
|
57
|
+
return {
|
|
58
|
+
createSignal: (obj, key) => new Signal.State(macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? {
|
|
59
|
+
obj,
|
|
60
|
+
key
|
|
61
|
+
} : null, { equals: () => false }),
|
|
62
|
+
notifySignal: (signal) => {
|
|
63
|
+
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_REACT_SIGNAL_INTEGRATION)) {
|
|
64
|
+
if (getGlobalConfig().WarpDrive.debug.LOG_REACT_SIGNAL_INTEGRATION || globalThis.getWarpDriveRuntimeConfig().debug.LOG_REACT_SIGNAL_INTEGRATION) {
|
|
65
|
+
if (Signal.subtle.hasSinks(signal)) console.log(`[WarpDrive] Notifying Signal`, signal);
|
|
66
|
+
else console.log(`[WarpDrive] Notified Signal That Has No Watcher`, signal);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
signal.set(signal.get());
|
|
70
|
+
},
|
|
71
|
+
consumeSignal: (signal) => {
|
|
72
|
+
tryConsumeContext(signal);
|
|
73
|
+
signal.get();
|
|
74
|
+
},
|
|
75
|
+
createMemo: (object, key, fn) => {
|
|
76
|
+
const memo = new Signal.Computed(fn);
|
|
77
|
+
return () => {
|
|
78
|
+
tryConsumeContext(memo);
|
|
79
|
+
return memo.get();
|
|
80
|
+
};
|
|
81
|
+
},
|
|
82
|
+
waitFor: (promise) => {
|
|
83
|
+
if (macroCondition(getGlobalConfig().WarpDrive.env.TESTING)) {
|
|
84
|
+
pending = pending || [];
|
|
85
|
+
const newPromise = promise.finally(() => {
|
|
86
|
+
pending = pending.filter((p) => p !== newPromise);
|
|
87
|
+
});
|
|
88
|
+
pending.push(newPromise);
|
|
89
|
+
return newPromise;
|
|
90
|
+
}
|
|
91
|
+
return promise;
|
|
92
|
+
},
|
|
93
|
+
willSyncFlushWatchers: () => false
|
|
94
|
+
};
|
|
102
95
|
}
|
|
103
96
|
setupSignals(buildSignalConfig);
|
|
104
|
-
|
|
97
|
+
|
|
98
|
+
//#endregion
|
|
99
|
+
export { buildSignalConfig, settled };
|
|
100
|
+
//# sourceMappingURL=install.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"install.js","names":["use","Signal","setupSignals","WatcherContext","getGlobalConfig","macroCondition","tryConsumeContext","signal","logError","console","error","watcher","watch","WarpDrive","activeLogging","LOG_REACT_SIGNAL_INTEGRATION","debug","globalThis","getWarpDriveRuntimeConfig","log","pending","settled","env","TESTING","length","current","Promise","allSettled","resolve","buildSignalConfig","options","createSignal","obj","key","State","DEBUG","equals","notifySignal","subtle","hasSinks","set","get","consumeSignal","createMemo","object","fn","memo","Computed","waitFor","promise","newPromise","finally","filter","p","push","willSyncFlushWatchers"],"sources":["../src/install.ts"],"sourcesContent":["/**\n * {@include ./install.md}\n * @module\n */\n\nimport { use } from 'react';\nimport { Signal } from 'signal-polyfill';\n\nimport { LOG_REACT_SIGNAL_INTEGRATION } from '@warp-drive/core/build-config/debugging';\nimport { DEBUG, TESTING } from '@warp-drive/core/build-config/env';\nimport { type HooksOptions, setupSignals, type SignalHooks } from '@warp-drive/core/configure';\n\nimport { WatcherContext } from './-private/reactive-context';\n\nfunction tryConsumeContext(signal: Signal.State<unknown> | Signal.Computed<unknown>): void {\n // eslint-disable-next-line no-console\n const logError = console.error;\n try {\n // eslint-disable-next-line no-console\n console.error = () => {};\n // ensure signals are watched by our closest watcher\n const watcher = use(WatcherContext);\n // eslint-disable-next-line no-console\n console.error = logError;\n watcher?.watcher.watch(signal);\n if (LOG_REACT_SIGNAL_INTEGRATION) {\n // eslint-disable-next-line no-console\n console.log(`[WarpDrive] Consumed Context Signal`, signal, watcher);\n }\n } catch {\n // eslint-disable-next-line no-console\n console.error = logError;\n // if we are not in a React context, we will Error\n // so we just ignore it.\n if (LOG_REACT_SIGNAL_INTEGRATION) {\n // eslint-disable-next-line no-console\n console.log(`[WarpDrive] No Context Available To Consume Signal`, signal);\n }\n }\n}\n\nlet pending: Promise<unknown>[];\n/**\n * Resolves once all pending requests started via WarpDrive's React signal\n * integration have settled. Only tracks requests while `TESTING` is enabled;\n * a no-op otherwise.\n *\n * @public\n */\nexport async function settled(): Promise<void> {\n if (TESTING) {\n // in testing mode we provide a test waiter integration\n if (!pending || !pending.length) return;\n const current = pending ?? [];\n pending = [];\n await Promise.allSettled(current);\n await Promise.resolve();\n await Promise.resolve();\n await Promise.resolve();\n\n return settled();\n }\n}\n\n/**\n * Builds the {@link SignalHooks} implementation backed by the\n * [Signal Polyfill](https://github.com/proposal-signals/signal-polyfill),\n * used to wire WarpDrive's reactivity primitives into React.\n *\n * @public\n */\nexport function buildSignalConfig(options: HooksOptions): SignalHooks {\n return {\n createSignal: (obj: object, key: string | symbol) =>\n new Signal.State(DEBUG ? { obj, key } : null, { equals: () => false }),\n\n notifySignal: (signal: Signal.State<unknown>) => {\n if (LOG_REACT_SIGNAL_INTEGRATION) {\n if (Signal.subtle.hasSinks(signal)) {\n // eslint-disable-next-line no-console\n console.log(`[WarpDrive] Notifying Signal`, signal);\n } else {\n // eslint-disable-next-line no-console\n console.log(`[WarpDrive] Notified Signal That Has No Watcher`, signal);\n }\n }\n signal.set(signal.get());\n },\n\n consumeSignal: (signal: Signal.State<unknown>) => {\n tryConsumeContext(signal);\n void signal.get();\n },\n\n createMemo: <F>(object: object, key: string | symbol, fn: () => F): (() => F) => {\n const memo = new Signal.Computed<F>(fn);\n return () => {\n tryConsumeContext(memo);\n\n return memo.get();\n };\n },\n\n waitFor: (promise) => {\n if (TESTING) {\n pending = pending || [];\n const newPromise = promise.finally(() => {\n pending = pending.filter((p) => p !== newPromise);\n });\n pending.push(newPromise);\n return newPromise;\n }\n return promise;\n },\n\n willSyncFlushWatchers: () => false,\n } as SignalHooks;\n}\n\nsetupSignals(buildSignalConfig);\n"],"mappings":";;;;;;;;;;;AAcA,SAASM,kBAAkBC,QAAgE;CAEzF,MAAMC,WAAWC,QAAQC;CACzB,IAAI;EAEFD,QAAQC,cAAc,CAAC;EAEvB,MAAMC,UAAUX,IAAIG,cAAc;EAElCM,QAAQC,QAAQF;EAChBG,S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BAAkC;KAChC,IAAId,OAAOqC,OAAOC,SAAShC,MAAM,GAE/BE,QAAQU,IAAI,gCAAgCZ,MAAM;UAGlDE,QAAQU,IAAI,mDAAmDZ,MAAM;IAEzE;;GACAA,OAAOiC,IAAIjC,OAAOkC,IAAI,CAAC;EACzB;EAEAC,gBAAgBnC,WAAkC;GAChDD,kBAAkBC,MAAM;GACxB,AAAKA,OAAOkC,IAAI;EAClB;EAEAE,aAAgBC,QAAgBX,KAAsBY,OAA2B;GAC/E,MAAMC,OAAO,IAAI7C,OAAO8C,SAAYF,EAAE;GACtC,aAAa;IACXvC,kBAAkBwC,IAAI;IAEtB,OAAOA,KAAKL,IAAI;GAClB;EACF;EAEAO,UAAUC,YAAY;GACpB,IAAA5C,eAAAD,gBAAA,CAAA,CAAAS,UAAAS,IAAAC,OAAA,GAAa;IACXH,UAAUA,WAAW,CAAA;IACrB,MAAM8B,aAAaD,QAAQE,cAAc;KACvC/B,UAAUA,QAAQgC,QAAQC,MAAMA,MAAMH,UAAU;IAClD,CAAC;IACD9B,QAAQkC,KAAKJ,UAAU;IACvB,OAAOA;GACT;GACA,OAAOD;EACT;EAEAM,6BAA6B;CAC/B;AACF;AAEArD,aAAa2B,iBAAiB"}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { Signal } from "signal-polyfill";
|
|
2
|
+
import { createContext, useMemo, useSyncExternalStore } from "react";
|
|
3
|
+
import { getGlobalConfig, macroCondition } from "@embroider/macros";
|
|
4
|
+
import { jsx } from "react/jsx-runtime";
|
|
5
|
+
|
|
6
|
+
//#region src/-private/reactive-context.tsx
|
|
7
|
+
let nextFlush = null;
|
|
8
|
+
let watchers = [];
|
|
9
|
+
let watcherId = 0;
|
|
10
|
+
function clearWatcher(state) {
|
|
11
|
+
state.watcher.unwatch(...Signal.subtle.introspectSources(state.watcher));
|
|
12
|
+
}
|
|
13
|
+
function flush(state) {
|
|
14
|
+
state.pending = false;
|
|
15
|
+
if (state.destroyed) {
|
|
16
|
+
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_REACT_SIGNAL_INTEGRATION)) {
|
|
17
|
+
if (getGlobalConfig().WarpDrive.debug.LOG_REACT_SIGNAL_INTEGRATION || globalThis.getWarpDriveRuntimeConfig().debug.LOG_REACT_SIGNAL_INTEGRATION) console.log(`[WarpDrive] Detected Watcher Destroyed During Notify Flush, clearing signals`);
|
|
18
|
+
}
|
|
19
|
+
state.snapshot = null;
|
|
20
|
+
clearWatcher(state);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_REACT_SIGNAL_INTEGRATION)) {
|
|
24
|
+
if (getGlobalConfig().WarpDrive.debug.LOG_REACT_SIGNAL_INTEGRATION || globalThis.getWarpDriveRuntimeConfig().debug.LOG_REACT_SIGNAL_INTEGRATION) {
|
|
25
|
+
console.log(`[WarpDrive] Notifying React That WatcherContext:${state.watcherId} Has Updated`);
|
|
26
|
+
console.log("all signals", new Set(Signal.subtle.introspectSources(state.watcher)));
|
|
27
|
+
console.log("dirty signals", new Set(state.watcher.getPending()));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
state.snapshot = { watcher: state.watcher };
|
|
31
|
+
if (state.notifyReact) state.notifyReact();
|
|
32
|
+
state.watcher.watch();
|
|
33
|
+
}
|
|
34
|
+
function _createWatcher() {
|
|
35
|
+
const id = watcherId++;
|
|
36
|
+
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_REACT_SIGNAL_INTEGRATION)) {
|
|
37
|
+
if (getGlobalConfig().WarpDrive.debug.LOG_REACT_SIGNAL_INTEGRATION || globalThis.getWarpDriveRuntimeConfig().debug.LOG_REACT_SIGNAL_INTEGRATION) console.log(`[WarpDrive] Creating a WatcherContext:${id}`);
|
|
38
|
+
}
|
|
39
|
+
const state = {
|
|
40
|
+
watcherId: id,
|
|
41
|
+
pending: false,
|
|
42
|
+
destroyed: false,
|
|
43
|
+
notifyReact: null,
|
|
44
|
+
watcher: null,
|
|
45
|
+
snapshot: null
|
|
46
|
+
};
|
|
47
|
+
state.watcher = new Signal.subtle.Watcher((...args) => {
|
|
48
|
+
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_REACT_SIGNAL_INTEGRATION)) {
|
|
49
|
+
if (getGlobalConfig().WarpDrive.debug.LOG_REACT_SIGNAL_INTEGRATION || globalThis.getWarpDriveRuntimeConfig().debug.LOG_REACT_SIGNAL_INTEGRATION) console.log(`watcher ${state.watcherId} notified`, args, state.watcher);
|
|
50
|
+
}
|
|
51
|
+
if (!state.pending && !state.destroyed) {
|
|
52
|
+
watchers.push(state);
|
|
53
|
+
state.pending = true;
|
|
54
|
+
if (!nextFlush) nextFlush = new Promise((resolve) => {
|
|
55
|
+
queueMicrotask(() => {
|
|
56
|
+
queueMicrotask(() => {
|
|
57
|
+
queueMicrotask(() => {
|
|
58
|
+
watchers.forEach(flush);
|
|
59
|
+
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_REACT_SIGNAL_INTEGRATION)) {
|
|
60
|
+
if (getGlobalConfig().WarpDrive.debug.LOG_REACT_SIGNAL_INTEGRATION || globalThis.getWarpDriveRuntimeConfig().debug.LOG_REACT_SIGNAL_INTEGRATION) console.log("Flushed watcher:", watchers.map((w) => w.watcherId));
|
|
61
|
+
}
|
|
62
|
+
watchers = [];
|
|
63
|
+
nextFlush = null;
|
|
64
|
+
resolve();
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
} else if (state.destroyed) {
|
|
70
|
+
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_REACT_SIGNAL_INTEGRATION)) {
|
|
71
|
+
if (getGlobalConfig().WarpDrive.debug.LOG_REACT_SIGNAL_INTEGRATION || globalThis.getWarpDriveRuntimeConfig().debug.LOG_REACT_SIGNAL_INTEGRATION) console.log(`[WarpDrive] Detected Watcher Destroyed During Notify, clearing signals`);
|
|
72
|
+
}
|
|
73
|
+
state.snapshot = null;
|
|
74
|
+
clearWatcher(state);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
state.watcher.watch();
|
|
78
|
+
state.snapshot = { watcher: state.watcher };
|
|
79
|
+
return state;
|
|
80
|
+
}
|
|
81
|
+
function useWatcher() {
|
|
82
|
+
const state = useMemo(_createWatcher, []);
|
|
83
|
+
return useSyncExternalStore((notifyChanged) => {
|
|
84
|
+
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_REACT_SIGNAL_INTEGRATION)) {
|
|
85
|
+
if (getGlobalConfig().WarpDrive.debug.LOG_REACT_SIGNAL_INTEGRATION || globalThis.getWarpDriveRuntimeConfig().debug.LOG_REACT_SIGNAL_INTEGRATION) console.log(`[WarpDrive] Subscribing to Watcher`);
|
|
86
|
+
}
|
|
87
|
+
state.destroyed = false;
|
|
88
|
+
state.notifyReact = notifyChanged;
|
|
89
|
+
state.watcher.watch();
|
|
90
|
+
return () => {
|
|
91
|
+
if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_REACT_SIGNAL_INTEGRATION)) {
|
|
92
|
+
if (getGlobalConfig().WarpDrive.debug.LOG_REACT_SIGNAL_INTEGRATION || globalThis.getWarpDriveRuntimeConfig().debug.LOG_REACT_SIGNAL_INTEGRATION) console.log(`[WarpDrive] Deactivating Watcher Subscription`);
|
|
93
|
+
}
|
|
94
|
+
state.destroyed = true;
|
|
95
|
+
state.notifyReact = null;
|
|
96
|
+
};
|
|
97
|
+
}, () => state.snapshot);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* @category Contexts
|
|
101
|
+
*/
|
|
102
|
+
const WatcherContext = /*#__PURE__*/ createContext(null);
|
|
103
|
+
/**
|
|
104
|
+
*
|
|
105
|
+
* @category Components
|
|
106
|
+
*/
|
|
107
|
+
function ReactiveContext({ children }) {
|
|
108
|
+
const watcher = useWatcher();
|
|
109
|
+
/**
|
|
110
|
+
* Unlike other frameworks, React does not have a built-in way to provide
|
|
111
|
+
* a context value other than by rendering an extra component.
|
|
112
|
+
*
|
|
113
|
+
*/
|
|
114
|
+
return /*#__PURE__*/ jsx(WatcherContext, {
|
|
115
|
+
value: watcher,
|
|
116
|
+
children
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
//#endregion
|
|
121
|
+
export { WatcherContext as n, ReactiveContext as t };
|
|
122
|
+
//# sourceMappingURL=reactive-context-DNq02nr1.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reactive-context-DNq02nr1.js","names":["Signal","createContext","useSyncExternalStore","useMemo","getGlobalConfig","macroCondition","jsx","_jsx","nextFlush","watchers","watcherId","clearWatcher","state","watcher","unwatch","subtle","introspectSources","flush","pending","destroyed","WarpDrive","activeLogging","LOG_REACT_SIGNAL_INTEGRATION","debug","globalThis","getWarpDriveRuntimeConfig","console","log","snapshot","Set","getPending","notifyReact","watch","_createWatcher","id","Watcher","args","push","Promise","resolve","queueMicrotask","forEach","map","w","useWatcher","notifyChanged","WatcherContext","ReactiveContext","children","value"],"sources":["../src/-private/reactive-context.tsx"],"sourcesContent":["import { Signal } from \"signal-polyfill\";\nimport { createContext, type JSX, type ReactNode, useSyncExternalStore, type Context, useMemo } from \"react\";\nimport { LOG_REACT_SIGNAL_INTEGRATION } from \"@warp-drive/core/build-config/debugging\";\n\nlet nextFlush: Promise<void> | null = null;\nlet watchers: WatcherState[] = [];\nlet watcherId = 0;\n\ninterface WatcherState {\n watcherId: number;\n pending: boolean;\n destroyed: boolean;\n notifyReact: (() => void) | null;\n watcher: Signal.subtle.Watcher;\n\n // the extra wrapper returned here ensures that the context value for the watcher\n // changes causing a re-render when the watcher is updated.\n snapshot: { watcher: Signal.subtle.Watcher } | null;\n}\n\nfunction clearWatcher(state: WatcherState) {\n state.watcher.unwatch(...Signal.subtle.introspectSources(state.watcher));\n}\n\nfunction flush(state: WatcherState) {\n state.pending = false;\n if (state.destroyed) {\n if (LOG_REACT_SIGNAL_INTEGRATION) {\n console.log(`[WarpDrive] Detected Watcher Destroyed During Notify Flush, clearing signals`);\n }\n state.snapshot = null;\n clearWatcher(state);\n return;\n }\n\n if (LOG_REACT_SIGNAL_INTEGRATION) {\n console.log(`[WarpDrive] Notifying React That WatcherContext:${state.watcherId} Has Updated`);\n console.log(\"all signals\", new Set(Signal.subtle.introspectSources(state.watcher)));\n console.log(\"dirty signals\", new Set(state.watcher.getPending()));\n }\n\n // any time signals have changed, we notify React that our store has updated\n state.snapshot = { watcher: state.watcher };\n if (state.notifyReact) state.notifyReact();\n\n // tell the Watcher to start watching for changes again\n // by signaling that notifications have been flushed.\n state.watcher.watch();\n}\n\nfunction _createWatcher() {\n const id = watcherId++;\n if (LOG_REACT_SIGNAL_INTEGRATION) {\n console.log(`[WarpDrive] Creating a WatcherContext:${id}`);\n }\n const state: WatcherState = {\n watcherId: id,\n pending: false,\n destroyed: false,\n notifyReact: null as (() => void) | null,\n watcher: null as unknown as Signal.subtle.Watcher,\n\n // the extra wrapper returned here ensures that the context value for the watcher\n // changes causing a re-render when the watcher is updated.\n snapshot: null as { watcher: Signal.subtle.Watcher } | null,\n };\n\n state.watcher = new Signal.subtle.Watcher((...args) => {\n if (LOG_REACT_SIGNAL_INTEGRATION) {\n console.log(`watcher ${state.watcherId} notified`, args, state.watcher);\n }\n if (!state.pending && !state.destroyed) {\n watchers.push(state);\n state.pending = true;\n\n if (!nextFlush) {\n nextFlush = new Promise((resolve) => {\n queueMicrotask(() => {\n queueMicrotask(() => {\n queueMicrotask(() => {\n watchers.forEach(flush);\n if (LOG_REACT_SIGNAL_INTEGRATION) {\n console.log(\n \"Flushed watcher:\",\n watchers.map((w) => w.watcherId)\n );\n }\n watchers = [];\n nextFlush = null;\n\n resolve();\n });\n });\n });\n });\n }\n } else if (state.destroyed) {\n if (LOG_REACT_SIGNAL_INTEGRATION) {\n console.log(`[WarpDrive] Detected Watcher Destroyed During Notify, clearing signals`);\n }\n // if we are destroyed, we clear the watcher signals\n // so that it does not continue to watch for changes.\n state.snapshot = null;\n clearWatcher(state);\n }\n });\n\n // The watcher won't begin watching until we call `watcher.watch()`\n state.watcher.watch();\n state.snapshot = { watcher: state.watcher };\n\n return state;\n}\n\nexport function useWatcher(): { watcher: Signal.subtle.Watcher } | null {\n const state = useMemo(_createWatcher, []);\n\n return useSyncExternalStore(\n (notifyChanged: () => void) => {\n if (LOG_REACT_SIGNAL_INTEGRATION) {\n console.log(`[WarpDrive] Subscribing to Watcher`);\n }\n state.destroyed = false;\n state.notifyReact = notifyChanged;\n\n // The watcher won't begin watching until we call `watcher.watch()`\n state.watcher.watch();\n\n return () => {\n if (LOG_REACT_SIGNAL_INTEGRATION) {\n console.log(`[WarpDrive] Deactivating Watcher Subscription`);\n }\n state.destroyed = true;\n state.notifyReact = null;\n };\n },\n () => state.snapshot\n );\n}\n\n/**\n * @category Contexts\n */\nexport const WatcherContext: Context<{\n /**\n * The `Signal.subtle.Watcher` used to observe signal changes for the nearest {@link ReactiveContext}.\n */\n watcher: Signal.subtle.Watcher;\n} | null> = createContext<{\n watcher: Signal.subtle.Watcher;\n} | null>(null);\n\n/**\n *\n * @category Components\n */\nexport function ReactiveContext({ children }: { children: ReactNode }): JSX.Element {\n const watcher = useWatcher();\n /**\n * Unlike other frameworks, React does not have a built-in way to provide\n * a context value other than by rendering an extra component.\n *\n */\n return <WatcherContext value={watcher}>{children}</WatcherContext>;\n}\n"],"mappings":";;;;;;AAIA,IAAIQ,YAAkC;AACtC,IAAIC,WAA2B,CAAA;AAC/B,IAAIC,YAAY;AAchB,SAASC,aAAaC,OAAqB;CACzCA,MAAMC,QAAQC,QAAQ,GAAGd,OAAOe,OAAOC,kBAAkBJ,MAAMC,OAAO,CAAC;AACzE;AAEA,SAASI,MAAML,OAAqB;CAClCA,MAAMM,UAAU;CAChB,IAAIN,MAAMO,WAAW;EACnB,IAAAd,eAAAD,gBAAA,CAAA,CAAAgB,UAAAC,cAAAC,4BAAA,GAAA;OAA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