@warp-drive-mirror/react 5.8.0-alpha.30 → 5.8.0-alpha.32
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/dist/unpkg/dev/declarations/-private/reactive-context.d.ts +18 -0
- package/dist/unpkg/dev/declarations/-private/request.d.ts +141 -0
- package/dist/unpkg/dev/declarations/-private/store-provider.d.ts +19 -0
- package/dist/unpkg/dev/declarations/index.d.ts +7 -0
- package/dist/unpkg/dev/declarations/install.d.ts +3 -0
- package/dist/unpkg/dev/index.js +274 -0
- package/dist/unpkg/dev/install.js +104 -0
- package/dist/unpkg/dev/reactive-context-ClTRYXie.js +154 -0
- package/dist/unpkg/dev-deprecated/declarations/-private/reactive-context.d.ts +18 -0
- package/dist/unpkg/dev-deprecated/declarations/-private/request.d.ts +141 -0
- package/dist/unpkg/dev-deprecated/declarations/-private/store-provider.d.ts +19 -0
- package/dist/unpkg/dev-deprecated/declarations/index.d.ts +7 -0
- package/dist/unpkg/dev-deprecated/declarations/install.d.ts +3 -0
- package/dist/unpkg/dev-deprecated/index.js +274 -0
- package/dist/unpkg/dev-deprecated/install.js +104 -0
- package/dist/unpkg/dev-deprecated/reactive-context-ClTRYXie.js +154 -0
- package/dist/unpkg/prod/declarations/-private/reactive-context.d.ts +18 -0
- package/dist/unpkg/prod/declarations/-private/request.d.ts +141 -0
- package/dist/unpkg/prod/declarations/-private/store-provider.d.ts +19 -0
- package/dist/unpkg/prod/declarations/index.d.ts +7 -0
- package/dist/unpkg/prod/declarations/install.d.ts +3 -0
- package/dist/unpkg/prod/index.js +274 -0
- package/dist/unpkg/prod/install.js +104 -0
- package/dist/unpkg/prod/reactive-context-ClTRYXie.js +154 -0
- package/dist/unpkg/prod-deprecated/declarations/-private/reactive-context.d.ts +18 -0
- package/dist/unpkg/prod-deprecated/declarations/-private/request.d.ts +141 -0
- package/dist/unpkg/prod-deprecated/declarations/-private/store-provider.d.ts +19 -0
- package/dist/unpkg/prod-deprecated/declarations/index.d.ts +7 -0
- package/dist/unpkg/prod-deprecated/declarations/install.d.ts +3 -0
- package/dist/unpkg/prod-deprecated/index.js +274 -0
- package/dist/unpkg/prod-deprecated/install.js +104 -0
- package/dist/unpkg/prod-deprecated/reactive-context-ClTRYXie.js +154 -0
- package/package.json +5 -5
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import { R as ReactiveContext } from "./reactive-context-ClTRYXie.js";
|
|
2
|
+
export { W as WatcherContext } from "./reactive-context-ClTRYXie.js";
|
|
3
|
+
import '@warp-drive-mirror/core';
|
|
4
|
+
import { useMemo, createContext, use, useRef, useEffect } from 'react';
|
|
5
|
+
import { macroCondition, getGlobalConfig } from '@embroider/macros';
|
|
6
|
+
import { jsx, Fragment } from 'react/jsx-runtime';
|
|
7
|
+
import { DISPOSE, createRequestSubscription, signal } from '@warp-drive-mirror/core/store/-private';
|
|
8
|
+
import '@warp-drive-mirror/core/request';
|
|
9
|
+
const StoreContext = /*#__PURE__*/createContext(null);
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @category Hooks
|
|
13
|
+
*/
|
|
14
|
+
function useStore() {
|
|
15
|
+
const store = use(StoreContext);
|
|
16
|
+
macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
|
|
17
|
+
if (!test) {
|
|
18
|
+
throw new Error("No Store provided via context. Please ensure you are using <StoreProvider> to provide a Store instance.");
|
|
19
|
+
}
|
|
20
|
+
})(store) : {};
|
|
21
|
+
return store;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* @category Components
|
|
25
|
+
*/
|
|
26
|
+
function StoreProvider($props) {
|
|
27
|
+
const store = useMemo(() => "store" in $props ? $props.store : new $props.Store(), ["store" in $props ? $props.store : $props.Store]);
|
|
28
|
+
return /*#__PURE__*/jsx(StoreContext, {
|
|
29
|
+
value: store,
|
|
30
|
+
children: $props.children
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
const deferred = /* @__PURE__ */new WeakMap();
|
|
34
|
+
function deferDecorator(proto, prop, desc) {
|
|
35
|
+
let map = deferred.get(proto);
|
|
36
|
+
if (!map) {
|
|
37
|
+
map = /* @__PURE__ */new Map();
|
|
38
|
+
deferred.set(proto, map);
|
|
39
|
+
}
|
|
40
|
+
map.set(prop, desc);
|
|
41
|
+
}
|
|
42
|
+
function findDeferredDecorator(target, prop) {
|
|
43
|
+
var _a;
|
|
44
|
+
let cursor = target.prototype;
|
|
45
|
+
while (cursor) {
|
|
46
|
+
let desc = (_a = deferred.get(cursor)) == null ? void 0 : _a.get(prop);
|
|
47
|
+
if (desc) {
|
|
48
|
+
return desc;
|
|
49
|
+
}
|
|
50
|
+
cursor = cursor.prototype;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function decorateFieldV2(prototype, prop, decorators, initializer) {
|
|
54
|
+
let desc = {
|
|
55
|
+
configurable: true,
|
|
56
|
+
enumerable: true,
|
|
57
|
+
writable: true,
|
|
58
|
+
initializer: null
|
|
59
|
+
};
|
|
60
|
+
if (initializer) {
|
|
61
|
+
desc.initializer = initializer;
|
|
62
|
+
}
|
|
63
|
+
for (let decorator of decorators) {
|
|
64
|
+
desc = decorator(prototype, prop, desc) || desc;
|
|
65
|
+
}
|
|
66
|
+
if (desc.initializer === void 0) {
|
|
67
|
+
Object.defineProperty(prototype, prop, desc);
|
|
68
|
+
} else {
|
|
69
|
+
deferDecorator(prototype, prop, desc);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function initializeDeferredDecorator(target, prop) {
|
|
73
|
+
let desc = findDeferredDecorator(target.constructor, prop);
|
|
74
|
+
if (desc) {
|
|
75
|
+
Object.defineProperty(target, prop, {
|
|
76
|
+
enumerable: desc.enumerable,
|
|
77
|
+
configurable: desc.configurable,
|
|
78
|
+
writable: desc.writable,
|
|
79
|
+
value: desc.initializer ? desc.initializer.call(target) : void 0
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
const IdleBlockMissingError = new Error("No idle block provided for <Request> component, and no query or request was provided.");
|
|
84
|
+
class ReactiveArgs {
|
|
85
|
+
static {
|
|
86
|
+
decorateFieldV2(this.prototype, "request", [signal]);
|
|
87
|
+
}
|
|
88
|
+
#request = (initializeDeferredDecorator(this, "request"), void 0);
|
|
89
|
+
static {
|
|
90
|
+
decorateFieldV2(this.prototype, "query", [signal]);
|
|
91
|
+
}
|
|
92
|
+
#query = (initializeDeferredDecorator(this, "query"), void 0);
|
|
93
|
+
static {
|
|
94
|
+
decorateFieldV2(this.prototype, "autorefresh", [signal]);
|
|
95
|
+
}
|
|
96
|
+
#autorefresh = (initializeDeferredDecorator(this, "autorefresh"), void 0);
|
|
97
|
+
static {
|
|
98
|
+
decorateFieldV2(this.prototype, "autorefreshThreshold", [signal]);
|
|
99
|
+
}
|
|
100
|
+
#autorefreshThreshold = (initializeDeferredDecorator(this, "autorefreshThreshold"), void 0);
|
|
101
|
+
static {
|
|
102
|
+
decorateFieldV2(this.prototype, "autorefreshBehavior", [signal]);
|
|
103
|
+
}
|
|
104
|
+
#autorefreshBehavior = (initializeDeferredDecorator(this, "autorefreshBehavior"), void 0);
|
|
105
|
+
}
|
|
106
|
+
const DefaultChrome = ({
|
|
107
|
+
children
|
|
108
|
+
}) => {
|
|
109
|
+
return /*#__PURE__*/jsx(Fragment, {
|
|
110
|
+
children: children
|
|
111
|
+
});
|
|
112
|
+
};
|
|
113
|
+
function Throw({
|
|
114
|
+
error
|
|
115
|
+
}) {
|
|
116
|
+
throw error;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* The `<Request />` component is a powerful tool for managing data fetching and
|
|
121
|
+
* state in your React application. It provides a declarative approach to reactive
|
|
122
|
+
* control-flow for managing requests and state in your application.
|
|
123
|
+
*
|
|
124
|
+
* The `<Request />` component is ideal for handling "boundaries", outside which some
|
|
125
|
+
* state is still allowed to be unresolved and within which it MUST be resolved.
|
|
126
|
+
*
|
|
127
|
+
* ## Request States
|
|
128
|
+
*
|
|
129
|
+
* `<Request />` has five states, only one of which will be active and rendered at a time.
|
|
130
|
+
*
|
|
131
|
+
* - `idle`: The component is waiting to be given a request to monitor
|
|
132
|
+
* - `loading`: The request is in progress
|
|
133
|
+
* - `error`: The request failed
|
|
134
|
+
* - `content`: The request succeeded
|
|
135
|
+
* - `cancelled`: The request was cancelled
|
|
136
|
+
*
|
|
137
|
+
* Additionally, the `content` state has a `refresh` method that can be used to
|
|
138
|
+
* refresh the request in the background, which is available as a sub-state of
|
|
139
|
+
* the `content` state.
|
|
140
|
+
*
|
|
141
|
+
* ### Example Usage
|
|
142
|
+
*
|
|
143
|
+
* ```tsx
|
|
144
|
+
* import { Request } from "@warp-drive-mirror/react";
|
|
145
|
+
*
|
|
146
|
+
* export function UserPreview($props: { id: string | null }) {
|
|
147
|
+
* return (
|
|
148
|
+
* <Request
|
|
149
|
+
* query={findRecord('user', $props.id)}
|
|
150
|
+
* states={{
|
|
151
|
+
* idle: () => <div>Waiting for User Selection</div>,
|
|
152
|
+
* loading: ({ state }) => <div>Loading user data...</div>,
|
|
153
|
+
* cancelled: ({ error, features }) => (
|
|
154
|
+
* <div>
|
|
155
|
+
* <p>Request Cancelled</p>
|
|
156
|
+
* <p><button onClick={features.retry}>Start Again?</button></p>
|
|
157
|
+
* </div>
|
|
158
|
+
* ),
|
|
159
|
+
* error: ({ error, features }) => (
|
|
160
|
+
* <div>
|
|
161
|
+
* <p>Error: {error.message}</p>
|
|
162
|
+
* <p><button onClick={features.retry}>Try Again?</button></p>
|
|
163
|
+
* </div>
|
|
164
|
+
* ),
|
|
165
|
+
* content: ({ result, features }) => (
|
|
166
|
+
* <div>
|
|
167
|
+
* <h2>User Details</h2>
|
|
168
|
+
* <p>ID: {result.id}</p>
|
|
169
|
+
* <p>Name: {result.name}</p>
|
|
170
|
+
* </div>
|
|
171
|
+
* ),
|
|
172
|
+
* }}
|
|
173
|
+
* />
|
|
174
|
+
* );
|
|
175
|
+
* }
|
|
176
|
+
*
|
|
177
|
+
* ```
|
|
178
|
+
*
|
|
179
|
+
* @category Components
|
|
180
|
+
*/
|
|
181
|
+
function Request($props) {
|
|
182
|
+
return /*#__PURE__*/jsx(ReactiveContext, {
|
|
183
|
+
children: /*#__PURE__*/jsx(InternalRequest, {
|
|
184
|
+
...$props
|
|
185
|
+
})
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
function isStrictModeRender() {
|
|
189
|
+
const count = useRef(0);
|
|
190
|
+
|
|
191
|
+
// in debug we need to skip every second invocation
|
|
192
|
+
if (macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG)) {
|
|
193
|
+
if (count.current++ % 2 === 1) {
|
|
194
|
+
return true;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
function InternalRequest($props) {
|
|
200
|
+
const isStrict = isStrictModeRender();
|
|
201
|
+
const store = $props.store ?? useStore();
|
|
202
|
+
const Chrome = $props.chrome ?? DefaultChrome;
|
|
203
|
+
const sink = useRef(null);
|
|
204
|
+
const args = useRef(null);
|
|
205
|
+
if (!args.current) {
|
|
206
|
+
args.current = new ReactiveArgs();
|
|
207
|
+
}
|
|
208
|
+
Object.assign(args.current, $props);
|
|
209
|
+
if (sink.current && (sink.current.store !== store || $props.subscription)) {
|
|
210
|
+
sink.current[DISPOSE]();
|
|
211
|
+
sink.current = null;
|
|
212
|
+
}
|
|
213
|
+
if (!sink.current && !$props.subscription) {
|
|
214
|
+
sink.current = createRequestSubscription(store, args.current);
|
|
215
|
+
}
|
|
216
|
+
const initialized = useRef(null);
|
|
217
|
+
const effect = () => {
|
|
218
|
+
if (sink.current && (!initialized.current || initialized.current.disposable !== sink.current)) {
|
|
219
|
+
initialized.current = {
|
|
220
|
+
disposable: sink.current,
|
|
221
|
+
dispose: () => {
|
|
222
|
+
sink.current?.[DISPOSE]();
|
|
223
|
+
initialized.current = null;
|
|
224
|
+
sink.current = null;
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
return sink.current ? initialized.current.dispose : undefined;
|
|
229
|
+
};
|
|
230
|
+
let maybeEffect = effect;
|
|
231
|
+
if (macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG)) {
|
|
232
|
+
if (isStrict) {
|
|
233
|
+
maybeEffect = () => {
|
|
234
|
+
if (initialized.current) {
|
|
235
|
+
return effect();
|
|
236
|
+
}
|
|
237
|
+
return () => {
|
|
238
|
+
// initialize our actual effect
|
|
239
|
+
effect();
|
|
240
|
+
// in strict mode we don't want to run the teardown
|
|
241
|
+
// for the second invocation
|
|
242
|
+
};
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
useEffect(maybeEffect, [sink.current]);
|
|
247
|
+
const state = $props.subscription ?? sink.current;
|
|
248
|
+
const slots = $props.states;
|
|
249
|
+
return /*#__PURE__*/jsx(Chrome, {
|
|
250
|
+
state: state.isIdle ? null : state.reqState,
|
|
251
|
+
features: state.contentFeatures,
|
|
252
|
+
children:
|
|
253
|
+
// prettier-ignore
|
|
254
|
+
state.isIdle && slots.idle ? /*#__PURE__*/jsx(slots.idle, {}) : state.isIdle ? /*#__PURE__*/jsx(Throw, {
|
|
255
|
+
error: IdleBlockMissingError
|
|
256
|
+
}) : state.reqState.isLoading ? slots.loading ? /*#__PURE__*/jsx(slots.loading, {
|
|
257
|
+
state: state.reqState.loadingState
|
|
258
|
+
}) : '' : state.reqState.isCancelled && slots.cancelled ? /*#__PURE__*/jsx(slots.cancelled, {
|
|
259
|
+
error: state.reqState.reason,
|
|
260
|
+
features: state.errorFeatures
|
|
261
|
+
}) : state.reqState.isError && slots.error ? /*#__PURE__*/jsx(slots.error, {
|
|
262
|
+
error: state.reqState.reason,
|
|
263
|
+
features: state.errorFeatures
|
|
264
|
+
}) : state.reqState.isSuccess ? slots.content ? /*#__PURE__*/jsx(slots.content, {
|
|
265
|
+
result: state.reqState.value,
|
|
266
|
+
features: state.contentFeatures
|
|
267
|
+
}) : /*#__PURE__*/jsx(Throw, {
|
|
268
|
+
error: new Error('No content block provided for <Request> component.')
|
|
269
|
+
}) : !state.reqState.isCancelled ? /*#__PURE__*/jsx(Throw, {
|
|
270
|
+
error: state.reqState.reason
|
|
271
|
+
}) : '' // never
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
export { ReactiveContext, Request, StoreProvider, Throw, useStore };
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { use } from 'react';
|
|
2
|
+
import { Signal } from 'signal-polyfill';
|
|
3
|
+
import { setupSignals } from '@warp-drive-mirror/core/configure';
|
|
4
|
+
import { W as WatcherContext } from "./reactive-context-ClTRYXie.js";
|
|
5
|
+
import { macroCondition, getGlobalConfig } from '@embroider/macros';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* {@include ./install.md}
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
function tryConsumeContext(signal) {
|
|
13
|
+
// eslint-disable-next-line no-console
|
|
14
|
+
const logError = console.error;
|
|
15
|
+
try {
|
|
16
|
+
// eslint-disable-next-line no-console
|
|
17
|
+
console.error = () => {};
|
|
18
|
+
// ensure signals are watched by our closest watcher
|
|
19
|
+
const watcher = use(WatcherContext);
|
|
20
|
+
// eslint-disable-next-line no-console
|
|
21
|
+
console.error = logError;
|
|
22
|
+
watcher?.watcher.watch(signal);
|
|
23
|
+
if (macroCondition(getGlobalConfig().WarpDriveMirror.activeLogging.LOG_REACT_SIGNAL_INTEGRATION)) {
|
|
24
|
+
if (getGlobalConfig().WarpDriveMirror.debug.LOG_REACT_SIGNAL_INTEGRATION || globalThis.getWarpDriveRuntimeConfig().debug.LOG_REACT_SIGNAL_INTEGRATION) {
|
|
25
|
+
// eslint-disable-next-line no-console
|
|
26
|
+
console.log(`[WarpDrive] Consumed Context Signal`, signal, watcher);
|
|
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().WarpDriveMirror.activeLogging.LOG_REACT_SIGNAL_INTEGRATION)) {
|
|
35
|
+
if (getGlobalConfig().WarpDriveMirror.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
|
+
}
|
|
41
|
+
}
|
|
42
|
+
let pending;
|
|
43
|
+
async function settled() {
|
|
44
|
+
if (macroCondition(getGlobalConfig().WarpDriveMirror.env.TESTING)) {
|
|
45
|
+
// in testing mode we provide a test waiter integration
|
|
46
|
+
if (!pending || !pending.length) return;
|
|
47
|
+
const current = pending ?? [];
|
|
48
|
+
pending = [];
|
|
49
|
+
await Promise.allSettled(current);
|
|
50
|
+
await Promise.resolve();
|
|
51
|
+
await Promise.resolve();
|
|
52
|
+
await Promise.resolve();
|
|
53
|
+
return settled();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function buildSignalConfig(options) {
|
|
57
|
+
return {
|
|
58
|
+
createSignal: (obj, key) => new Signal.State(macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? {
|
|
59
|
+
obj,
|
|
60
|
+
key
|
|
61
|
+
} : null, {
|
|
62
|
+
equals: () => false
|
|
63
|
+
}),
|
|
64
|
+
notifySignal: signal => {
|
|
65
|
+
if (macroCondition(getGlobalConfig().WarpDriveMirror.activeLogging.LOG_REACT_SIGNAL_INTEGRATION)) {
|
|
66
|
+
if (getGlobalConfig().WarpDriveMirror.debug.LOG_REACT_SIGNAL_INTEGRATION || globalThis.getWarpDriveRuntimeConfig().debug.LOG_REACT_SIGNAL_INTEGRATION) {
|
|
67
|
+
if (Signal.subtle.hasSinks(signal)) {
|
|
68
|
+
// eslint-disable-next-line no-console
|
|
69
|
+
console.log(`[WarpDrive] Notifying Signal`, signal);
|
|
70
|
+
} else {
|
|
71
|
+
// eslint-disable-next-line no-console
|
|
72
|
+
console.log(`[WarpDrive] Notified Signal That Has No Watcher`, signal);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
signal.set(signal.get());
|
|
77
|
+
},
|
|
78
|
+
consumeSignal: signal => {
|
|
79
|
+
tryConsumeContext(signal);
|
|
80
|
+
void signal.get();
|
|
81
|
+
},
|
|
82
|
+
createMemo: (object, key, fn) => {
|
|
83
|
+
const memo = new Signal.Computed(fn);
|
|
84
|
+
return () => {
|
|
85
|
+
tryConsumeContext(memo);
|
|
86
|
+
return memo.get();
|
|
87
|
+
};
|
|
88
|
+
},
|
|
89
|
+
waitFor: promise => {
|
|
90
|
+
if (macroCondition(getGlobalConfig().WarpDriveMirror.env.TESTING)) {
|
|
91
|
+
pending = pending || [];
|
|
92
|
+
const newPromise = promise.finally(() => {
|
|
93
|
+
pending = pending.filter(p => p !== newPromise);
|
|
94
|
+
});
|
|
95
|
+
pending.push(newPromise);
|
|
96
|
+
return newPromise;
|
|
97
|
+
}
|
|
98
|
+
return promise;
|
|
99
|
+
},
|
|
100
|
+
willSyncFlushWatchers: () => false
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
setupSignals(buildSignalConfig);
|
|
104
|
+
export { buildSignalConfig, settled };
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { Signal } from 'signal-polyfill';
|
|
2
|
+
import { createContext, useMemo, useSyncExternalStore } from 'react';
|
|
3
|
+
import { macroCondition, getGlobalConfig } from '@embroider/macros';
|
|
4
|
+
import { jsx } from 'react/jsx-runtime';
|
|
5
|
+
let nextFlush = null;
|
|
6
|
+
let watchers = [];
|
|
7
|
+
let watcherId = 0;
|
|
8
|
+
function clearWatcher(state) {
|
|
9
|
+
state.watcher.unwatch(...Signal.subtle.introspectSources(state.watcher));
|
|
10
|
+
}
|
|
11
|
+
function flush(state) {
|
|
12
|
+
state.pending = false;
|
|
13
|
+
if (state.destroyed) {
|
|
14
|
+
if (macroCondition(getGlobalConfig().WarpDriveMirror.activeLogging.LOG_REACT_SIGNAL_INTEGRATION)) {
|
|
15
|
+
if (getGlobalConfig().WarpDriveMirror.debug.LOG_REACT_SIGNAL_INTEGRATION || globalThis.getWarpDriveRuntimeConfig().debug.LOG_REACT_SIGNAL_INTEGRATION) {
|
|
16
|
+
console.log(`[WarpDrive] Detected Watcher Destroyed During Notify Flush, clearing signals`);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
state.snapshot = null;
|
|
20
|
+
clearWatcher(state);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (macroCondition(getGlobalConfig().WarpDriveMirror.activeLogging.LOG_REACT_SIGNAL_INTEGRATION)) {
|
|
24
|
+
if (getGlobalConfig().WarpDriveMirror.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
|
+
|
|
31
|
+
// any time signals have changed, we notify React that our store has updated
|
|
32
|
+
state.snapshot = {
|
|
33
|
+
watcher: state.watcher
|
|
34
|
+
};
|
|
35
|
+
if (state.notifyReact) state.notifyReact();
|
|
36
|
+
|
|
37
|
+
// tell the Watcher to start watching for changes again
|
|
38
|
+
// by signaling that notifications have been flushed.
|
|
39
|
+
state.watcher.watch();
|
|
40
|
+
}
|
|
41
|
+
function _createWatcher() {
|
|
42
|
+
const id = watcherId++;
|
|
43
|
+
if (macroCondition(getGlobalConfig().WarpDriveMirror.activeLogging.LOG_REACT_SIGNAL_INTEGRATION)) {
|
|
44
|
+
if (getGlobalConfig().WarpDriveMirror.debug.LOG_REACT_SIGNAL_INTEGRATION || globalThis.getWarpDriveRuntimeConfig().debug.LOG_REACT_SIGNAL_INTEGRATION) {
|
|
45
|
+
console.log(`[WarpDrive] Creating a WatcherContext:${id}`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const state = {
|
|
49
|
+
watcherId: id,
|
|
50
|
+
pending: false,
|
|
51
|
+
destroyed: false,
|
|
52
|
+
notifyReact: null,
|
|
53
|
+
watcher: null,
|
|
54
|
+
// the extra wrapper returned here ensures that the context value for the watcher
|
|
55
|
+
// changes causing a re-render when the watcher is updated.
|
|
56
|
+
snapshot: null
|
|
57
|
+
};
|
|
58
|
+
state.watcher = new Signal.subtle.Watcher((...args) => {
|
|
59
|
+
if (macroCondition(getGlobalConfig().WarpDriveMirror.activeLogging.LOG_REACT_SIGNAL_INTEGRATION)) {
|
|
60
|
+
if (getGlobalConfig().WarpDriveMirror.debug.LOG_REACT_SIGNAL_INTEGRATION || globalThis.getWarpDriveRuntimeConfig().debug.LOG_REACT_SIGNAL_INTEGRATION) {
|
|
61
|
+
console.log(`watcher ${state.watcherId} notified`, args, state.watcher);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (!state.pending && !state.destroyed) {
|
|
65
|
+
watchers.push(state);
|
|
66
|
+
state.pending = true;
|
|
67
|
+
if (!nextFlush) {
|
|
68
|
+
nextFlush = new Promise(resolve => {
|
|
69
|
+
queueMicrotask(() => {
|
|
70
|
+
queueMicrotask(() => {
|
|
71
|
+
queueMicrotask(() => {
|
|
72
|
+
watchers.forEach(flush);
|
|
73
|
+
if (macroCondition(getGlobalConfig().WarpDriveMirror.activeLogging.LOG_REACT_SIGNAL_INTEGRATION)) {
|
|
74
|
+
if (getGlobalConfig().WarpDriveMirror.debug.LOG_REACT_SIGNAL_INTEGRATION || globalThis.getWarpDriveRuntimeConfig().debug.LOG_REACT_SIGNAL_INTEGRATION) {
|
|
75
|
+
console.log("Flushed watcher:", watchers.map(w => w.watcherId));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
watchers = [];
|
|
79
|
+
nextFlush = null;
|
|
80
|
+
resolve();
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
} else if (state.destroyed) {
|
|
87
|
+
if (macroCondition(getGlobalConfig().WarpDriveMirror.activeLogging.LOG_REACT_SIGNAL_INTEGRATION)) {
|
|
88
|
+
if (getGlobalConfig().WarpDriveMirror.debug.LOG_REACT_SIGNAL_INTEGRATION || globalThis.getWarpDriveRuntimeConfig().debug.LOG_REACT_SIGNAL_INTEGRATION) {
|
|
89
|
+
console.log(`[WarpDrive] Detected Watcher Destroyed During Notify, clearing signals`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// if we are destroyed, we clear the watcher signals
|
|
93
|
+
// so that it does not continue to watch for changes.
|
|
94
|
+
state.snapshot = null;
|
|
95
|
+
clearWatcher(state);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
// The watcher won't begin watching until we call `watcher.watch()`
|
|
100
|
+
state.watcher.watch();
|
|
101
|
+
state.snapshot = {
|
|
102
|
+
watcher: state.watcher
|
|
103
|
+
};
|
|
104
|
+
return state;
|
|
105
|
+
}
|
|
106
|
+
function useWatcher() {
|
|
107
|
+
const state = useMemo(_createWatcher, []);
|
|
108
|
+
return useSyncExternalStore(notifyChanged => {
|
|
109
|
+
if (macroCondition(getGlobalConfig().WarpDriveMirror.activeLogging.LOG_REACT_SIGNAL_INTEGRATION)) {
|
|
110
|
+
if (getGlobalConfig().WarpDriveMirror.debug.LOG_REACT_SIGNAL_INTEGRATION || globalThis.getWarpDriveRuntimeConfig().debug.LOG_REACT_SIGNAL_INTEGRATION) {
|
|
111
|
+
console.log(`[WarpDrive] Subscribing to Watcher`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
state.destroyed = false;
|
|
115
|
+
state.notifyReact = notifyChanged;
|
|
116
|
+
|
|
117
|
+
// The watcher won't begin watching until we call `watcher.watch()`
|
|
118
|
+
state.watcher.watch();
|
|
119
|
+
return () => {
|
|
120
|
+
if (macroCondition(getGlobalConfig().WarpDriveMirror.activeLogging.LOG_REACT_SIGNAL_INTEGRATION)) {
|
|
121
|
+
if (getGlobalConfig().WarpDriveMirror.debug.LOG_REACT_SIGNAL_INTEGRATION || globalThis.getWarpDriveRuntimeConfig().debug.LOG_REACT_SIGNAL_INTEGRATION) {
|
|
122
|
+
console.log(`[WarpDrive] Deactivating Watcher Subscription`);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
state.destroyed = true;
|
|
126
|
+
state.notifyReact = null;
|
|
127
|
+
};
|
|
128
|
+
}, () => state.snapshot);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* @category Contexts
|
|
133
|
+
*/
|
|
134
|
+
const WatcherContext = /*#__PURE__*/createContext(null);
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
*
|
|
138
|
+
* @category Components
|
|
139
|
+
*/
|
|
140
|
+
function ReactiveContext({
|
|
141
|
+
children
|
|
142
|
+
}) {
|
|
143
|
+
const watcher = useWatcher();
|
|
144
|
+
/**
|
|
145
|
+
* Unlike other frameworks, React does not have a built-in way to provide
|
|
146
|
+
* a context value other than by rendering an extra component.
|
|
147
|
+
*
|
|
148
|
+
*/
|
|
149
|
+
return /*#__PURE__*/jsx(WatcherContext, {
|
|
150
|
+
value: watcher,
|
|
151
|
+
children: children
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
export { ReactiveContext as R, WatcherContext as W };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Signal } from "signal-polyfill";
|
|
2
|
+
import { type JSX, type ReactNode, type Context } from "react";
|
|
3
|
+
export declare function useWatcher(): {
|
|
4
|
+
watcher: Signal.subtle.Watcher;
|
|
5
|
+
} | null;
|
|
6
|
+
/**
|
|
7
|
+
* @category Contexts
|
|
8
|
+
*/
|
|
9
|
+
export declare const WatcherContext: Context<{
|
|
10
|
+
watcher: Signal.subtle.Watcher;
|
|
11
|
+
} | null>;
|
|
12
|
+
/**
|
|
13
|
+
*
|
|
14
|
+
* @category Components
|
|
15
|
+
*/
|
|
16
|
+
export declare function ReactiveContext({ children }: {
|
|
17
|
+
children: ReactNode;
|
|
18
|
+
}): JSX.Element;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { RequestArgs, type ContentFeatures, type RecoveryFeatures, type RequestLoadingState, type RequestState } from "@warp-drive-mirror/core/store/-private";
|
|
2
|
+
import type { StructuredErrorDocument } from "@warp-drive-mirror/core/types/request";
|
|
3
|
+
import { JSX, ReactNode } from "react";
|
|
4
|
+
interface ChromeComponentProps<RT> {
|
|
5
|
+
children: ReactNode;
|
|
6
|
+
state: RequestState | null;
|
|
7
|
+
features: ContentFeatures<RT>;
|
|
8
|
+
}
|
|
9
|
+
export interface RequestProps<
|
|
10
|
+
RT,
|
|
11
|
+
E
|
|
12
|
+
> extends RequestArgs<RT, E> {
|
|
13
|
+
chrome?: React.FC<ChromeComponentProps<RT>>;
|
|
14
|
+
states: RequestStates<RT, E>;
|
|
15
|
+
}
|
|
16
|
+
interface RequestStates<
|
|
17
|
+
RT,
|
|
18
|
+
E
|
|
19
|
+
> {
|
|
20
|
+
/**
|
|
21
|
+
* The block to render when the component is idle and waiting to be given a request.
|
|
22
|
+
*
|
|
23
|
+
*/
|
|
24
|
+
idle?: React.FC<{}>;
|
|
25
|
+
/**
|
|
26
|
+
* The block to render when the request is loading.
|
|
27
|
+
*
|
|
28
|
+
*/
|
|
29
|
+
loading?: React.FC<{
|
|
30
|
+
state: RequestLoadingState;
|
|
31
|
+
}>;
|
|
32
|
+
/**
|
|
33
|
+
* The block to render when the request was cancelled.
|
|
34
|
+
*
|
|
35
|
+
*/
|
|
36
|
+
cancelled?: React.FC<{
|
|
37
|
+
/**
|
|
38
|
+
* The Error the request rejected with.
|
|
39
|
+
*/
|
|
40
|
+
error: StructuredErrorDocument<E>;
|
|
41
|
+
/**
|
|
42
|
+
* Utilities to assist in recovering from the error.
|
|
43
|
+
*/
|
|
44
|
+
features: RecoveryFeatures;
|
|
45
|
+
}>;
|
|
46
|
+
/**
|
|
47
|
+
* The block to render when the request failed. If this block is not provided,
|
|
48
|
+
* the error will be rethrown.
|
|
49
|
+
*
|
|
50
|
+
* Thus it is required to provide an error block and proper error handling if
|
|
51
|
+
* you do not want the error to crash the application.
|
|
52
|
+
*/
|
|
53
|
+
error: React.FC<{
|
|
54
|
+
/**
|
|
55
|
+
* The Error the request rejected with.
|
|
56
|
+
*/
|
|
57
|
+
error: StructuredErrorDocument<E>;
|
|
58
|
+
/**
|
|
59
|
+
* Utilities to assist in recovering from the error.
|
|
60
|
+
*/
|
|
61
|
+
features: RecoveryFeatures;
|
|
62
|
+
}>;
|
|
63
|
+
/**
|
|
64
|
+
* The block to render when the request succeeded.
|
|
65
|
+
*
|
|
66
|
+
*/
|
|
67
|
+
content: React.FC<{
|
|
68
|
+
result: RT;
|
|
69
|
+
features: ContentFeatures<RT>;
|
|
70
|
+
}>;
|
|
71
|
+
}
|
|
72
|
+
export declare function Throw({ error }: {
|
|
73
|
+
error: Error;
|
|
74
|
+
}): never;
|
|
75
|
+
/**
|
|
76
|
+
* The `<Request />` component is a powerful tool for managing data fetching and
|
|
77
|
+
* state in your React application. It provides a declarative approach to reactive
|
|
78
|
+
* control-flow for managing requests and state in your application.
|
|
79
|
+
*
|
|
80
|
+
* The `<Request />` component is ideal for handling "boundaries", outside which some
|
|
81
|
+
* state is still allowed to be unresolved and within which it MUST be resolved.
|
|
82
|
+
*
|
|
83
|
+
* ## Request States
|
|
84
|
+
*
|
|
85
|
+
* `<Request />` has five states, only one of which will be active and rendered at a time.
|
|
86
|
+
*
|
|
87
|
+
* - `idle`: The component is waiting to be given a request to monitor
|
|
88
|
+
* - `loading`: The request is in progress
|
|
89
|
+
* - `error`: The request failed
|
|
90
|
+
* - `content`: The request succeeded
|
|
91
|
+
* - `cancelled`: The request was cancelled
|
|
92
|
+
*
|
|
93
|
+
* Additionally, the `content` state has a `refresh` method that can be used to
|
|
94
|
+
* refresh the request in the background, which is available as a sub-state of
|
|
95
|
+
* the `content` state.
|
|
96
|
+
*
|
|
97
|
+
* ### Example Usage
|
|
98
|
+
*
|
|
99
|
+
* ```tsx
|
|
100
|
+
* import { Request } from "@warp-drive-mirror/react";
|
|
101
|
+
*
|
|
102
|
+
* export function UserPreview($props: { id: string | null }) {
|
|
103
|
+
* return (
|
|
104
|
+
* <Request
|
|
105
|
+
* query={findRecord('user', $props.id)}
|
|
106
|
+
* states={{
|
|
107
|
+
* idle: () => <div>Waiting for User Selection</div>,
|
|
108
|
+
* loading: ({ state }) => <div>Loading user data...</div>,
|
|
109
|
+
* cancelled: ({ error, features }) => (
|
|
110
|
+
* <div>
|
|
111
|
+
* <p>Request Cancelled</p>
|
|
112
|
+
* <p><button onClick={features.retry}>Start Again?</button></p>
|
|
113
|
+
* </div>
|
|
114
|
+
* ),
|
|
115
|
+
* error: ({ error, features }) => (
|
|
116
|
+
* <div>
|
|
117
|
+
* <p>Error: {error.message}</p>
|
|
118
|
+
* <p><button onClick={features.retry}>Try Again?</button></p>
|
|
119
|
+
* </div>
|
|
120
|
+
* ),
|
|
121
|
+
* content: ({ result, features }) => (
|
|
122
|
+
* <div>
|
|
123
|
+
* <h2>User Details</h2>
|
|
124
|
+
* <p>ID: {result.id}</p>
|
|
125
|
+
* <p>Name: {result.name}</p>
|
|
126
|
+
* </div>
|
|
127
|
+
* ),
|
|
128
|
+
* }}
|
|
129
|
+
* />
|
|
130
|
+
* );
|
|
131
|
+
* }
|
|
132
|
+
*
|
|
133
|
+
* ```
|
|
134
|
+
*
|
|
135
|
+
* @category Components
|
|
136
|
+
*/
|
|
137
|
+
export declare function Request<
|
|
138
|
+
RT,
|
|
139
|
+
E
|
|
140
|
+
>($props: RequestProps<RT, E>): JSX.Element;
|
|
141
|
+
export {};
|