@effect-rx/rx-react 0.11.3 → 0.13.0

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/src/index.ts CHANGED
@@ -198,35 +198,32 @@ type SuspenseResult<E, A> =
198
198
  }
199
199
  | {
200
200
  readonly _tag: "Value"
201
- readonly isWaiting: boolean
202
201
  readonly value: Result.Success<E, A> | Result.Failure<E, A>
203
202
  }
204
203
 
205
204
  const suspenseRx = Rx.family((rx: Rx.Rx<Result.Result<any, any>>) =>
206
205
  Rx.readable((get): SuspenseResult<any, any> => {
207
206
  const result = get(rx)
208
- const value = Result.noWaiting(result)
209
- if (value._tag === "Initial") {
207
+ if (result._tag === "Initial") {
210
208
  return {
211
209
  _tag: "Suspended",
212
210
  promise: new Promise<void>((resolve) => get.addFinalizer(resolve))
213
211
  } as const
214
212
  }
215
- const isWaiting = Result.isWaiting(result)
216
- return { _tag: "Value", isWaiting, value } as const
213
+ return { _tag: "Value", value: result } as const
217
214
  })
218
215
  )
219
216
 
220
217
  const suspenseRxWaiting = Rx.family((rx: Rx.Rx<Result.Result<any, any>>) =>
221
218
  Rx.readable((get): SuspenseResult<any, any> => {
222
219
  const result = get(rx)
223
- if (result._tag === "Waiting" || result._tag === "Initial") {
220
+ if (result.waiting || result._tag === "Initial") {
224
221
  return {
225
222
  _tag: "Suspended",
226
223
  promise: new Promise<void>((resolve) => get.addFinalizer(resolve))
227
224
  } as const
228
225
  }
229
- return { _tag: "Value", isWaiting: false, value: result } as const
226
+ return { _tag: "Value", value: result } as const
230
227
  })
231
228
  )
232
229
 
@@ -239,10 +236,7 @@ const suspenseMounts = globalValue("@effect-rx/rx-react/suspenseMounts", () => n
239
236
  export const useRxSuspense = <E, A>(
240
237
  rx: Rx.Rx<Result.Result<E, A>>,
241
238
  options?: { readonly suspendOnWaiting?: boolean }
242
- ): {
243
- readonly isWaiting: boolean
244
- readonly value: Result.Success<E, A> | Result.Failure<E, A>
245
- } => {
239
+ ): Result.Success<E, A> | Result.Failure<E, A> => {
246
240
  const registry = React.useContext(RegistryContext)
247
241
  const resultRx = React.useMemo(
248
242
  () => (options?.suspendOnWaiting ? suspenseRxWaiting(rx) : suspenseRx(rx)),
@@ -263,7 +257,7 @@ export const useRxSuspense = <E, A>(
263
257
  throw result.promise
264
258
  }
265
259
 
266
- return result
260
+ return result.value
267
261
  }
268
262
 
269
263
  /**
@@ -273,18 +267,12 @@ export const useRxSuspense = <E, A>(
273
267
  export const useRxSuspenseSuccess = <E, A>(
274
268
  rx: Rx.Rx<Result.Result<E, A>>,
275
269
  options?: { readonly suspendOnWaiting?: boolean }
276
- ): {
277
- readonly isWaiting: boolean
278
- readonly value: A
279
- } => {
270
+ ): Result.Success<E, A> => {
280
271
  const result = useRxSuspense(rx, options)
281
- if (result.value._tag === "Failure") {
282
- throw Cause.squash(result.value.cause)
283
- }
284
- return {
285
- isWaiting: result.isWaiting,
286
- value: result.value.value
272
+ if (result._tag === "Failure") {
273
+ throw Cause.squash(result.cause)
287
274
  }
275
+ return result
288
276
  }
289
277
 
290
278
  /**
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"../../../src","sources":["index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,QAAQ,MAAM,wBAAwB,CAAA;AAClD,OAAO,KAAK,MAAM,MAAM,sBAAsB,CAAA;AAC9C,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAA;AACtC,OAAO,KAAK,KAAK,KAAK,MAAM,qBAAqB,CAAA;AAEjD,OAAO,KAAK,KAAK,IAAI,MAAM,aAAa,CAAA;AAExC,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAE9B;;;GAGG;AACH,OAAO,KAAK,QAAQ,MAAM,wBAAwB,CAAA;AAClD;;;GAGG;AACH,OAAO,KAAK,MAAM,MAAM,sBAAsB,CAAA;AAC9C;;;GAGG;AACH,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAA;AACtC;;;GAGG;AACH,OAAO,KAAK,KAAK,MAAM,qBAAqB,CAAA;AAE5C;;;GAGG;AACH,eAAO,MAAM,eAAe,kCAA0D,CAAA;AAmCtF;;;GAGG;AACH,eAAO,MAAM,UAAU,EAAE;IACvB,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;IACpB,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;CAQxC,CAAA;AAmDD;;;GAGG;AACH,eAAO,MAAM,UAAU,uBAAsB,IAG5C,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,QAAQ,6DAA4D,IAIhF,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,eAAe,0FAK3B,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,YAAY,wCAAuC,MAAM,IAMrE,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,KAAK,8FAE4C,IAAI,CAMjE,CAAA;AA2CD;;;GAGG;AACH,eAAO,MAAM,aAAa,mDAEd;IAAE,QAAQ,CAAC,gBAAgB,CAAC,EAAE,OAAO,CAAA;CAAE;wBAE7B,OAAO;;CAwB5B,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,oBAAoB,mDAErB;IAAE,QAAQ,CAAC,gBAAgB,CAAC,EAAE,OAAO,CAAA;CAAE;wBAE7B,OAAO;;CAW5B,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,cAAc,iCAEZ,IAAI,YACP;IAAE,QAAQ,CAAC,SAAS,CAAC,EAAE,OAAO,CAAA;CAAE,KACzC,IAMF,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,QAAQ,qCAIpB,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,YAAY,2EAExB,CAAA"}
@@ -1,2 +0,0 @@
1
- export * from "./declarations/src/index.js";
2
- //# sourceMappingURL=effect-rx-rx-react.cjs.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"effect-rx-rx-react.cjs.d.mts","sourceRoot":"","sources":["./declarations/src/index.d.ts"],"names":[],"mappings":"AAAA"}
@@ -1,2 +0,0 @@
1
- export * from "./declarations/src/index";
2
- //# sourceMappingURL=effect-rx-rx-react.cjs.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"effect-rx-rx-react.cjs.d.ts","sourceRoot":"","sources":["./declarations/src/index.d.ts"],"names":[],"mappings":"AAAA"}
@@ -1,285 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var Registry = require('@effect-rx/rx/Registry');
6
- var Result = require('@effect-rx/rx/Result');
7
- var Rx = require('@effect-rx/rx/Rx');
8
- var Cause = require('effect/Cause');
9
- var GlobalValue = require('effect/GlobalValue');
10
- var React = require('react');
11
- var RxRef = require('@effect-rx/rx/RxRef');
12
-
13
- function _interopNamespace(e) {
14
- if (e && e.__esModule) return e;
15
- var n = Object.create(null);
16
- if (e) {
17
- Object.keys(e).forEach(function (k) {
18
- if (k !== 'default') {
19
- var d = Object.getOwnPropertyDescriptor(e, k);
20
- Object.defineProperty(n, k, d.get ? d : {
21
- enumerable: true,
22
- get: function () { return e[k]; }
23
- });
24
- }
25
- });
26
- }
27
- n["default"] = e;
28
- return Object.freeze(n);
29
- }
30
-
31
- var Registry__namespace = /*#__PURE__*/_interopNamespace(Registry);
32
- var Result__namespace = /*#__PURE__*/_interopNamespace(Result);
33
- var Rx__namespace = /*#__PURE__*/_interopNamespace(Rx);
34
- var Cause__namespace = /*#__PURE__*/_interopNamespace(Cause);
35
- var React__namespace = /*#__PURE__*/_interopNamespace(React);
36
- var RxRef__namespace = /*#__PURE__*/_interopNamespace(RxRef);
37
-
38
- /**
39
- * @since 1.0.0
40
- */
41
-
42
- /**
43
- * @since 1.0.0
44
- * @category context
45
- */
46
- const RegistryContext = /*#__PURE__*/React__namespace.createContext( /*#__PURE__*/Registry__namespace.make());
47
- const storeRegistry = /*#__PURE__*/GlobalValue.globalValue("@effect-rx/rx-react/storeRegistry", () => new WeakMap());
48
- function makeStore(registry, rx) {
49
- const stores = storeRegistry.get(registry) ?? storeRegistry.set(registry, new WeakMap()).get(registry);
50
- const store = stores.get(rx);
51
- if (store) {
52
- return store;
53
- }
54
- const newStore = {
55
- subscribe(f) {
56
- return registry.subscribe(rx, f);
57
- },
58
- snapshot() {
59
- return registry.get(rx);
60
- }
61
- };
62
- stores.set(rx, newStore);
63
- return newStore;
64
- }
65
- function useStore(registry, rx) {
66
- const store = makeStore(registry, rx);
67
- return React__namespace.useSyncExternalStore(store.subscribe, store.snapshot);
68
- }
69
-
70
- /**
71
- * @since 1.0.0
72
- * @category hooks
73
- */
74
- const useRxValue = (rx, f) => {
75
- const registry = React__namespace.useContext(RegistryContext);
76
- if (f) {
77
- const rxB = React__namespace.useMemo(() => Rx__namespace.map(rx, f), [rx, f]);
78
- return useStore(registry, rxB);
79
- }
80
- return useStore(registry, rx);
81
- };
82
- function mountRx(registry, rx) {
83
- React__namespace.useEffect(() => registry.mount(rx), [rx, registry]);
84
- }
85
- function setRx(registry, rx) {
86
- return React__namespace.useCallback(value => {
87
- if (typeof value === "function") {
88
- registry.set(rx, value(registry.get(rx)));
89
- return;
90
- } else {
91
- registry.set(rx, value);
92
- }
93
- }, [registry, rx]);
94
- }
95
- function setRxPromise(registry, rx) {
96
- const cancelRef = React__namespace.useRef(undefined);
97
- if (!cancelRef.current) {
98
- cancelRef.current = new Set();
99
- }
100
- React__namespace.useEffect(() => () => {
101
- cancelRef.current.forEach(cancel => cancel());
102
- cancelRef.current.clear();
103
- }, []);
104
- return React__namespace.useCallback(value => {
105
- registry.set(rx, value);
106
- return new Promise(resolve => {
107
- const value = registry.get(rx);
108
- if (Result__namespace.isSuccess(value) || Result__namespace.isFailure(value)) {
109
- resolve(Result__namespace.toExit(value));
110
- } else {
111
- const cancel = registry.subscribe(rx, value => {
112
- if (Result__namespace.isSuccess(value) || Result__namespace.isFailure(value)) {
113
- cancelRef.current.delete(cancel);
114
- cancel();
115
- resolve(Result__namespace.toExit(value));
116
- }
117
- });
118
- cancelRef.current.add(cancel);
119
- }
120
- });
121
- }, [registry, rx]);
122
- }
123
-
124
- /**
125
- * @since 1.0.0
126
- * @category hooks
127
- */
128
- const useRxMount = rx => {
129
- const registry = React__namespace.useContext(RegistryContext);
130
- mountRx(registry, rx);
131
- };
132
-
133
- /**
134
- * @since 1.0.0
135
- * @category hooks
136
- */
137
- const useRxSet = rx => {
138
- const registry = React__namespace.useContext(RegistryContext);
139
- mountRx(registry, rx);
140
- return setRx(registry, rx);
141
- };
142
-
143
- /**
144
- * @since 1.0.0
145
- * @category hooks
146
- */
147
- const useRxSetPromise = rx => {
148
- const registry = React__namespace.useContext(RegistryContext);
149
- return setRxPromise(registry, rx);
150
- };
151
-
152
- /**
153
- * @since 1.0.0
154
- * @category hooks
155
- */
156
- const useRxRefresh = rx => {
157
- const registry = React__namespace.useContext(RegistryContext);
158
- mountRx(registry, rx);
159
- return React__namespace.useCallback(() => {
160
- registry.refresh(rx);
161
- }, [registry, rx]);
162
- };
163
-
164
- /**
165
- * @since 1.0.0
166
- * @category hooks
167
- */
168
- const useRx = rx => {
169
- const registry = React__namespace.useContext(RegistryContext);
170
- return [useStore(registry, rx), setRx(registry, rx)];
171
- };
172
- const suspenseRx = /*#__PURE__*/Rx__namespace.family(rx => Rx__namespace.readable(get => {
173
- const result = get(rx);
174
- const value = Result__namespace.noWaiting(result);
175
- if (value._tag === "Initial") {
176
- return {
177
- _tag: "Suspended",
178
- promise: new Promise(resolve => get.addFinalizer(resolve))
179
- };
180
- }
181
- const isWaiting = Result__namespace.isWaiting(result);
182
- return {
183
- _tag: "Value",
184
- isWaiting,
185
- value
186
- };
187
- }));
188
- const suspenseRxWaiting = /*#__PURE__*/Rx__namespace.family(rx => Rx__namespace.readable(get => {
189
- const result = get(rx);
190
- if (result._tag === "Waiting" || result._tag === "Initial") {
191
- return {
192
- _tag: "Suspended",
193
- promise: new Promise(resolve => get.addFinalizer(resolve))
194
- };
195
- }
196
- return {
197
- _tag: "Value",
198
- isWaiting: false,
199
- value: result
200
- };
201
- }));
202
- const suspenseMounts = /*#__PURE__*/GlobalValue.globalValue("@effect-rx/rx-react/suspenseMounts", () => new Set());
203
-
204
- /**
205
- * @since 1.0.0
206
- * @category hooks
207
- */
208
- const useRxSuspense = (rx, options) => {
209
- const registry = React__namespace.useContext(RegistryContext);
210
- const resultRx = React__namespace.useMemo(() => options?.suspendOnWaiting ? suspenseRxWaiting(rx) : suspenseRx(rx), [options?.suspendOnWaiting, rx]);
211
- const result = useStore(registry, resultRx);
212
- if (result._tag === "Suspended") {
213
- if (!suspenseMounts.has(resultRx)) {
214
- suspenseMounts.add(resultRx);
215
- const unmount = registry.mount(resultRx);
216
- result.promise.then(function () {
217
- setTimeout(function () {
218
- unmount();
219
- suspenseMounts.delete(resultRx);
220
- }, 1000);
221
- });
222
- }
223
- throw result.promise;
224
- }
225
- return result;
226
- };
227
-
228
- /**
229
- * @since 1.0.0
230
- * @category hooks
231
- */
232
- const useRxSuspenseSuccess = (rx, options) => {
233
- const result = useRxSuspense(rx, options);
234
- if (result.value._tag === "Failure") {
235
- throw Cause__namespace.squash(result.value.cause);
236
- }
237
- return {
238
- isWaiting: result.isWaiting,
239
- value: result.value.value
240
- };
241
- };
242
-
243
- /**
244
- * @since 1.0.0
245
- * @category hooks
246
- */
247
- const useRxSubscribe = (rx, f, options) => {
248
- const registry = React__namespace.useContext(RegistryContext);
249
- React__namespace.useEffect(() => registry.subscribe(rx, f, options), [registry, rx, f, options?.immediate]);
250
- };
251
-
252
- /**
253
- * @since 1.0.0
254
- * @category hooks
255
- */
256
- const useRxRef = ref => {
257
- const [value, setValue] = React__namespace.useState(ref.value);
258
- React__namespace.useEffect(() => ref.subscribe(setValue), [ref]);
259
- return value;
260
- };
261
-
262
- /**
263
- * @since 1.0.0
264
- * @category hooks
265
- */
266
- const useRxRefProp = (ref, prop) => {
267
- return React__namespace.useMemo(() => ref.prop(prop), [ref, prop]);
268
- };
269
-
270
- exports.Registry = Registry__namespace;
271
- exports.Result = Result__namespace;
272
- exports.Rx = Rx__namespace;
273
- exports.RxRef = RxRef__namespace;
274
- exports.RegistryContext = RegistryContext;
275
- exports.useRx = useRx;
276
- exports.useRxMount = useRxMount;
277
- exports.useRxRef = useRxRef;
278
- exports.useRxRefProp = useRxRefProp;
279
- exports.useRxRefresh = useRxRefresh;
280
- exports.useRxSet = useRxSet;
281
- exports.useRxSetPromise = useRxSetPromise;
282
- exports.useRxSubscribe = useRxSubscribe;
283
- exports.useRxSuspense = useRxSuspense;
284
- exports.useRxSuspenseSuccess = useRxSuspenseSuccess;
285
- exports.useRxValue = useRxValue;
@@ -1,7 +0,0 @@
1
- 'use strict';
2
-
3
- if (process.env.NODE_ENV === "production") {
4
- module.exports = require("./effect-rx-rx-react.cjs.prod.js");
5
- } else {
6
- module.exports = require("./effect-rx-rx-react.cjs.dev.js");
7
- }
@@ -1,18 +0,0 @@
1
- export {
2
- Registry,
3
- RegistryContext,
4
- Result,
5
- Rx,
6
- RxRef,
7
- useRx,
8
- useRxMount,
9
- useRxRef,
10
- useRxRefProp,
11
- useRxRefresh,
12
- useRxSet,
13
- useRxSetPromise,
14
- useRxSubscribe,
15
- useRxSuspense,
16
- useRxSuspenseSuccess,
17
- useRxValue
18
- } from "./effect-rx-rx-react.cjs.js";
@@ -1,285 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var Registry = require('@effect-rx/rx/Registry');
6
- var Result = require('@effect-rx/rx/Result');
7
- var Rx = require('@effect-rx/rx/Rx');
8
- var Cause = require('effect/Cause');
9
- var GlobalValue = require('effect/GlobalValue');
10
- var React = require('react');
11
- var RxRef = require('@effect-rx/rx/RxRef');
12
-
13
- function _interopNamespace(e) {
14
- if (e && e.__esModule) return e;
15
- var n = Object.create(null);
16
- if (e) {
17
- Object.keys(e).forEach(function (k) {
18
- if (k !== 'default') {
19
- var d = Object.getOwnPropertyDescriptor(e, k);
20
- Object.defineProperty(n, k, d.get ? d : {
21
- enumerable: true,
22
- get: function () { return e[k]; }
23
- });
24
- }
25
- });
26
- }
27
- n["default"] = e;
28
- return Object.freeze(n);
29
- }
30
-
31
- var Registry__namespace = /*#__PURE__*/_interopNamespace(Registry);
32
- var Result__namespace = /*#__PURE__*/_interopNamespace(Result);
33
- var Rx__namespace = /*#__PURE__*/_interopNamespace(Rx);
34
- var Cause__namespace = /*#__PURE__*/_interopNamespace(Cause);
35
- var React__namespace = /*#__PURE__*/_interopNamespace(React);
36
- var RxRef__namespace = /*#__PURE__*/_interopNamespace(RxRef);
37
-
38
- /**
39
- * @since 1.0.0
40
- */
41
-
42
- /**
43
- * @since 1.0.0
44
- * @category context
45
- */
46
- const RegistryContext = /*#__PURE__*/React__namespace.createContext( /*#__PURE__*/Registry__namespace.make());
47
- const storeRegistry = /*#__PURE__*/GlobalValue.globalValue("@effect-rx/rx-react/storeRegistry", () => new WeakMap());
48
- function makeStore(registry, rx) {
49
- const stores = storeRegistry.get(registry) ?? storeRegistry.set(registry, new WeakMap()).get(registry);
50
- const store = stores.get(rx);
51
- if (store) {
52
- return store;
53
- }
54
- const newStore = {
55
- subscribe(f) {
56
- return registry.subscribe(rx, f);
57
- },
58
- snapshot() {
59
- return registry.get(rx);
60
- }
61
- };
62
- stores.set(rx, newStore);
63
- return newStore;
64
- }
65
- function useStore(registry, rx) {
66
- const store = makeStore(registry, rx);
67
- return React__namespace.useSyncExternalStore(store.subscribe, store.snapshot);
68
- }
69
-
70
- /**
71
- * @since 1.0.0
72
- * @category hooks
73
- */
74
- const useRxValue = (rx, f) => {
75
- const registry = React__namespace.useContext(RegistryContext);
76
- if (f) {
77
- const rxB = React__namespace.useMemo(() => Rx__namespace.map(rx, f), [rx, f]);
78
- return useStore(registry, rxB);
79
- }
80
- return useStore(registry, rx);
81
- };
82
- function mountRx(registry, rx) {
83
- React__namespace.useEffect(() => registry.mount(rx), [rx, registry]);
84
- }
85
- function setRx(registry, rx) {
86
- return React__namespace.useCallback(value => {
87
- if (typeof value === "function") {
88
- registry.set(rx, value(registry.get(rx)));
89
- return;
90
- } else {
91
- registry.set(rx, value);
92
- }
93
- }, [registry, rx]);
94
- }
95
- function setRxPromise(registry, rx) {
96
- const cancelRef = React__namespace.useRef(undefined);
97
- if (!cancelRef.current) {
98
- cancelRef.current = new Set();
99
- }
100
- React__namespace.useEffect(() => () => {
101
- cancelRef.current.forEach(cancel => cancel());
102
- cancelRef.current.clear();
103
- }, []);
104
- return React__namespace.useCallback(value => {
105
- registry.set(rx, value);
106
- return new Promise(resolve => {
107
- const value = registry.get(rx);
108
- if (Result__namespace.isSuccess(value) || Result__namespace.isFailure(value)) {
109
- resolve(Result__namespace.toExit(value));
110
- } else {
111
- const cancel = registry.subscribe(rx, value => {
112
- if (Result__namespace.isSuccess(value) || Result__namespace.isFailure(value)) {
113
- cancelRef.current.delete(cancel);
114
- cancel();
115
- resolve(Result__namespace.toExit(value));
116
- }
117
- });
118
- cancelRef.current.add(cancel);
119
- }
120
- });
121
- }, [registry, rx]);
122
- }
123
-
124
- /**
125
- * @since 1.0.0
126
- * @category hooks
127
- */
128
- const useRxMount = rx => {
129
- const registry = React__namespace.useContext(RegistryContext);
130
- mountRx(registry, rx);
131
- };
132
-
133
- /**
134
- * @since 1.0.0
135
- * @category hooks
136
- */
137
- const useRxSet = rx => {
138
- const registry = React__namespace.useContext(RegistryContext);
139
- mountRx(registry, rx);
140
- return setRx(registry, rx);
141
- };
142
-
143
- /**
144
- * @since 1.0.0
145
- * @category hooks
146
- */
147
- const useRxSetPromise = rx => {
148
- const registry = React__namespace.useContext(RegistryContext);
149
- return setRxPromise(registry, rx);
150
- };
151
-
152
- /**
153
- * @since 1.0.0
154
- * @category hooks
155
- */
156
- const useRxRefresh = rx => {
157
- const registry = React__namespace.useContext(RegistryContext);
158
- mountRx(registry, rx);
159
- return React__namespace.useCallback(() => {
160
- registry.refresh(rx);
161
- }, [registry, rx]);
162
- };
163
-
164
- /**
165
- * @since 1.0.0
166
- * @category hooks
167
- */
168
- const useRx = rx => {
169
- const registry = React__namespace.useContext(RegistryContext);
170
- return [useStore(registry, rx), setRx(registry, rx)];
171
- };
172
- const suspenseRx = /*#__PURE__*/Rx__namespace.family(rx => Rx__namespace.readable(get => {
173
- const result = get(rx);
174
- const value = Result__namespace.noWaiting(result);
175
- if (value._tag === "Initial") {
176
- return {
177
- _tag: "Suspended",
178
- promise: new Promise(resolve => get.addFinalizer(resolve))
179
- };
180
- }
181
- const isWaiting = Result__namespace.isWaiting(result);
182
- return {
183
- _tag: "Value",
184
- isWaiting,
185
- value
186
- };
187
- }));
188
- const suspenseRxWaiting = /*#__PURE__*/Rx__namespace.family(rx => Rx__namespace.readable(get => {
189
- const result = get(rx);
190
- if (result._tag === "Waiting" || result._tag === "Initial") {
191
- return {
192
- _tag: "Suspended",
193
- promise: new Promise(resolve => get.addFinalizer(resolve))
194
- };
195
- }
196
- return {
197
- _tag: "Value",
198
- isWaiting: false,
199
- value: result
200
- };
201
- }));
202
- const suspenseMounts = /*#__PURE__*/GlobalValue.globalValue("@effect-rx/rx-react/suspenseMounts", () => new Set());
203
-
204
- /**
205
- * @since 1.0.0
206
- * @category hooks
207
- */
208
- const useRxSuspense = (rx, options) => {
209
- const registry = React__namespace.useContext(RegistryContext);
210
- const resultRx = React__namespace.useMemo(() => options?.suspendOnWaiting ? suspenseRxWaiting(rx) : suspenseRx(rx), [options?.suspendOnWaiting, rx]);
211
- const result = useStore(registry, resultRx);
212
- if (result._tag === "Suspended") {
213
- if (!suspenseMounts.has(resultRx)) {
214
- suspenseMounts.add(resultRx);
215
- const unmount = registry.mount(resultRx);
216
- result.promise.then(function () {
217
- setTimeout(function () {
218
- unmount();
219
- suspenseMounts.delete(resultRx);
220
- }, 1000);
221
- });
222
- }
223
- throw result.promise;
224
- }
225
- return result;
226
- };
227
-
228
- /**
229
- * @since 1.0.0
230
- * @category hooks
231
- */
232
- const useRxSuspenseSuccess = (rx, options) => {
233
- const result = useRxSuspense(rx, options);
234
- if (result.value._tag === "Failure") {
235
- throw Cause__namespace.squash(result.value.cause);
236
- }
237
- return {
238
- isWaiting: result.isWaiting,
239
- value: result.value.value
240
- };
241
- };
242
-
243
- /**
244
- * @since 1.0.0
245
- * @category hooks
246
- */
247
- const useRxSubscribe = (rx, f, options) => {
248
- const registry = React__namespace.useContext(RegistryContext);
249
- React__namespace.useEffect(() => registry.subscribe(rx, f, options), [registry, rx, f, options?.immediate]);
250
- };
251
-
252
- /**
253
- * @since 1.0.0
254
- * @category hooks
255
- */
256
- const useRxRef = ref => {
257
- const [value, setValue] = React__namespace.useState(ref.value);
258
- React__namespace.useEffect(() => ref.subscribe(setValue), [ref]);
259
- return value;
260
- };
261
-
262
- /**
263
- * @since 1.0.0
264
- * @category hooks
265
- */
266
- const useRxRefProp = (ref, prop) => {
267
- return React__namespace.useMemo(() => ref.prop(prop), [ref, prop]);
268
- };
269
-
270
- exports.Registry = Registry__namespace;
271
- exports.Result = Result__namespace;
272
- exports.Rx = Rx__namespace;
273
- exports.RxRef = RxRef__namespace;
274
- exports.RegistryContext = RegistryContext;
275
- exports.useRx = useRx;
276
- exports.useRxMount = useRxMount;
277
- exports.useRxRef = useRxRef;
278
- exports.useRxRefProp = useRxRefProp;
279
- exports.useRxRefresh = useRxRefresh;
280
- exports.useRxSet = useRxSet;
281
- exports.useRxSetPromise = useRxSetPromise;
282
- exports.useRxSubscribe = useRxSubscribe;
283
- exports.useRxSuspense = useRxSuspense;
284
- exports.useRxSuspenseSuccess = useRxSuspenseSuccess;
285
- exports.useRxValue = useRxValue;