-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfacades.ts
141 lines (131 loc) · 5.03 KB
/
facades.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
137
138
139
140
141
import { AffectedItemChange } from './AffectedItemChange';
import { LocalForageObservableChangeWithPrivateProps } from './LocalForageObservableChangeWithPrivateProps';
import { LocalForageObservableWrapper } from './LocalForageObservableWrapper';
import { LocalForageWithObservablePrivateProps } from './LocalForageWithObservablePrivateProps';
import { equals as isEqual } from './utils';
export function processObserverList(
list: LocalForageObservableWrapper[],
changeArgs: LocalForageObservableChangeWithPrivateProps,
) {
for (const observableWrapper of list) {
const itemOptions = observableWrapper.options;
if (
!itemOptions ||
(observableWrapper.shouldNotifyAboutAffectedKey(changeArgs) &&
observableWrapper.shouldNotifyAboutMethodCall(
changeArgs.methodName,
) &&
// do not publish cross tab evets when the observable
// doesn't explicitelly require it,
// to avoid messing troubles in existing implementations.
(!changeArgs.crossTabNotification ||
itemOptions.crossTabNotification))
) {
observableWrapper.publish(changeArgs);
}
}
}
const getChangeArgsForKey = (
localforageInstance: LocalForageWithObservablePrivateProps,
methodName: keyof LocalForageObservableMethodOptions,
key: string,
newValue: any,
) => {
return localforageInstance.getItem(key).then(oldValue => {
return {
key,
methodName,
oldValue,
newValue,
} as LocalForageObservableChange;
});
};
const getChangeArgsForKeys = (
localforageInstance: LocalForageWithObservablePrivateProps,
methodName: keyof LocalForageObservableMethodOptions,
keys: string[],
newValue: any,
) => {
return Promise.all(
keys.map(key =>
getChangeArgsForKey(localforageInstance, methodName, key, newValue),
),
);
};
export const setOldValues = (
localforageInstance: LocalForageWithObservablePrivateProps,
detectChanges: boolean,
changeArgs: LocalForageObservableChangeWithPrivateProps,
): Promise<void> => {
if (!detectChanges) {
return Promise.resolve();
}
if (changeArgs.methodName === 'clear') {
const {
observedKeys,
allKeysObservers,
} = localforageInstance._observables.changeDetection.reduce(
(acc, o) => {
if (!o.shouldNotifyAboutMethodCall('clear')) {
return acc;
}
const observesKey = o.options && o.options.key;
if (!observesKey) {
acc.allKeysObservers.push(o);
} else if (acc.observedKeys.indexOf(observesKey) < 0) {
acc.keyObservers.push(o);
acc.observedKeys.push(observesKey);
}
return acc;
},
{
observedKeys: [] as string[],
keyObservers: [] as LocalForageObservableWrapper[],
allKeysObservers: [] as LocalForageObservableWrapper[],
},
);
const keysPromise = allKeysObservers.length
? localforageInstance.keys()
: Promise.resolve(observedKeys);
return keysPromise
.then(keys => {
const affectedItems: AffectedItemChange[] = keys.map(key => {
return {
oldValue: changeArgs.oldValue,
newValue: changeArgs.newValue,
key,
};
});
let affectedItemsByKey:
| { [key: string]: AffectedItemChange }
| undefined;
return Promise.all(
affectedItems.map(affectedItem => {
return localforageInstance
.getItem(affectedItem.key)
.then(value => {
affectedItem.oldValue = value;
const valueChange = !isEqual(
affectedItem.oldValue,
affectedItem.newValue,
);
if (valueChange) {
affectedItemsByKey =
affectedItemsByKey || {};
affectedItemsByKey[
affectedItem.key
] = affectedItem;
}
});
}),
).then(result => {
changeArgs._affectedItemsByKey = affectedItemsByKey;
});
})
.then(() => undefined);
}
return localforageInstance.getItem(changeArgs.key).then(function(value) {
changeArgs.oldValue = value;
// don't return anything
});
};