@warp-drive-mirror/react 5.8.0-alpha.3 → 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.
Files changed (50) hide show
  1. package/README.md +36 -15
  2. package/dist/unpkg/dev/declarations/-private/reactive-context.d.ts +18 -0
  3. package/dist/unpkg/dev/declarations/-private/request.d.ts +141 -0
  4. package/dist/unpkg/dev/declarations/-private/store-provider.d.ts +19 -0
  5. package/dist/unpkg/dev/declarations/index.d.ts +7 -0
  6. package/dist/unpkg/dev/declarations/install.d.ts +3 -0
  7. package/dist/unpkg/dev/index.js +274 -0
  8. package/dist/unpkg/dev/install.js +104 -0
  9. package/dist/unpkg/dev/reactive-context-ClTRYXie.js +154 -0
  10. package/dist/unpkg/dev-deprecated/declarations/-private/reactive-context.d.ts +18 -0
  11. package/dist/unpkg/dev-deprecated/declarations/-private/request.d.ts +141 -0
  12. package/dist/unpkg/dev-deprecated/declarations/-private/store-provider.d.ts +19 -0
  13. package/dist/unpkg/dev-deprecated/declarations/index.d.ts +7 -0
  14. package/dist/unpkg/dev-deprecated/declarations/install.d.ts +3 -0
  15. package/dist/unpkg/dev-deprecated/index.js +274 -0
  16. package/dist/unpkg/dev-deprecated/install.js +104 -0
  17. package/dist/unpkg/dev-deprecated/reactive-context-ClTRYXie.js +154 -0
  18. package/dist/unpkg/prod/declarations/-private/reactive-context.d.ts +18 -0
  19. package/dist/unpkg/prod/declarations/-private/request.d.ts +141 -0
  20. package/dist/unpkg/prod/declarations/-private/store-provider.d.ts +19 -0
  21. package/dist/unpkg/prod/declarations/index.d.ts +7 -0
  22. package/dist/unpkg/prod/declarations/install.d.ts +3 -0
  23. package/dist/unpkg/prod/index.js +274 -0
  24. package/dist/unpkg/prod/install.js +104 -0
  25. package/dist/unpkg/prod/reactive-context-ClTRYXie.js +154 -0
  26. package/dist/unpkg/prod-deprecated/declarations/-private/reactive-context.d.ts +18 -0
  27. package/dist/unpkg/prod-deprecated/declarations/-private/request.d.ts +141 -0
  28. package/dist/unpkg/prod-deprecated/declarations/-private/store-provider.d.ts +19 -0
  29. package/dist/unpkg/prod-deprecated/declarations/index.d.ts +7 -0
  30. package/dist/unpkg/prod-deprecated/declarations/install.d.ts +3 -0
  31. package/dist/unpkg/prod-deprecated/index.js +274 -0
  32. package/dist/unpkg/prod-deprecated/install.js +104 -0
  33. package/dist/unpkg/prod-deprecated/reactive-context-ClTRYXie.js +154 -0
  34. package/logos/README.md +2 -2
  35. package/logos/logo-yellow-slab.svg +1 -0
  36. package/logos/word-mark-black.svg +1 -0
  37. package/logos/word-mark-white.svg +1 -0
  38. package/package.json +5 -5
  39. package/logos/NCC-1701-a-blue.svg +0 -4
  40. package/logos/NCC-1701-a-gold.svg +0 -4
  41. package/logos/NCC-1701-a-gold_100.svg +0 -1
  42. package/logos/NCC-1701-a-gold_base-64.txt +0 -1
  43. package/logos/NCC-1701-a.svg +0 -4
  44. package/logos/docs-badge.svg +0 -2
  45. package/logos/ember-data-logo-dark.svg +0 -12
  46. package/logos/ember-data-logo-light.svg +0 -12
  47. package/logos/social1.png +0 -0
  48. package/logos/social2.png +0 -0
  49. package/logos/warp-drive-logo-dark.svg +0 -4
  50. package/logos/warp-drive-logo-gold.svg +0 -4
@@ -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 {};
@@ -0,0 +1,19 @@
1
+ import { Store } from "@warp-drive-mirror/core";
2
+ import { JSX, ReactNode } from "react";
3
+ /**
4
+ * @category Hooks
5
+ */
6
+ export declare function useStore(): Store;
7
+ type WithExistingStore = {
8
+ store: Store;
9
+ children: ReactNode;
10
+ };
11
+ type WithNewStore = {
12
+ Store: typeof Store;
13
+ children: ReactNode;
14
+ };
15
+ /**
16
+ * @category Components
17
+ */
18
+ export declare function StoreProvider($props: WithExistingStore | WithNewStore): JSX.Element;
19
+ export {};
@@ -0,0 +1,7 @@
1
+ /**
2
+ * @module
3
+ * @mergeModuleWith <project>
4
+ */
5
+ export { ReactiveContext, WatcherContext } from "./-private/reactive-context.js";
6
+ export { StoreProvider, useStore } from "./-private/store-provider.js";
7
+ export { Request, Throw } from "./-private/request.js";
@@ -0,0 +1,3 @@
1
+ import { type HooksOptions, type SignalHooks } from "@warp-drive-mirror/core/configure";
2
+ export declare function settled(): Promise<void>;
3
+ export declare function buildSignalConfig(options: HooksOptions): SignalHooks;