@warp-drive-mirror/react 5.8.0-alpha.4 → 5.8.0-alpha.41

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/README.md CHANGED
@@ -1,25 +1,46 @@
1
1
  <p align="center">
2
2
  <img
3
3
  class="project-logo"
4
- src="./logos/NCC-1701-a-blue.svg#gh-light-mode-only"
4
+ src="./logos/logo-yellow-slab.svg"
5
5
  alt="WarpDrive"
6
- width="120px"
7
- title="WarpDrive" />
8
- <img
9
- class="project-logo"
10
- src="./logos/NCC-1701-a.svg#gh-dark-mode-only"
11
- alt="WarpDrive"
12
- width="120px"
13
- title="WarpDrive" />
6
+ width="180px"
7
+ title="WarpDrive"
8
+ />
14
9
  </p>
15
10
 
16
- <h3 align="center">:electron: Components, Hooks and Utilities for using <em style="color: lightgreen">Warp</em><strong style="color: magenta">Drive</strong> with <strong style="color: lightblue">React</strong></h3>
11
+ ![NPM Stable Version](https://img.shields.io/npm/v/ember-data/latest?label=version&style=flat&color=fdb155)
12
+ ![NPM Downloads](https://img.shields.io/npm/dm/ember-data.svg?style=flat&color=fdb155)
13
+ ![License](https://img.shields.io/github/license/warp-drive-data/warp-drive.svg?style=flat&color=fdb155)
14
+ [![EmberJS Discord Community Server](https://img.shields.io/badge/EmberJS-grey?logo=discord&logoColor=fdb155)](https://discord.gg/zT3asNS
15
+ )
16
+ [![WarpDrive Discord Server](https://img.shields.io/badge/WarpDrive-grey?logo=discord&logoColor=fdb155)](https://discord.gg/PHBbnWJx5S
17
+ )
18
+
19
+ <h3 align="center">Signals Integration and Component API for using <em>Warp</em><strong>Drive</strong> with <strong style="color: lightblue">React</strong></h3>
20
+
21
+ ***Warp*Drive** makes it easy to build scalable, fast, feature
22
+ rich applications &mdash; letting you ship better experiences more quickly without re-architecting your app or API. ***Warp*Drive** is:
23
+
24
+ - 🌌 Seamless Reactivity in any Framework
25
+ - ⚡️ Committed to Best-In-Class Performance
26
+ - 💚 Typed
27
+ - ⚛️ Works with any API
28
+ - 🌲 Focused on being as tiny as possible
29
+ - 🚀 SSR Ready
30
+ - 🐹 Built with ♥️ by [Ember](https://emberjs.com)
31
+
32
+ <br>
33
+ <br>
34
+
35
+ *Get Started* → [Guides](https://warp-drive.io/guides/)
36
+
37
+ <br>
17
38
 
18
39
  ---
19
40
 
20
- ```sh
21
- pnpm install @warp-drive-mirror/react
22
- ```
41
+ <br>
42
+
43
+ # @warp-drive-mirror/react
23
44
 
24
45
  **Tagged Releases**
25
46
 
@@ -44,7 +65,7 @@ This library provides reactive utilities for working with promises and requests,
44
65
  img.project-logo {
45
66
  padding: 0 5em 1em 5em;
46
67
  width: 100px;
47
- border-bottom: 2px solid #0969da;
68
+ border-bottom: 2px solid #bbb;
48
69
  margin: 0 auto;
49
70
  display: block;
50
71
  }
@@ -60,7 +81,7 @@ This library provides reactive utilities for working with promises and requests,
60
81
  display: inline-block;
61
82
  padding: .2rem 0;
62
83
  color: #000;
63
- border-bottom: 3px solid #0969da;
84
+ border-bottom: 3px solid #bbb;
64
85
  }
65
86
 
66
87
  details > details {
@@ -1,4 +1,5 @@
1
- import { RequestArgs, type ContentFeatures, type RecoveryFeatures, type RequestLoadingState, type RequestState } from "@warp-drive-mirror/core/store/-private";
1
+ import { type RequestLoadingState, type RequestState } from "@warp-drive-mirror/core/reactive";
2
+ import { RequestArgs, type ContentFeatures, type RecoveryFeatures } from "@warp-drive-mirror/core/signals/-leaked";
2
3
  import type { StructuredErrorDocument } from "@warp-drive-mirror/core/types/request";
3
4
  import { JSX, ReactNode } from "react";
4
5
  interface ChromeComponentProps<RT> {
package/dist/index.js CHANGED
@@ -4,7 +4,8 @@ import '@warp-drive-mirror/core';
4
4
  import { useMemo, createContext, use, useRef, useEffect } from 'react';
5
5
  import { macroCondition, getGlobalConfig } from '@embroider/macros';
6
6
  import { jsx, Fragment } from 'react/jsx-runtime';
7
- import { DISPOSE, createRequestSubscription, signal } from '@warp-drive-mirror/core/store/-private';
7
+ import { createRequestSubscription } from '@warp-drive-mirror/core/reactive';
8
+ import { DISPOSE, signal } from '@warp-drive-mirror/core/signals/-leaked';
8
9
  import '@warp-drive-mirror/core/request';
9
10
  const StoreContext = /*#__PURE__*/createContext(null);
10
11
 
@@ -0,0 +1,324 @@
1
+ import { R as ReactiveContext } from "./reactive-context-CTtwoaBx.js";
2
+ export { W as WatcherContext } from "./reactive-context-CTtwoaBx.js";
3
+ import '@warp-drive-mirror/core';
4
+ import { useMemo, createContext, use, useRef, useEffect } from 'react';
5
+ import { jsxDEV, Fragment } from 'react/jsx-dev-runtime';
6
+ import { createRequestSubscription } from '@warp-drive-mirror/core/reactive';
7
+ import { DISPOSE, signal } from '@warp-drive-mirror/core/signals/-leaked';
8
+ import '@warp-drive-mirror/core/request';
9
+ var _jsxFileName$1 = "/home/runner/work/warp-drive/warp-drive/warp-drive-packages/react/src/-private/store-provider.tsx";
10
+ const StoreContext = /*#__PURE__*/createContext(null);
11
+
12
+ /**
13
+ * @category Hooks
14
+ */
15
+ function useStore() {
16
+ const store = use(StoreContext);
17
+ (test => {
18
+ if (!test) {
19
+ throw new Error("No Store provided via context. Please ensure you are using <StoreProvider> to provide a Store instance.");
20
+ }
21
+ })(store);
22
+ return store;
23
+ }
24
+ /**
25
+ * @category Components
26
+ */
27
+ function StoreProvider($props) {
28
+ const store = useMemo(() => "store" in $props ? $props.store : new $props.Store(), ["store" in $props ? $props.store : $props.Store]);
29
+ return /*#__PURE__*/jsxDEV(StoreContext, {
30
+ value: store,
31
+ children: $props.children
32
+ }, void 0, false, {
33
+ fileName: _jsxFileName$1,
34
+ lineNumber: 34,
35
+ columnNumber: 10
36
+ }, this);
37
+ }
38
+ const deferred = /* @__PURE__ */new WeakMap();
39
+ function deferDecorator(proto, prop, desc) {
40
+ let map = deferred.get(proto);
41
+ if (!map) {
42
+ map = /* @__PURE__ */new Map();
43
+ deferred.set(proto, map);
44
+ }
45
+ map.set(prop, desc);
46
+ }
47
+ function findDeferredDecorator(target, prop) {
48
+ var _a;
49
+ let cursor = target.prototype;
50
+ while (cursor) {
51
+ let desc = (_a = deferred.get(cursor)) == null ? void 0 : _a.get(prop);
52
+ if (desc) {
53
+ return desc;
54
+ }
55
+ cursor = cursor.prototype;
56
+ }
57
+ }
58
+ function decorateFieldV2(prototype, prop, decorators, initializer) {
59
+ let desc = {
60
+ configurable: true,
61
+ enumerable: true,
62
+ writable: true,
63
+ initializer: null
64
+ };
65
+ if (initializer) {
66
+ desc.initializer = initializer;
67
+ }
68
+ for (let decorator of decorators) {
69
+ desc = decorator(prototype, prop, desc) || desc;
70
+ }
71
+ if (desc.initializer === void 0) {
72
+ Object.defineProperty(prototype, prop, desc);
73
+ } else {
74
+ deferDecorator(prototype, prop, desc);
75
+ }
76
+ }
77
+ function initializeDeferredDecorator(target, prop) {
78
+ let desc = findDeferredDecorator(target.constructor, prop);
79
+ if (desc) {
80
+ Object.defineProperty(target, prop, {
81
+ enumerable: desc.enumerable,
82
+ configurable: desc.configurable,
83
+ writable: desc.writable,
84
+ value: desc.initializer ? desc.initializer.call(target) : void 0
85
+ });
86
+ }
87
+ }
88
+ var _jsxFileName = "/home/runner/work/warp-drive/warp-drive/warp-drive-packages/react/src/-private/request.tsx";
89
+ const IdleBlockMissingError = new Error("No idle block provided for <Request> component, and no query or request was provided.");
90
+ class ReactiveArgs {
91
+ static {
92
+ decorateFieldV2(this.prototype, "request", [signal]);
93
+ }
94
+ #request = (initializeDeferredDecorator(this, "request"), void 0);
95
+ static {
96
+ decorateFieldV2(this.prototype, "query", [signal]);
97
+ }
98
+ #query = (initializeDeferredDecorator(this, "query"), void 0);
99
+ static {
100
+ decorateFieldV2(this.prototype, "autorefresh", [signal]);
101
+ }
102
+ #autorefresh = (initializeDeferredDecorator(this, "autorefresh"), void 0);
103
+ static {
104
+ decorateFieldV2(this.prototype, "autorefreshThreshold", [signal]);
105
+ }
106
+ #autorefreshThreshold = (initializeDeferredDecorator(this, "autorefreshThreshold"), void 0);
107
+ static {
108
+ decorateFieldV2(this.prototype, "autorefreshBehavior", [signal]);
109
+ }
110
+ #autorefreshBehavior = (initializeDeferredDecorator(this, "autorefreshBehavior"), void 0);
111
+ }
112
+ const DefaultChrome = ({
113
+ children
114
+ }) => {
115
+ return /*#__PURE__*/jsxDEV(Fragment, {
116
+ children: children
117
+ }, void 0, false);
118
+ };
119
+ function Throw({
120
+ error
121
+ }) {
122
+ throw error;
123
+ }
124
+
125
+ /**
126
+ * The `<Request />` component is a powerful tool for managing data fetching and
127
+ * state in your React application. It provides a declarative approach to reactive
128
+ * control-flow for managing requests and state in your application.
129
+ *
130
+ * The `<Request />` component is ideal for handling "boundaries", outside which some
131
+ * state is still allowed to be unresolved and within which it MUST be resolved.
132
+ *
133
+ * ## Request States
134
+ *
135
+ * `<Request />` has five states, only one of which will be active and rendered at a time.
136
+ *
137
+ * - `idle`: The component is waiting to be given a request to monitor
138
+ * - `loading`: The request is in progress
139
+ * - `error`: The request failed
140
+ * - `content`: The request succeeded
141
+ * - `cancelled`: The request was cancelled
142
+ *
143
+ * Additionally, the `content` state has a `refresh` method that can be used to
144
+ * refresh the request in the background, which is available as a sub-state of
145
+ * the `content` state.
146
+ *
147
+ * ### Example Usage
148
+ *
149
+ * ```tsx
150
+ * import { Request } from "@warp-drive-mirror/react";
151
+ *
152
+ * export function UserPreview($props: { id: string | null }) {
153
+ * return (
154
+ * <Request
155
+ * query={findRecord('user', $props.id)}
156
+ * states={{
157
+ * idle: () => <div>Waiting for User Selection</div>,
158
+ * loading: ({ state }) => <div>Loading user data...</div>,
159
+ * cancelled: ({ error, features }) => (
160
+ * <div>
161
+ * <p>Request Cancelled</p>
162
+ * <p><button onClick={features.retry}>Start Again?</button></p>
163
+ * </div>
164
+ * ),
165
+ * error: ({ error, features }) => (
166
+ * <div>
167
+ * <p>Error: {error.message}</p>
168
+ * <p><button onClick={features.retry}>Try Again?</button></p>
169
+ * </div>
170
+ * ),
171
+ * content: ({ result, features }) => (
172
+ * <div>
173
+ * <h2>User Details</h2>
174
+ * <p>ID: {result.id}</p>
175
+ * <p>Name: {result.name}</p>
176
+ * </div>
177
+ * ),
178
+ * }}
179
+ * />
180
+ * );
181
+ * }
182
+ *
183
+ * ```
184
+ *
185
+ * @category Components
186
+ */
187
+ function Request($props) {
188
+ return /*#__PURE__*/jsxDEV(ReactiveContext, {
189
+ children: /*#__PURE__*/jsxDEV(InternalRequest, {
190
+ ...$props
191
+ }, void 0, false, {
192
+ fileName: _jsxFileName,
193
+ lineNumber: 174,
194
+ columnNumber: 7
195
+ }, this)
196
+ }, void 0, false, {
197
+ fileName: _jsxFileName,
198
+ lineNumber: 173,
199
+ columnNumber: 5
200
+ }, this);
201
+ }
202
+ function isStrictModeRender() {
203
+ const count = useRef(0);
204
+
205
+ // in debug we need to skip every second invocation
206
+ {
207
+ if (count.current++ % 2 === 1) {
208
+ return true;
209
+ }
210
+ }
211
+ return false;
212
+ }
213
+ function InternalRequest($props) {
214
+ const isStrict = isStrictModeRender();
215
+ const store = $props.store ?? useStore();
216
+ const Chrome = $props.chrome ?? DefaultChrome;
217
+ const sink = useRef(null);
218
+ const args = useRef(null);
219
+ if (!args.current) {
220
+ args.current = new ReactiveArgs();
221
+ }
222
+ Object.assign(args.current, $props);
223
+ if (sink.current && (sink.current.store !== store || $props.subscription)) {
224
+ sink.current[DISPOSE]();
225
+ sink.current = null;
226
+ }
227
+ if (!sink.current && !$props.subscription) {
228
+ sink.current = createRequestSubscription(store, args.current);
229
+ }
230
+ const initialized = useRef(null);
231
+ const effect = () => {
232
+ if (sink.current && (!initialized.current || initialized.current.disposable !== sink.current)) {
233
+ initialized.current = {
234
+ disposable: sink.current,
235
+ dispose: () => {
236
+ sink.current?.[DISPOSE]();
237
+ initialized.current = null;
238
+ sink.current = null;
239
+ }
240
+ };
241
+ }
242
+ return sink.current ? initialized.current.dispose : undefined;
243
+ };
244
+ let maybeEffect = effect;
245
+ {
246
+ if (isStrict) {
247
+ maybeEffect = () => {
248
+ if (initialized.current) {
249
+ return effect();
250
+ }
251
+ return () => {
252
+ // initialize our actual effect
253
+ effect();
254
+ // in strict mode we don't want to run the teardown
255
+ // for the second invocation
256
+ };
257
+ };
258
+ }
259
+ }
260
+ useEffect(maybeEffect, [sink.current]);
261
+ const state = $props.subscription ?? sink.current;
262
+ const slots = $props.states;
263
+ return /*#__PURE__*/jsxDEV(Chrome, {
264
+ state: state.isIdle ? null : state.reqState,
265
+ features: state.contentFeatures,
266
+ children:
267
+ // prettier-ignore
268
+ state.isIdle && slots.idle ? /*#__PURE__*/jsxDEV(slots.idle, {}, void 0, false, {
269
+ fileName: _jsxFileName,
270
+ lineNumber: 258,
271
+ columnNumber: 38
272
+ }, this) : state.isIdle ? /*#__PURE__*/jsxDEV(Throw, {
273
+ error: IdleBlockMissingError
274
+ }, void 0, false, {
275
+ fileName: _jsxFileName,
276
+ lineNumber: 259,
277
+ columnNumber: 28
278
+ }, this) : state.reqState.isLoading ? slots.loading ? /*#__PURE__*/jsxDEV(slots.loading, {
279
+ state: state.reqState.loadingState
280
+ }, void 0, false, {
281
+ fileName: _jsxFileName,
282
+ lineNumber: 260,
283
+ columnNumber: 56
284
+ }, this) : '' : state.reqState.isCancelled && slots.cancelled ? /*#__PURE__*/jsxDEV(slots.cancelled, {
285
+ error: state.reqState.reason,
286
+ features: state.errorFeatures
287
+ }, void 0, false, {
288
+ fileName: _jsxFileName,
289
+ lineNumber: 261,
290
+ columnNumber: 61
291
+ }, this) : state.reqState.isError && slots.error ? /*#__PURE__*/jsxDEV(slots.error, {
292
+ error: state.reqState.reason,
293
+ features: state.errorFeatures
294
+ }, void 0, false, {
295
+ fileName: _jsxFileName,
296
+ lineNumber: 262,
297
+ columnNumber: 53
298
+ }, this) : state.reqState.isSuccess ? slots.content ? /*#__PURE__*/jsxDEV(slots.content, {
299
+ result: state.reqState.value,
300
+ features: state.contentFeatures
301
+ }, void 0, false, {
302
+ fileName: _jsxFileName,
303
+ lineNumber: 263,
304
+ columnNumber: 56
305
+ }, this) : /*#__PURE__*/jsxDEV(Throw, {
306
+ error: new Error('No content block provided for <Request> component.')
307
+ }, void 0, false, {
308
+ fileName: _jsxFileName,
309
+ lineNumber: 263,
310
+ columnNumber: 139
311
+ }, this) : !state.reqState.isCancelled ? /*#__PURE__*/jsxDEV(Throw, {
312
+ error: state.reqState.reason
313
+ }, void 0, false, {
314
+ fileName: _jsxFileName,
315
+ lineNumber: 264,
316
+ columnNumber: 43
317
+ }, this) : '' // never
318
+ }, void 0, false, {
319
+ fileName: _jsxFileName,
320
+ lineNumber: 255,
321
+ columnNumber: 5
322
+ }, this);
323
+ }
324
+ export { ReactiveContext, Request, StoreProvider, Throw, useStore };
@@ -0,0 +1,102 @@
1
+ import { use } from 'react';
2
+ import { Signal } from 'signal-polyfill';
3
+ import { setupSignals } from '@warp-drive-mirror/core/configure';
4
+ import { g as getGlobalConfig, W as WatcherContext } from "./reactive-context-CTtwoaBx.js";
5
+
6
+ /**
7
+ * {@include ./install.md}
8
+ * @module
9
+ */
10
+
11
+ function tryConsumeContext(signal) {
12
+ // eslint-disable-next-line no-console
13
+ const logError = console.error;
14
+ try {
15
+ // eslint-disable-next-line no-console
16
+ console.error = () => {};
17
+ // ensure signals are watched by our closest watcher
18
+ const watcher = use(WatcherContext);
19
+ // eslint-disable-next-line no-console
20
+ console.error = logError;
21
+ watcher?.watcher.watch(signal);
22
+ {
23
+ if (getGlobalConfig().WarpDriveMirror.debug.LOG_REACT_SIGNAL_INTEGRATION || globalThis.getWarpDriveRuntimeConfig().debug.LOG_REACT_SIGNAL_INTEGRATION) {
24
+ // eslint-disable-next-line no-console
25
+ console.log(`[WarpDrive] Consumed Context Signal`, signal, watcher);
26
+ }
27
+ }
28
+ } catch {
29
+ // eslint-disable-next-line no-console
30
+ console.error = logError;
31
+ // if we are not in a React context, we will Error
32
+ // so we just ignore it.
33
+ {
34
+ if (getGlobalConfig().WarpDriveMirror.debug.LOG_REACT_SIGNAL_INTEGRATION || globalThis.getWarpDriveRuntimeConfig().debug.LOG_REACT_SIGNAL_INTEGRATION) {
35
+ // eslint-disable-next-line no-console
36
+ console.log(`[WarpDrive] No Context Available To Consume Signal`, signal);
37
+ }
38
+ }
39
+ }
40
+ }
41
+ let pending;
42
+ async function settled() {
43
+ {
44
+ // in testing mode we provide a test waiter integration
45
+ if (!pending || !pending.length) return;
46
+ const current = pending ?? [];
47
+ pending = [];
48
+ await Promise.allSettled(current);
49
+ await Promise.resolve();
50
+ await Promise.resolve();
51
+ await Promise.resolve();
52
+ return settled();
53
+ }
54
+ }
55
+ function buildSignalConfig(options) {
56
+ return {
57
+ createSignal: (obj, key) => new Signal.State({
58
+ obj,
59
+ key
60
+ }, {
61
+ equals: () => false
62
+ }),
63
+ notifySignal: signal => {
64
+ {
65
+ if (getGlobalConfig().WarpDriveMirror.debug.LOG_REACT_SIGNAL_INTEGRATION || globalThis.getWarpDriveRuntimeConfig().debug.LOG_REACT_SIGNAL_INTEGRATION) {
66
+ if (Signal.subtle.hasSinks(signal)) {
67
+ // eslint-disable-next-line no-console
68
+ console.log(`[WarpDrive] Notifying Signal`, signal);
69
+ } else {
70
+ // eslint-disable-next-line no-console
71
+ console.log(`[WarpDrive] Notified Signal That Has No Watcher`, signal);
72
+ }
73
+ }
74
+ }
75
+ signal.set(signal.get());
76
+ },
77
+ consumeSignal: signal => {
78
+ tryConsumeContext(signal);
79
+ void signal.get();
80
+ },
81
+ createMemo: (object, key, fn) => {
82
+ const memo = new Signal.Computed(fn);
83
+ return () => {
84
+ tryConsumeContext(memo);
85
+ return memo.get();
86
+ };
87
+ },
88
+ waitFor: promise => {
89
+ {
90
+ pending = pending || [];
91
+ const newPromise = promise.finally(() => {
92
+ pending = pending.filter(p => p !== newPromise);
93
+ });
94
+ pending.push(newPromise);
95
+ return newPromise;
96
+ }
97
+ },
98
+ willSyncFlushWatchers: () => false
99
+ };
100
+ }
101
+ setupSignals(buildSignalConfig);
102
+ export { buildSignalConfig, settled };