@data-weave/datamanager 0.6.1 → 0.6.2
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/OptimisticReference.d.ts +28 -0
- package/dist/OptimisticReference.js +61 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Reference } from './Reference';
|
|
2
|
+
export type Patch<T> = Partial<T> | ((current: T) => Partial<T>);
|
|
3
|
+
/**
|
|
4
|
+
* Wraps a `Reference<T>` and overlays an optimistic `_patch` on top of the
|
|
5
|
+
* source value. The patch is automatically discarded the next time the source
|
|
6
|
+
* publishes a new value (detected lazily by comparing the source value against
|
|
7
|
+
* the snapshot the patch was applied against).
|
|
8
|
+
*
|
|
9
|
+
* This class is intentionally mobx-free. For reactive consumers, wrap the
|
|
10
|
+
* instance with `ObservableOptimisticReference` from
|
|
11
|
+
* `@data-weave/datamanager-mobx`.
|
|
12
|
+
*/
|
|
13
|
+
export declare class OptimisticReference<T extends object> implements Reference<T> {
|
|
14
|
+
private readonly source;
|
|
15
|
+
private _patch;
|
|
16
|
+
private _patchedAgainstSnapshot;
|
|
17
|
+
constructor(source: Reference<T>);
|
|
18
|
+
get value(): T | undefined;
|
|
19
|
+
get resolved(): boolean;
|
|
20
|
+
get hasError(): boolean;
|
|
21
|
+
get error(): unknown | undefined;
|
|
22
|
+
resolve(): Promise<T | undefined>;
|
|
23
|
+
applyOptimistic(patch: Patch<T>): void;
|
|
24
|
+
clearOptimistic(): void;
|
|
25
|
+
get hasPendingOptimistic(): boolean;
|
|
26
|
+
dispose(): void;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=OptimisticReference.d.ts.map
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OptimisticReference = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Wraps a `Reference<T>` and overlays an optimistic `_patch` on top of the
|
|
6
|
+
* source value. The patch is automatically discarded the next time the source
|
|
7
|
+
* publishes a new value (detected lazily by comparing the source value against
|
|
8
|
+
* the snapshot the patch was applied against).
|
|
9
|
+
*
|
|
10
|
+
* This class is intentionally mobx-free. For reactive consumers, wrap the
|
|
11
|
+
* instance with `ObservableOptimisticReference` from
|
|
12
|
+
* `@data-weave/datamanager-mobx`.
|
|
13
|
+
*/
|
|
14
|
+
class OptimisticReference {
|
|
15
|
+
source;
|
|
16
|
+
_patch;
|
|
17
|
+
_patchedAgainstSnapshot;
|
|
18
|
+
constructor(source) {
|
|
19
|
+
this.source = source;
|
|
20
|
+
}
|
|
21
|
+
get value() {
|
|
22
|
+
const base = this.source.value;
|
|
23
|
+
if (this._patch !== undefined && base !== this._patchedAgainstSnapshot) {
|
|
24
|
+
this._patch = undefined;
|
|
25
|
+
this._patchedAgainstSnapshot = undefined;
|
|
26
|
+
}
|
|
27
|
+
if (!this._patch)
|
|
28
|
+
return base;
|
|
29
|
+
if (!base)
|
|
30
|
+
return this._patch;
|
|
31
|
+
return { ...base, ...this._patch };
|
|
32
|
+
}
|
|
33
|
+
get resolved() {
|
|
34
|
+
return this.source.resolved;
|
|
35
|
+
}
|
|
36
|
+
get hasError() {
|
|
37
|
+
return this.source.hasError;
|
|
38
|
+
}
|
|
39
|
+
get error() {
|
|
40
|
+
return this.source.error;
|
|
41
|
+
}
|
|
42
|
+
resolve() {
|
|
43
|
+
return this.source.resolve();
|
|
44
|
+
}
|
|
45
|
+
applyOptimistic(patch) {
|
|
46
|
+
const current = this.value ?? {};
|
|
47
|
+
const resolved = typeof patch === 'function' ? patch(current) : patch;
|
|
48
|
+
this._patch = { ...this._patch, ...resolved };
|
|
49
|
+
this._patchedAgainstSnapshot = this.source.value;
|
|
50
|
+
}
|
|
51
|
+
clearOptimistic() {
|
|
52
|
+
this._patch = undefined;
|
|
53
|
+
this._patchedAgainstSnapshot = undefined;
|
|
54
|
+
}
|
|
55
|
+
get hasPendingOptimistic() {
|
|
56
|
+
return this._patch !== undefined;
|
|
57
|
+
}
|
|
58
|
+
dispose() { }
|
|
59
|
+
}
|
|
60
|
+
exports.OptimisticReference = OptimisticReference;
|
|
61
|
+
//# sourceMappingURL=OptimisticReference.js.map
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -19,5 +19,6 @@ __exportStar(require("./DataManager"), exports);
|
|
|
19
19
|
__exportStar(require("./List"), exports);
|
|
20
20
|
__exportStar(require("./LiveList"), exports);
|
|
21
21
|
__exportStar(require("./LiveReference"), exports);
|
|
22
|
+
__exportStar(require("./OptimisticReference"), exports);
|
|
22
23
|
__exportStar(require("./Reference"), exports);
|
|
23
24
|
//# sourceMappingURL=index.js.map
|