-
Notifications
You must be signed in to change notification settings - Fork 2
/
IndirectionSynchronizer.ts
136 lines (122 loc) · 4.63 KB
/
IndirectionSynchronizer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { WildcardAccessPath } from "./shared/AccessPath";
import JsonProxy, { NoWildcard, SynchronizerTriggerCallback, TriggeredWildcard } from "./shared/JsonProxy";
import Log from './Log';
import * as Obj from "./shared/Obj";
const logger = Log.logger(__filename);
const NotFound = new Object();
// Read an adress from an indiproperty and dynamically register watchers for it
// ROOT is backofficeStatus
// POINTER: is IndiProperty
export default class IndirectionSynchronizer<ROOT, POINTER> {
private readonly appStateManager: JsonProxy<ROOT>;
private readonly pointerPath: Array<string|null>;
private readonly hasWildcard: boolean;
private readonly listenBuilder: (wildcardValues: string, pointer: POINTER)=>SynchronizerTriggerCallback|null;
private readonly synchronizerByPath: {[wildcardValues: string]: {listenFor: POINTER, cb: SynchronizerTriggerCallback|null}} = {};
// pointerPath is limited to one wildcard
constructor(appStateManager: JsonProxy<ROOT>,
pointerPath: WildcardAccessPath<ROOT, POINTER>,
listenBuilder: (wildcardValues: string, pointer: POINTER)=>SynchronizerTriggerCallback|null)
{
this.appStateManager = appStateManager;
this.pointerPath = pointerPath.path;
this.listenBuilder = listenBuilder;
let nullFound = false;
for(const v of this.pointerPath) {
if (v === null) nullFound = true;
}
this.hasWildcard = nullFound;
this.appStateManager.addSynchronizer(
this.pointerPath,
this.updateProperties,
true, true);
}
getCurrentWildCardValues() {
if (!this.hasWildcard) {
return [""];
}
let pointer:any = this.appStateManager.getTarget();
for(const v of this.pointerPath) {
if ((typeof pointer !== "object") || pointer === null) {
logger.debug('Not an object', {v, pointer})
return [];
}
if (v === null) {
return Object.keys(pointer);
}
if (!Obj.hasKey(pointer, v)) {
logger.debug('Key not found', {v, pointer})
return [];
}
pointer = pointer[v];
}
throw new Error("wildcard not found");
}
getPointer(uid: string):POINTER|typeof NotFound {
let pointer:any = this.appStateManager.getTarget();
for(let v of this.pointerPath) {
if ((typeof pointer !== "object") || pointer === null) {
return NotFound;
}
if (v === null) {
v = uid;
if (!Obj.hasKey(pointer, v)) {
return NotFound;
}
}
pointer = pointer[v];
}
return pointer;
}
addRemoveSynchronizer(uuid: string) {
let needDelete: boolean = false;
let needCreate: boolean = false;
if (this.getCurrentWildCardValues().indexOf(uuid) === -1) {
needDelete = true;
} else if (!Obj.hasKey(this.synchronizerByPath, uuid)) {
needCreate = true;
} else {
const wantingProperty = this.getPointer(uuid);
const currentProperty = this.synchronizerByPath[uuid].listenFor;
if (Obj.deepEqual(wantingProperty, currentProperty)) {
return;
}
needDelete = true;
needCreate = true;
}
if (needDelete||needCreate) {
if (Obj.hasKey(this.synchronizerByPath, uuid)) {
const cb = this.synchronizerByPath[uuid].cb;
if (cb !== null) {
this.appStateManager.removeSynchronizer(cb);
}
delete this.synchronizerByPath[uuid];
}
}
if (needCreate) {
const wantingProperty = this.getPointer(uuid);
if (wantingProperty === NotFound) {
return;
}
this.synchronizerByPath[uuid] = {
listenFor: Obj.deepCopy(wantingProperty as POINTER),
cb: this.listenBuilder(uuid, wantingProperty as POINTER),
}
}
}
updateProperties=(where: TriggeredWildcard)=>{
logger.debug('Triggering ', where);
let toInspect : string[];
if (where[NoWildcard]) {
toInspect = Object.keys({
...this.synchronizerByPath,
...this.getCurrentWildCardValues(),
});
} else {
toInspect = Object.keys(where);
}
for(const uuid of toInspect) {
this.addRemoveSynchronizer(uuid);
}
}
}