@dra2020/baseclient 1.0.72 → 1.0.73

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 @@
1
+ export * from './dataflow';
@@ -0,0 +1,30 @@
1
+ interface UseItem {
2
+ name: string;
3
+ df: DataFlow;
4
+ id?: any;
5
+ }
6
+ export declare class DataFlow {
7
+ uniquename: number;
8
+ usesList: {
9
+ [name: string]: UseItem;
10
+ };
11
+ constructor();
12
+ ready(): boolean;
13
+ id(): any;
14
+ value(): any;
15
+ uses(df: DataFlow, name?: string): void;
16
+ find(name: string): DataFlow;
17
+ findValue(name: string): any;
18
+ usesReady(): boolean;
19
+ usesStale(): boolean;
20
+ usesRemember(): void;
21
+ }
22
+ export declare class DataFlowNonNull extends DataFlow {
23
+ _value: any;
24
+ _cb: () => any;
25
+ constructor(cb: () => any);
26
+ ready(): boolean;
27
+ id(): any;
28
+ value(): any;
29
+ }
30
+ export {};
package/lib/all/all.ts CHANGED
@@ -25,3 +25,5 @@ import * as CSV from '../csv/all';
25
25
  export { CSV };
26
26
  import * as Colors from '../colors/colors';
27
27
  export { Colors };
28
+ import * as DataFlow from '../dataflow/all';
29
+ export { DataFlow }
@@ -0,0 +1 @@
1
+ export * from './dataflow';
@@ -0,0 +1,106 @@
1
+ //
2
+ // DataFlow: mechanism for setting up a data-flow dependency graph that gets computed on demand.
3
+ //
4
+ // Semantics are these:
5
+ // 1. The simplest "atomic" DataFlow object just has an id() and a value(). The id() is used to check for exact
6
+ // equivalence when determining if any dependents need to be recomputed. id() and value() might be the same
7
+ // for something that just creates a new whole object when recomputed. In other cases, id() might represent a
8
+ // hash or changestamp/timestamp that is distinct from the value(). The value may or may not be "ready" as well.
9
+ // If the value is not "ready", no dependents can be computed.
10
+ // 2. A DataFlow object can record that it "uses" another DataFlow object. If it does, it can use a set of helper
11
+ // routines to track the state of its dependents. When its dependents are all ready(), it remembers their ids
12
+ // and can later test if they are stale.
13
+ //
14
+ //
15
+
16
+ interface UseItem
17
+ {
18
+ name: string,
19
+ df: DataFlow,
20
+ id?: any,
21
+ }
22
+
23
+ export class DataFlow
24
+ {
25
+ uniquename: number;
26
+ usesList: { [name: string]: UseItem };
27
+
28
+ constructor()
29
+ {
30
+ this.usesList = {};
31
+ this.uniquename = 1;
32
+ }
33
+
34
+ // override in subclass
35
+ ready(): boolean { return this.usesReady() }
36
+ id(): any { return null }
37
+ value(): any { return null }
38
+
39
+ uses(df: DataFlow, name?: string): void
40
+ {
41
+ if (!name) name = `_df_${this.uniquename++}`;
42
+ this.usesList[name] = { name: name, df: df };
43
+ }
44
+
45
+ find(name: string): DataFlow
46
+ {
47
+ let ui = this.usesList[name];
48
+ return ui ? ui.df : undefined;
49
+ }
50
+
51
+ findValue(name: string): any
52
+ {
53
+ let df = this.find(name);
54
+ return df ? df.value() : undefined;
55
+ }
56
+
57
+ usesReady(): boolean
58
+ {
59
+ let isready = true;
60
+ Object.values(this.usesList).forEach((ui: UseItem) => { if (!ui.df.ready()) isready = false });
61
+ return isready;
62
+ }
63
+
64
+ usesStale(): boolean
65
+ {
66
+ let isstale = false;
67
+ Object.values(this.usesList).forEach((ui: UseItem) => { if (ui.df.id !== ui.df.id()) isstale = true });
68
+ return isstale;
69
+ }
70
+
71
+ usesRemember(): void
72
+ {
73
+ Object.values(this.usesList).forEach((ui: UseItem) => { ui.df.id = ui.df.id() });
74
+ }
75
+ }
76
+
77
+ // Takes callback that, when ready, returns non-null. The return value is both the value and the id.
78
+ export class DataFlowNonNull extends DataFlow
79
+ {
80
+ _value: any;
81
+ _cb: () => any;
82
+
83
+ constructor(cb: () => any)
84
+ {
85
+ super();
86
+ this._cb = cb;
87
+ }
88
+
89
+ ready(): boolean
90
+ {
91
+ // Allow chaining
92
+ if (!this.usesReady())
93
+ return false;
94
+
95
+ // Real core semantics, with chaining on stale
96
+ if (!this._value || this.usesStale())
97
+ {
98
+ this._value = this._cb();
99
+ if (this._value) this.usesRemember();
100
+ }
101
+ return !!this._value;
102
+ }
103
+
104
+ id(): any { return this.ready(), this._value }
105
+ value(): any { return this.ready(), this._value }
106
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dra2020/baseclient",
3
- "version": "1.0.72",
3
+ "version": "1.0.73",
4
4
  "description": "Utility functions for Javascript projects.",
5
5
  "main": "dist/baseclient.js",
6
6
  "types": "./dist/all/all.d.ts",