react-obsidian 0.0.47 → 1.0.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/dist/src/observable/useObserver.d.ts +4 -2
- package/dist/src/observable/useObserver.d.ts.map +1 -1
- package/dist/src/observable/useObserver.js +6 -1
- package/dist/src/observable/useObserver.js.map +1 -1
- package/documentation/docs/documentation/usage/Reactivity.mdx +20 -0
- package/documentation/yarn.lock +8167 -0
- package/package.json +1 -1
- package/src/observable/useObserver.ts +18 -3
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
import { Observable } from './
|
|
2
|
-
|
|
1
|
+
import { Observable } from './Observable';
|
|
2
|
+
type ObservableOrGenerator<T> = Observable<T> | (() => Observable<T>);
|
|
3
|
+
export declare function useObserver<T>(observableOrGenerator: ObservableOrGenerator<T>): [T, (next: T) => void];
|
|
4
|
+
export {};
|
|
3
5
|
//# sourceMappingURL=useObserver.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useObserver.d.ts","sourceRoot":"","sources":["../../../src/observable/useObserver.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useObserver.d.ts","sourceRoot":"","sources":["../../../src/observable/useObserver.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C,KAAK,qBAAqB,CAAC,CAAC,IAAI,UAAU,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;AAEtE,wBAAgB,WAAW,CAAC,CAAC,EAAE,qBAAqB,EAAE,qBAAqB,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,IAAI,CAAC,CAetG"}
|
|
@@ -3,7 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.useObserver = void 0;
|
|
4
4
|
/* eslint-disable no-param-reassign */
|
|
5
5
|
const react_1 = require("react");
|
|
6
|
-
|
|
6
|
+
const Observable_1 = require("./Observable");
|
|
7
|
+
function useObserver(observableOrGenerator) {
|
|
8
|
+
const observable = (0, react_1.useMemo)(() => getOrGenerateObservable(observableOrGenerator), []);
|
|
7
9
|
const [value, setValue] = (0, react_1.useState)(observable.value);
|
|
8
10
|
const onNext = (0, react_1.useCallback)((next) => {
|
|
9
11
|
observable.value = next;
|
|
@@ -14,4 +16,7 @@ function useObserver(observable) {
|
|
|
14
16
|
return [value, onNext];
|
|
15
17
|
}
|
|
16
18
|
exports.useObserver = useObserver;
|
|
19
|
+
function getOrGenerateObservable(observableOrGenerator) {
|
|
20
|
+
return observableOrGenerator instanceof Observable_1.Observable ? observableOrGenerator : observableOrGenerator();
|
|
21
|
+
}
|
|
17
22
|
//# sourceMappingURL=useObserver.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useObserver.js","sourceRoot":"","sources":["../../../src/observable/useObserver.ts"],"names":[],"mappings":";;;AAAA,sCAAsC;AACtC,
|
|
1
|
+
{"version":3,"file":"useObserver.js","sourceRoot":"","sources":["../../../src/observable/useObserver.ts"],"names":[],"mappings":";;;AAAA,sCAAsC;AACtC,iCAKe;AACf,6CAA0C;AAI1C,SAAgB,WAAW,CAAI,qBAA+C;IAC5E,MAAM,UAAU,GAAG,IAAA,eAAO,EACxB,GAAG,EAAE,CAAC,uBAAuB,CAAC,qBAAqB,CAAC,EACpD,EAAE,CACH,CAAC;IACF,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,IAAA,gBAAQ,EAAC,UAAU,CAAC,KAAK,CAAC,CAAC;IACrD,MAAM,MAAM,GAAG,IAAA,mBAAW,EAAC,CAAC,IAAO,EAAE,EAAE;QACrC,UAAU,CAAC,KAAK,GAAG,IAAI,CAAC;IAC1B,CAAC,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC;IAEjB,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,OAAO,UAAU,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;IACxC,CAAC,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC;IAEjB,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;AACzB,CAAC;AAfD,kCAeC;AAED,SAAS,uBAAuB,CAAC,qBAAiD;IAChF,OAAO,qBAAqB,YAAY,uBAAU,CAAC,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,qBAAqB,EAAE,CAAC;AACvG,CAAC"}
|
|
@@ -74,6 +74,26 @@ const useLogin = () => {
|
|
|
74
74
|
}
|
|
75
75
|
```
|
|
76
76
|
|
|
77
|
+
### Avoid recreating the initial observable
|
|
78
|
+
When using the `useObserver` hook, it is important to avoid recreating the initial observable.
|
|
79
|
+
|
|
80
|
+
```ts title="Avoid instantiating observables in hooks"
|
|
81
|
+
const useLogin = () => {
|
|
82
|
+
const [isLoggedIn] = useObserver(new Observable(false));
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Even if the value of the observable is the same, this can cause unexpected behavior since it's instantiated on every render.
|
|
87
|
+
|
|
88
|
+
To solve this, you can pass a generator function to the useObserver hook instead:
|
|
89
|
+
```ts
|
|
90
|
+
const useLogin = () => {
|
|
91
|
+
const [isLoggedIn] = useObserver(() => new Observable(false));
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
If you pass a function to the useObserver hook, it will only be called on the first render. This ensures that the observable is only instantiated once.
|
|
96
|
+
|
|
77
97
|
### Merge multiple observable sources
|
|
78
98
|
`MediatorObservable` is a special type of observable that allows you to merge multiple observable sources into a single observable. This is useful for creating side effect from one or more observables.
|
|
79
99
|
|