@effect-rx/rx-react 0.1.3 → 0.1.5
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/index.d.ts +3 -8
- package/index.d.ts.map +1 -1
- package/index.js +32 -30
- package/index.js.map +1 -1
- package/mjs/index.mjs +30 -27
- package/mjs/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +34 -34
package/index.d.ts
CHANGED
|
@@ -21,20 +21,15 @@ export declare const useRxValue: <A>(rx: Rx.Rx<A>) => A;
|
|
|
21
21
|
* @since 1.0.0
|
|
22
22
|
* @category hooks
|
|
23
23
|
*/
|
|
24
|
-
export declare const useSetRx: <R, W>(rx: Rx.Writeable<R, W>) => (_: W) => void;
|
|
24
|
+
export declare const useSetRx: <R, W>(rx: Rx.Writeable<R, W>) => (_: W | ((_: R) => W)) => void;
|
|
25
25
|
/**
|
|
26
26
|
* @since 1.0.0
|
|
27
27
|
* @category hooks
|
|
28
28
|
*/
|
|
29
|
-
export declare const
|
|
29
|
+
export declare const useRefreshRx: <A>(rx: Rx.Rx<A> & Rx.Refreshable) => () => void;
|
|
30
30
|
/**
|
|
31
31
|
* @since 1.0.0
|
|
32
32
|
* @category hooks
|
|
33
33
|
*/
|
|
34
|
-
export declare const useRx: <R, W>(rx: Rx.Writeable<R, W>) => readonly [R, (_: W) => void];
|
|
35
|
-
/**
|
|
36
|
-
* @since 1.0.0
|
|
37
|
-
* @category hooks
|
|
38
|
-
*/
|
|
39
|
-
export declare const useRxUpdate: <R, W>(rx: Rx.Writeable<R, W>) => readonly [R, (f: (_: R) => W) => void];
|
|
34
|
+
export declare const useRx: <R, W>(rx: Rx.Writeable<R, W>) => readonly [value: R, setOrUpdate: (_: W | ((_: R) => W)) => void];
|
|
40
35
|
//# sourceMappingURL=index.d.ts.map
|
package/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["./src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,QAAQ,MAAM,wBAAwB,CAAA;AAClD,OAAO,KAAK,KAAK,EAAE,MAAM,kBAAkB,CAAA;AAC3C,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAE9B,OAAO,KAAK,QAAQ,MAAM,wBAAwB,CAAA;AAClD,OAAO,KAAK,MAAM,MAAM,sBAAsB,CAAA;AAC9C,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAA;AAEtC;;;GAGG;AACH,eAAO,MAAM,eAAe,kCAA0D,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["./src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,QAAQ,MAAM,wBAAwB,CAAA;AAClD,OAAO,KAAK,KAAK,EAAE,MAAM,kBAAkB,CAAA;AAC3C,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAE9B,OAAO,KAAK,QAAQ,MAAM,wBAAwB,CAAA;AAClD,OAAO,KAAK,MAAM,MAAM,sBAAsB,CAAA;AAC9C,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAA;AAEtC;;;GAGG;AACH,eAAO,MAAM,eAAe,kCAA0D,CAAA;AAwBtF;;;GAGG;AACH,eAAO,MAAM,UAAU,wBAOtB,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,QAAQ,8DAA6D,IAUjF,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,YAAY,wCAAuC,MAAM,IAKrE,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,KAAK,+FAA8F,IAAI,CAIxG,CAAA"}
|
package/index.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.
|
|
6
|
+
exports.useSetRx = exports.useRxValue = exports.useRx = exports.useRefreshRx = exports.Rx = exports.Result = exports.RegistryContext = exports.Registry = void 0;
|
|
7
7
|
var Registry = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect-rx/rx/Registry"));
|
|
8
8
|
var Registry_1 = Registry;
|
|
9
9
|
exports.Registry = Registry;
|
|
@@ -24,25 +24,24 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
|
|
|
24
24
|
*/
|
|
25
25
|
const RegistryContext = /*#__PURE__*/React.createContext( /*#__PURE__*/Registry.make());
|
|
26
26
|
exports.RegistryContext = RegistryContext;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
this.value = this.registry.get(this.rx);
|
|
36
|
-
} else {
|
|
37
|
-
this.init = true;
|
|
38
|
-
}
|
|
39
|
-
return this.registry.subscribe(this.rx, a => {
|
|
40
|
-
this.value = a;
|
|
41
|
-
f();
|
|
42
|
-
});
|
|
43
|
-
};
|
|
44
|
-
this.snapshot = () => this.value;
|
|
27
|
+
function makeStore(registry, rx) {
|
|
28
|
+
let getter = function () {
|
|
29
|
+
return registry.get(rx);
|
|
30
|
+
};
|
|
31
|
+
function subscribe(f) {
|
|
32
|
+
const [get, unmount] = registry.subscribeGetter(rx, f);
|
|
33
|
+
getter = get;
|
|
34
|
+
return unmount;
|
|
45
35
|
}
|
|
36
|
+
function snapshot() {
|
|
37
|
+
return getter();
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
rx,
|
|
41
|
+
registry,
|
|
42
|
+
subscribe,
|
|
43
|
+
snapshot
|
|
44
|
+
};
|
|
46
45
|
}
|
|
47
46
|
/**
|
|
48
47
|
* @since 1.0.0
|
|
@@ -52,7 +51,7 @@ const useRxValue = rx => {
|
|
|
52
51
|
const registry = React.useContext(RegistryContext);
|
|
53
52
|
const store = React.useRef(undefined);
|
|
54
53
|
if (store.current?.rx !== rx || store.current?.registry !== registry) {
|
|
55
|
-
store.current =
|
|
54
|
+
store.current = makeStore(registry, rx);
|
|
56
55
|
}
|
|
57
56
|
return React.useSyncExternalStore(store.current.subscribe, store.current.snapshot);
|
|
58
57
|
};
|
|
@@ -63,28 +62,31 @@ const useRxValue = rx => {
|
|
|
63
62
|
exports.useRxValue = useRxValue;
|
|
64
63
|
const useSetRx = rx => {
|
|
65
64
|
const registry = React.useContext(RegistryContext);
|
|
66
|
-
return React.useCallback(value =>
|
|
65
|
+
return React.useCallback(value => {
|
|
66
|
+
if (typeof value === "function") {
|
|
67
|
+
registry.set(rx, value(registry.get(rx)));
|
|
68
|
+
return;
|
|
69
|
+
} else {
|
|
70
|
+
registry.set(rx, value);
|
|
71
|
+
}
|
|
72
|
+
}, [registry, rx]);
|
|
67
73
|
};
|
|
68
74
|
/**
|
|
69
75
|
* @since 1.0.0
|
|
70
76
|
* @category hooks
|
|
71
77
|
*/
|
|
72
78
|
exports.useSetRx = useSetRx;
|
|
73
|
-
const
|
|
79
|
+
const useRefreshRx = rx => {
|
|
74
80
|
const registry = React.useContext(RegistryContext);
|
|
75
|
-
return React.useCallback(
|
|
81
|
+
return React.useCallback(() => {
|
|
82
|
+
registry.refresh(rx);
|
|
83
|
+
}, [registry, rx]);
|
|
76
84
|
};
|
|
77
85
|
/**
|
|
78
86
|
* @since 1.0.0
|
|
79
87
|
* @category hooks
|
|
80
88
|
*/
|
|
81
|
-
exports.
|
|
89
|
+
exports.useRefreshRx = useRefreshRx;
|
|
82
90
|
const useRx = rx => [useRxValue(rx), useSetRx(rx)];
|
|
83
|
-
/**
|
|
84
|
-
* @since 1.0.0
|
|
85
|
-
* @category hooks
|
|
86
|
-
*/
|
|
87
91
|
exports.useRx = useRx;
|
|
88
|
-
const useRxUpdate = rx => [useRxValue(rx), useUpdateRx(rx)];
|
|
89
|
-
exports.useRxUpdate = useRxUpdate;
|
|
90
92
|
//# sourceMappingURL=index.js.map
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["Registry","_interopRequireWildcard","require","Registry_1","exports","React","Result","Result_1","Rx","Rx_1","_getRequireWildcardCache","nodeInterop","WeakMap","cacheBabelInterop","cacheNodeInterop","obj","__esModule","default","cache","has","get","newObj","hasPropertyDescriptor","Object","defineProperty","getOwnPropertyDescriptor","key","prototype","hasOwnProperty","call","desc","set","RegistryContext","createContext","make","
|
|
1
|
+
{"version":3,"file":"index.js","names":["Registry","_interopRequireWildcard","require","Registry_1","exports","React","Result","Result_1","Rx","Rx_1","_getRequireWildcardCache","nodeInterop","WeakMap","cacheBabelInterop","cacheNodeInterop","obj","__esModule","default","cache","has","get","newObj","hasPropertyDescriptor","Object","defineProperty","getOwnPropertyDescriptor","key","prototype","hasOwnProperty","call","desc","set","RegistryContext","createContext","make","makeStore","registry","rx","getter","subscribe","f","unmount","subscribeGetter","snapshot","useRxValue","useContext","store","useRef","undefined","current","useSyncExternalStore","useSetRx","useCallback","value","useRefreshRx","refresh","useRx"],"sources":["./src/index.ts"],"sourcesContent":[null],"mappings":";;;;;;AAGA,IAAAA,QAAA,gBAAAC,uBAAA,eAAAC,OAAA;AAAkD,IAAAC,UAAA,GAAAH,QAAA;AAAAI,OAAA,CAAAJ,QAAA,GAAAA,QAAA;AAElD,IAAAK,KAAA,gBAAAJ,uBAAA,eAAAC,OAAA;;AAG8CE,OAAA,CAAAE,MAAA,GAAAC,QAAA;;AACRH,OAAA,CAAAI,EAAA,GAAAC,IAAA;AAAA,SAAAC,yBAAAC,WAAA,eAAAC,OAAA,kCAAAC,iBAAA,OAAAD,OAAA,QAAAE,gBAAA,OAAAF,OAAA,YAAAF,wBAAA,YAAAA,CAAAC,WAAA,WAAAA,WAAA,GAAAG,gBAAA,GAAAD,iBAAA,KAAAF,WAAA;AAAA,SAAAV,wBAAAc,GAAA,EAAAJ,WAAA,SAAAA,WAAA,IAAAI,GAAA,IAAAA,GAAA,CAAAC,UAAA,WAAAD,GAAA,QAAAA,GAAA,oBAAAA,GAAA,wBAAAA,GAAA,4BAAAE,OAA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,EAA6B,IAAgB;EAC3E,MAAMD,QAAQ,GAAG/B,KAAK,CAACwC,UAAU,CAACb,eAAe,CAAC;EAClD,OAAO3B,KAAK,CAAC+C,WAAW,CAAC,MAAK;IAC5BhB,QAAQ,CAACmB,OAAO,CAAClB,EAAE,CAAC;EACtB,CAAC,EAAE,CAACD,QAAQ,EAAEC,EAAE,CAAC,CAAC;AACpB,CAAC;AAED;;;;AAAAjC,OAAA,CAAAkD,YAAA,GAAAA,YAAA;AAIO,MAAME,KAAK,GAAUnB,EAAsB,IAChD,CACEO,UAAU,CAACP,EAAE,CAAC,EACdc,QAAQ,CAACd,EAAE,CAAC,CACJ;AAAAjC,OAAA,CAAAoD,KAAA,GAAAA,KAAA"}
|
package/mjs/index.mjs
CHANGED
|
@@ -14,25 +14,24 @@ export { Rx_1 as Rx };
|
|
|
14
14
|
* @category context
|
|
15
15
|
*/
|
|
16
16
|
export const RegistryContext = /*#__PURE__*/React.createContext( /*#__PURE__*/Registry.make());
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
this.value = this.registry.get(this.rx);
|
|
26
|
-
} else {
|
|
27
|
-
this.init = true;
|
|
28
|
-
}
|
|
29
|
-
return this.registry.subscribe(this.rx, a => {
|
|
30
|
-
this.value = a;
|
|
31
|
-
f();
|
|
32
|
-
});
|
|
33
|
-
};
|
|
34
|
-
this.snapshot = () => this.value;
|
|
17
|
+
function makeStore(registry, rx) {
|
|
18
|
+
let getter = function () {
|
|
19
|
+
return registry.get(rx);
|
|
20
|
+
};
|
|
21
|
+
function subscribe(f) {
|
|
22
|
+
const [get, unmount] = registry.subscribeGetter(rx, f);
|
|
23
|
+
getter = get;
|
|
24
|
+
return unmount;
|
|
35
25
|
}
|
|
26
|
+
function snapshot() {
|
|
27
|
+
return getter();
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
rx,
|
|
31
|
+
registry,
|
|
32
|
+
subscribe,
|
|
33
|
+
snapshot
|
|
34
|
+
};
|
|
36
35
|
}
|
|
37
36
|
/**
|
|
38
37
|
* @since 1.0.0
|
|
@@ -42,7 +41,7 @@ export const useRxValue = rx => {
|
|
|
42
41
|
const registry = React.useContext(RegistryContext);
|
|
43
42
|
const store = React.useRef(undefined);
|
|
44
43
|
if (store.current?.rx !== rx || store.current?.registry !== registry) {
|
|
45
|
-
store.current =
|
|
44
|
+
store.current = makeStore(registry, rx);
|
|
46
45
|
}
|
|
47
46
|
return React.useSyncExternalStore(store.current.subscribe, store.current.snapshot);
|
|
48
47
|
};
|
|
@@ -52,24 +51,28 @@ export const useRxValue = rx => {
|
|
|
52
51
|
*/
|
|
53
52
|
export const useSetRx = rx => {
|
|
54
53
|
const registry = React.useContext(RegistryContext);
|
|
55
|
-
return React.useCallback(value =>
|
|
54
|
+
return React.useCallback(value => {
|
|
55
|
+
if (typeof value === "function") {
|
|
56
|
+
registry.set(rx, value(registry.get(rx)));
|
|
57
|
+
return;
|
|
58
|
+
} else {
|
|
59
|
+
registry.set(rx, value);
|
|
60
|
+
}
|
|
61
|
+
}, [registry, rx]);
|
|
56
62
|
};
|
|
57
63
|
/**
|
|
58
64
|
* @since 1.0.0
|
|
59
65
|
* @category hooks
|
|
60
66
|
*/
|
|
61
|
-
export const
|
|
67
|
+
export const useRefreshRx = rx => {
|
|
62
68
|
const registry = React.useContext(RegistryContext);
|
|
63
|
-
return React.useCallback(
|
|
69
|
+
return React.useCallback(() => {
|
|
70
|
+
registry.refresh(rx);
|
|
71
|
+
}, [registry, rx]);
|
|
64
72
|
};
|
|
65
73
|
/**
|
|
66
74
|
* @since 1.0.0
|
|
67
75
|
* @category hooks
|
|
68
76
|
*/
|
|
69
77
|
export const useRx = rx => [useRxValue(rx), useSetRx(rx)];
|
|
70
|
-
/**
|
|
71
|
-
* @since 1.0.0
|
|
72
|
-
* @category hooks
|
|
73
|
-
*/
|
|
74
|
-
export const useRxUpdate = rx => [useRxValue(rx), useUpdateRx(rx)];
|
|
75
78
|
//# sourceMappingURL=index.mjs.map
|
package/mjs/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["Registry","React","Result","Rx","RegistryContext","createContext","make","
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["Registry","React","Result","Rx","RegistryContext","createContext","make","makeStore","registry","rx","getter","get","subscribe","f","unmount","subscribeGetter","snapshot","useRxValue","useContext","store","useRef","undefined","current","useSyncExternalStore","useSetRx","useCallback","value","set","useRefreshRx","refresh","useRx"],"sources":["../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA;;;AAGA,OAAO,KAAKA,QAAQ,MAAM,wBAAwB;AAElD,OAAO,KAAKC,KAAK,MAAM,OAAO;4BAEJ,wBAAwB;uBAAtCD,QAAQ;0BACI,sBAAsB;qBAAlCE,MAAM;sBACE,kBAAkB;iBAA1BC,EAAE;AAEd;;;;AAIA,OAAO,MAAMC,eAAe,gBAAGH,KAAK,CAACI,aAAa,eAAoBL,QAAQ,CAACM,IAAI,EAAE,CAAC;AAStF,SAASC,SAASA,CAAIC,QAA2B,EAAEC,EAAY;EAC7D,IAAIC,MAAM,GAAG,SAAAA,CAAA;IACX,OAAOF,QAAQ,CAACG,GAAG,CAACF,EAAE,CAAC;EACzB,CAAC;EACD,SAASG,SAASA,CAACC,CAAa;IAC9B,MAAM,CAACF,GAAG,EAAEG,OAAO,CAAC,GAAGN,QAAQ,CAACO,eAAe,CAACN,EAAE,EAAEI,CAAC,CAAC;IACtDH,MAAM,GAAGC,GAAG;IACZ,OAAOG,OAAO;EAChB;EACA,SAASE,QAAQA,CAAA;IACf,OAAON,MAAM,EAAE;EACjB;EACA,OAAO;IAAED,EAAE;IAAED,QAAQ;IAAEI,SAAS;IAAEI;EAAQ,CAAE;AAC9C;AAEA;;;;AAIA,OAAO,MAAMC,UAAU,GAAOR,EAAY,IAAO;EAC/C,MAAMD,QAAQ,GAAGP,KAAK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AAsB,IAChD,CACEQ,UAAU,CAACR,EAAE,CAAC,EACde,QAAQ,CAACf,EAAE,CAAC,CACJ"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@effect-rx/rx-react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "Reactive toolkit for Effect",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"sideEffects": [],
|
|
36
36
|
"author": "Effect contributors",
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@effect-rx/rx": "^0.1.
|
|
38
|
+
"@effect-rx/rx": "^0.1.5"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
41
|
"@effect/data": "^0.18.5",
|
package/src/index.ts
CHANGED
|
@@ -15,25 +15,26 @@ export * as Rx from "@effect-rx/rx/Rx"
|
|
|
15
15
|
*/
|
|
16
16
|
export const RegistryContext = React.createContext<Registry.Registry>(Registry.make())
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
)
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
} else {
|
|
29
|
-
this.init = true
|
|
30
|
-
}
|
|
31
|
-
return this.registry.subscribe(this.rx, (a) => {
|
|
32
|
-
this.value = a
|
|
33
|
-
f()
|
|
34
|
-
})
|
|
18
|
+
interface RxStore<A> {
|
|
19
|
+
readonly rx: Rx.Rx<A>
|
|
20
|
+
readonly registry: Registry.Registry
|
|
21
|
+
readonly subscribe: (f: () => void) => () => void
|
|
22
|
+
readonly snapshot: () => A
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function makeStore<A>(registry: Registry.Registry, rx: Rx.Rx<A>): RxStore<A> {
|
|
26
|
+
let getter = function() {
|
|
27
|
+
return registry.get(rx)
|
|
35
28
|
}
|
|
36
|
-
|
|
29
|
+
function subscribe(f: () => void): () => void {
|
|
30
|
+
const [get, unmount] = registry.subscribeGetter(rx, f)
|
|
31
|
+
getter = get
|
|
32
|
+
return unmount
|
|
33
|
+
}
|
|
34
|
+
function snapshot() {
|
|
35
|
+
return getter()
|
|
36
|
+
}
|
|
37
|
+
return { rx, registry, subscribe, snapshot }
|
|
37
38
|
}
|
|
38
39
|
|
|
39
40
|
/**
|
|
@@ -44,7 +45,7 @@ export const useRxValue = <A>(rx: Rx.Rx<A>): A => {
|
|
|
44
45
|
const registry = React.useContext(RegistryContext)
|
|
45
46
|
const store = React.useRef<RxStore<A>>(undefined as any)
|
|
46
47
|
if (store.current?.rx !== rx || store.current?.registry !== registry) {
|
|
47
|
-
store.current =
|
|
48
|
+
store.current = makeStore(registry, rx)
|
|
48
49
|
}
|
|
49
50
|
return React.useSyncExternalStore(store.current.subscribe, store.current.snapshot)
|
|
50
51
|
}
|
|
@@ -53,36 +54,35 @@ export const useRxValue = <A>(rx: Rx.Rx<A>): A => {
|
|
|
53
54
|
* @since 1.0.0
|
|
54
55
|
* @category hooks
|
|
55
56
|
*/
|
|
56
|
-
export const useSetRx = <R, W>(rx: Rx.Writeable<R, W>): (_: W) => void => {
|
|
57
|
+
export const useSetRx = <R, W>(rx: Rx.Writeable<R, W>): (_: W | ((_: R) => W)) => void => {
|
|
57
58
|
const registry = React.useContext(RegistryContext)
|
|
58
|
-
return React.useCallback((value) =>
|
|
59
|
+
return React.useCallback((value) => {
|
|
60
|
+
if (typeof value === "function") {
|
|
61
|
+
registry.set(rx, (value as any)(registry.get(rx)))
|
|
62
|
+
return
|
|
63
|
+
} else {
|
|
64
|
+
registry.set(rx, value)
|
|
65
|
+
}
|
|
66
|
+
}, [registry, rx])
|
|
59
67
|
}
|
|
60
68
|
|
|
61
69
|
/**
|
|
62
70
|
* @since 1.0.0
|
|
63
71
|
* @category hooks
|
|
64
72
|
*/
|
|
65
|
-
export const
|
|
73
|
+
export const useRefreshRx = <A>(rx: Rx.Rx<A> & Rx.Refreshable): () => void => {
|
|
66
74
|
const registry = React.useContext(RegistryContext)
|
|
67
|
-
return React.useCallback((
|
|
75
|
+
return React.useCallback(() => {
|
|
76
|
+
registry.refresh(rx)
|
|
77
|
+
}, [registry, rx])
|
|
68
78
|
}
|
|
69
79
|
|
|
70
80
|
/**
|
|
71
81
|
* @since 1.0.0
|
|
72
82
|
* @category hooks
|
|
73
83
|
*/
|
|
74
|
-
export const useRx = <R, W>(rx: Rx.Writeable<R, W>): readonly [R, (_: W) => void] =>
|
|
84
|
+
export const useRx = <R, W>(rx: Rx.Writeable<R, W>): readonly [value: R, setOrUpdate: (_: W | ((_: R) => W)) => void] =>
|
|
75
85
|
[
|
|
76
86
|
useRxValue(rx),
|
|
77
87
|
useSetRx(rx)
|
|
78
88
|
] as const
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* @since 1.0.0
|
|
82
|
-
* @category hooks
|
|
83
|
-
*/
|
|
84
|
-
export const useRxUpdate = <R, W>(rx: Rx.Writeable<R, W>): readonly [R, (f: (_: R) => W) => void] =>
|
|
85
|
-
[
|
|
86
|
-
useRxValue(rx),
|
|
87
|
-
useUpdateRx(rx)
|
|
88
|
-
] as const
|