@data-weave/datamanager-mobx 0.6.1 → 0.6.3
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.
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { LiveReference, OptimisticReference } from '@data-weave/datamanager';
|
|
2
|
+
export type ObservableOptimisticReference<T extends object> = OptimisticReference<T>;
|
|
3
|
+
/**
|
|
4
|
+
* Wraps a `LiveReference<T>` with optimistic patch support and MobX
|
|
5
|
+
* reactivity. Source-value reactivity is provided by `ObservableReference`;
|
|
6
|
+
* a separate atom owned by this factory tracks `_patch` mutations made via
|
|
7
|
+
* `applyOptimistic` / `clearOptimistic` so that MobX reactions observing
|
|
8
|
+
* `value` (or `hasPendingOptimistic`) re-run when the optimistic overlay
|
|
9
|
+
* changes.
|
|
10
|
+
*/
|
|
11
|
+
export declare const ObservableOptimisticReference: <T extends object>(source: LiveReference<T>) => OptimisticReference<T>;
|
|
12
|
+
//# sourceMappingURL=ObservableOptimisticReference.d.ts.map
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ObservableOptimisticReference = void 0;
|
|
4
|
+
const datamanager_1 = require("@data-weave/datamanager");
|
|
5
|
+
const mobx_1 = require("mobx");
|
|
6
|
+
const ObservableReference_1 = require("./ObservableReference");
|
|
7
|
+
/**
|
|
8
|
+
* Wraps a `LiveReference<T>` with optimistic patch support and MobX
|
|
9
|
+
* reactivity. Source-value reactivity is provided by `ObservableReference`;
|
|
10
|
+
* a separate atom owned by this factory tracks `_patch` mutations made via
|
|
11
|
+
* `applyOptimistic` / `clearOptimistic` so that MobX reactions observing
|
|
12
|
+
* `value` (or `hasPendingOptimistic`) re-run when the optimistic overlay
|
|
13
|
+
* changes.
|
|
14
|
+
*/
|
|
15
|
+
const ObservableOptimisticReference = (source) => {
|
|
16
|
+
const observableSource = (0, ObservableReference_1.ObservableReference)(source);
|
|
17
|
+
const optimistic = new datamanager_1.OptimisticReference(observableSource);
|
|
18
|
+
const patchAtom = (0, mobx_1.createAtom)('ObservableOptimisticReference');
|
|
19
|
+
return new Proxy(optimistic, {
|
|
20
|
+
get(target, prop, receiver) {
|
|
21
|
+
if (prop === 'value' || prop === 'hasPendingOptimistic') {
|
|
22
|
+
patchAtom.reportObserved();
|
|
23
|
+
}
|
|
24
|
+
const value = Reflect.get(target, prop, receiver);
|
|
25
|
+
if (prop === 'applyOptimistic' || prop === 'clearOptimistic') {
|
|
26
|
+
return (...args) => {
|
|
27
|
+
const result = value.apply(target, args);
|
|
28
|
+
patchAtom.reportChanged();
|
|
29
|
+
return result;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
return value;
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
exports.ObservableOptimisticReference = ObservableOptimisticReference;
|
|
37
|
+
//# sourceMappingURL=ObservableOptimisticReference.js.map
|
|
@@ -2,16 +2,28 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ObservableReference = void 0;
|
|
4
4
|
const mobx_1 = require("mobx");
|
|
5
|
+
const PATCHED = Symbol('@data-weave/observableReference.patched');
|
|
5
6
|
const ObservableReference = (sourceReference) => {
|
|
6
7
|
const atom = (0, mobx_1.createAtom)('ObservableReference', () => sourceReference.resolve(), () => sourceReference.unsubscribe());
|
|
8
|
+
// FirestoreReference (and any LiveReference) calls `this.onUpdate(...)` on
|
|
9
|
+
// the bare instance from inside its snapshot callback, which then calls
|
|
10
|
+
// `this.onValueChange()` on the bare instance too. The Proxy's get trap
|
|
11
|
+
// never sees that call, so we patch the bare method to also notify the
|
|
12
|
+
// atom. Idempotent across multiple wraps via the PATCHED symbol guard.
|
|
13
|
+
const tagged = sourceReference;
|
|
14
|
+
if (!tagged[PATCHED]) {
|
|
15
|
+
tagged[PATCHED] = true;
|
|
16
|
+
const original = sourceReference.onValueChange.bind(sourceReference);
|
|
17
|
+
sourceReference.onValueChange = () => {
|
|
18
|
+
original();
|
|
19
|
+
atom.reportChanged();
|
|
20
|
+
};
|
|
21
|
+
}
|
|
7
22
|
return new Proxy(sourceReference, {
|
|
8
23
|
get(target, prop, receiver) {
|
|
9
24
|
if (prop === 'value' || prop === 'resolved' || prop === 'hasError' || prop === 'error') {
|
|
10
25
|
atom.reportObserved();
|
|
11
26
|
}
|
|
12
|
-
if (prop === 'onValueChange') {
|
|
13
|
-
atom.reportChanged();
|
|
14
|
-
}
|
|
15
27
|
return Reflect.get(target, prop, receiver);
|
|
16
28
|
},
|
|
17
29
|
});
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -17,5 +17,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("@data-weave/backend-firestore"), exports);
|
|
18
18
|
__exportStar(require("@data-weave/datamanager"), exports);
|
|
19
19
|
__exportStar(require("./ObservableList"), exports);
|
|
20
|
+
__exportStar(require("./ObservableOptimisticReference"), exports);
|
|
20
21
|
__exportStar(require("./ObservableReference"), exports);
|
|
21
22
|
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@data-weave/datamanager-mobx",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.3",
|
|
4
4
|
"author": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -23,15 +23,15 @@
|
|
|
23
23
|
"registry": "https://registry.npmjs.org/"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@data-weave/backend-firestore": "~0.6.
|
|
27
|
-
"@data-weave/datamanager": "~0.6.
|
|
26
|
+
"@data-weave/backend-firestore": "~0.6.3",
|
|
27
|
+
"@data-weave/datamanager": "~0.6.3"
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
30
|
"mobx": ">=6.0.0"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@data-weave/backend-firestore": "~0.6.
|
|
34
|
-
"@data-weave/datamanager": "~0.6.
|
|
33
|
+
"@data-weave/backend-firestore": "~0.6.3",
|
|
34
|
+
"@data-weave/datamanager": "~0.6.3",
|
|
35
35
|
"mobx": "6.15.0",
|
|
36
36
|
"typescript": "5.9.3"
|
|
37
37
|
},
|