@data-weave/datamanager 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,18 @@
1
+ import type { Reference } from './Reference';
2
+ export type Patch<T> = Partial<T> | ((current: T) => Partial<T>);
3
+ export declare class OptimisticReference<T extends object> implements Reference<T> {
4
+ private readonly source;
5
+ private _patch;
6
+ constructor(source: Reference<T>);
7
+ get value(): T | undefined;
8
+ get resolved(): boolean;
9
+ get hasError(): boolean;
10
+ get error(): unknown | undefined;
11
+ resolve(): Promise<T | undefined>;
12
+ applyOptimistic(patch: Patch<T>): void;
13
+ clearOptimistic(): void;
14
+ get hasPendingOptimistic(): boolean;
15
+ dispose(): void;
16
+ private patchMatches;
17
+ }
18
+ //# sourceMappingURL=OptimisticReference.d.ts.map
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OptimisticReference = void 0;
4
+ class OptimisticReference {
5
+ source;
6
+ _patch;
7
+ constructor(source) {
8
+ this.source = source;
9
+ }
10
+ get value() {
11
+ const base = this.source.value;
12
+ if (this._patch !== undefined && base !== undefined && this.patchMatches(base, this._patch)) {
13
+ // Source has caught up to the optimistic write; safe to discard.
14
+ this._patch = undefined;
15
+ }
16
+ if (!this._patch)
17
+ return base;
18
+ if (!base)
19
+ return this._patch;
20
+ return { ...base, ...this._patch };
21
+ }
22
+ get resolved() {
23
+ return this.source.resolved;
24
+ }
25
+ get hasError() {
26
+ return this.source.hasError;
27
+ }
28
+ get error() {
29
+ return this.source.error;
30
+ }
31
+ resolve() {
32
+ return this.source.resolve();
33
+ }
34
+ applyOptimistic(patch) {
35
+ const current = this.value ?? {};
36
+ const resolved = typeof patch === 'function' ? patch(current) : patch;
37
+ this._patch = { ...this._patch, ...resolved };
38
+ }
39
+ clearOptimistic() {
40
+ this._patch = undefined;
41
+ }
42
+ get hasPendingOptimistic() {
43
+ return this._patch !== undefined;
44
+ }
45
+ dispose() { }
46
+ patchMatches(base, patch) {
47
+ for (const key of Object.keys(patch)) {
48
+ if (!Object.is(base[key], patch[key]))
49
+ return false;
50
+ }
51
+ return true;
52
+ }
53
+ }
54
+ exports.OptimisticReference = OptimisticReference;
55
+ //# sourceMappingURL=OptimisticReference.js.map
package/dist/index.d.ts CHANGED
@@ -3,5 +3,6 @@ export * from './DataManager';
3
3
  export * from './List';
4
4
  export * from './LiveList';
5
5
  export * from './LiveReference';
6
+ export * from './OptimisticReference';
6
7
  export * from './Reference';
7
8
  //# sourceMappingURL=index.d.ts.map
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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@data-weave/datamanager",
3
- "version": "0.6.1",
3
+ "version": "0.6.3",
4
4
  "author": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",