reactjrx 1.0.3 → 1.2.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/index.js +62 -0
- package/dist/index.umd.cjs +64 -0
- package/package.json +10 -1
package/dist/index.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { distinctUntilChanged, tap, BehaviorSubject } from "rxjs";
|
|
2
|
+
import { useRef, useCallback, useSyncExternalStore } from "react";
|
|
3
|
+
function shallowEqual(objA, objB) {
|
|
4
|
+
if (objA === null || objA === void 0 || objB === null || objB === void 0) {
|
|
5
|
+
return objA === objB;
|
|
6
|
+
}
|
|
7
|
+
if (typeof objA !== "object" || typeof objB !== "object") {
|
|
8
|
+
return objA === objB;
|
|
9
|
+
}
|
|
10
|
+
if (objA.constructor !== objB.constructor) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
const keysA = Object.keys(objA);
|
|
14
|
+
const keysB = Object.keys(objB);
|
|
15
|
+
if (keysA.length !== keysB.length) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
for (let i = 0; i < keysA.length; i++) {
|
|
19
|
+
const key = keysA[i];
|
|
20
|
+
if (key && objA[key] !== objB[key]) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
function useObserve(source$, { defaultValue }, deps = []) {
|
|
27
|
+
const valueRef = useRef(defaultValue);
|
|
28
|
+
const subscribe = useCallback((next) => {
|
|
29
|
+
const sourceAsFn = typeof source$ === "function" ? source$ : () => source$;
|
|
30
|
+
const sub = sourceAsFn().pipe(
|
|
31
|
+
distinctUntilChanged(shallowEqual),
|
|
32
|
+
tap((value) => {
|
|
33
|
+
valueRef.current = value;
|
|
34
|
+
})
|
|
35
|
+
).subscribe(next);
|
|
36
|
+
return () => sub.unsubscribe();
|
|
37
|
+
}, deps);
|
|
38
|
+
const getSnapshot = useCallback(() => valueRef.current, []);
|
|
39
|
+
return useSyncExternalStore(subscribe, getSnapshot);
|
|
40
|
+
}
|
|
41
|
+
const signal = ({
|
|
42
|
+
default: defaultValue
|
|
43
|
+
}) => {
|
|
44
|
+
const subject = new BehaviorSubject(defaultValue);
|
|
45
|
+
const hook = () => useObserve(subject.asObservable(), { defaultValue }, []);
|
|
46
|
+
const setValue = (arg) => {
|
|
47
|
+
if (arg === subject.getValue())
|
|
48
|
+
return;
|
|
49
|
+
if (typeof arg === "function") {
|
|
50
|
+
const change = arg(subject.getValue());
|
|
51
|
+
if (change === subject.getValue())
|
|
52
|
+
return;
|
|
53
|
+
return subject.next(change);
|
|
54
|
+
}
|
|
55
|
+
return subject.next(arg);
|
|
56
|
+
};
|
|
57
|
+
return [hook, setValue];
|
|
58
|
+
};
|
|
59
|
+
export {
|
|
60
|
+
signal,
|
|
61
|
+
useObserve
|
|
62
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
(function(global, factory) {
|
|
2
|
+
typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("rxjs"), require("react")) : typeof define === "function" && define.amd ? define(["exports", "rxjs", "react"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.reactjrx = {}, global.rxjs, global.react));
|
|
3
|
+
})(this, function(exports2, rxjs, react) {
|
|
4
|
+
"use strict";
|
|
5
|
+
function shallowEqual(objA, objB) {
|
|
6
|
+
if (objA === null || objA === void 0 || objB === null || objB === void 0) {
|
|
7
|
+
return objA === objB;
|
|
8
|
+
}
|
|
9
|
+
if (typeof objA !== "object" || typeof objB !== "object") {
|
|
10
|
+
return objA === objB;
|
|
11
|
+
}
|
|
12
|
+
if (objA.constructor !== objB.constructor) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
const keysA = Object.keys(objA);
|
|
16
|
+
const keysB = Object.keys(objB);
|
|
17
|
+
if (keysA.length !== keysB.length) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
for (let i = 0; i < keysA.length; i++) {
|
|
21
|
+
const key = keysA[i];
|
|
22
|
+
if (key && objA[key] !== objB[key]) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
function useObserve(source$, { defaultValue }, deps = []) {
|
|
29
|
+
const valueRef = react.useRef(defaultValue);
|
|
30
|
+
const subscribe = react.useCallback((next) => {
|
|
31
|
+
const sourceAsFn = typeof source$ === "function" ? source$ : () => source$;
|
|
32
|
+
const sub = sourceAsFn().pipe(
|
|
33
|
+
rxjs.distinctUntilChanged(shallowEqual),
|
|
34
|
+
rxjs.tap((value) => {
|
|
35
|
+
valueRef.current = value;
|
|
36
|
+
})
|
|
37
|
+
).subscribe(next);
|
|
38
|
+
return () => sub.unsubscribe();
|
|
39
|
+
}, deps);
|
|
40
|
+
const getSnapshot = react.useCallback(() => valueRef.current, []);
|
|
41
|
+
return react.useSyncExternalStore(subscribe, getSnapshot);
|
|
42
|
+
}
|
|
43
|
+
const signal = ({
|
|
44
|
+
default: defaultValue
|
|
45
|
+
}) => {
|
|
46
|
+
const subject = new rxjs.BehaviorSubject(defaultValue);
|
|
47
|
+
const hook = () => useObserve(subject.asObservable(), { defaultValue }, []);
|
|
48
|
+
const setValue = (arg) => {
|
|
49
|
+
if (arg === subject.getValue())
|
|
50
|
+
return;
|
|
51
|
+
if (typeof arg === "function") {
|
|
52
|
+
const change = arg(subject.getValue());
|
|
53
|
+
if (change === subject.getValue())
|
|
54
|
+
return;
|
|
55
|
+
return subject.next(change);
|
|
56
|
+
}
|
|
57
|
+
return subject.next(arg);
|
|
58
|
+
};
|
|
59
|
+
return [hook, setValue];
|
|
60
|
+
};
|
|
61
|
+
exports2.signal = signal;
|
|
62
|
+
exports2.useObserve = useObserve;
|
|
63
|
+
Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
|
|
64
|
+
});
|
package/package.json
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "reactjrx",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "1.0
|
|
4
|
+
"version": "1.2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist"
|
|
8
8
|
],
|
|
9
|
+
"main": "./dist/index.umd.cjs",
|
|
10
|
+
"module": "./dist/index.js",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"require": "./dist/index.umd.cjs"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
9
17
|
"publishConfig": {
|
|
10
18
|
"access": "public"
|
|
11
19
|
},
|
|
@@ -13,6 +21,7 @@
|
|
|
13
21
|
"dev": "vite",
|
|
14
22
|
"build": "tsc && vite build",
|
|
15
23
|
"preview": "vite preview",
|
|
24
|
+
"prepublishOnly": "npm run build",
|
|
16
25
|
"semantic-release": "semantic-release"
|
|
17
26
|
},
|
|
18
27
|
"peerDependencies": {
|