-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLocalForageObservableWrapper.ts
91 lines (80 loc) · 2.54 KB
/
LocalForageObservableWrapper.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
import { LocalForageObservableChangeWithPrivateProps } from './LocalForageObservableChangeWithPrivateProps';
import { ObservableLibraryMethods } from './ObservableLibraryMethods';
export class LocalForageObservableWrapper {
constructor(
public options: LocalForageObservableOptions,
private subscriptionObserver: Observer<LocalForageObservableChange>,
) {}
hasMethodFilterOptions() {
if (this.options) {
for (const methodName of ObservableLibraryMethods) {
if (this.options[methodName]) {
return true;
}
}
}
return false;
}
shouldNotifyAboutMethodCall(
methodName: keyof LocalForageObservableMethodOptions,
) {
return (
!this.options ||
!!this.options[methodName] ||
// if it doesn't have any specific method set
// it applies to all of them
!this.hasMethodFilterOptions()
);
}
shouldNotifyAboutKey(key: string) {
return !this.options || !this.options.key || this.options.key === key;
}
shouldNotifyAboutAffectedKey(
changeArgs: LocalForageObservableChangeWithPrivateProps,
) {
if (!this.options || !this.options.key) {
return true;
}
if (this.options.key === changeArgs.key) {
return true;
}
// if it affects all keys
if (changeArgs.methodName === 'clear') {
if (!this.options.changeDetection) {
return true;
}
if (changeArgs._affectedItemsByKey) {
const affectedItem =
changeArgs._affectedItemsByKey[this.options.key];
if (affectedItem && this.options.key === affectedItem.key) {
return true;
}
}
}
return false;
}
publish(publishObject: LocalForageObservableChange) {
if (
publishObject.success &&
typeof this.subscriptionObserver.next === 'function'
) {
try {
this.subscriptionObserver.next(publishObject);
} catch (e) {
/* */
}
return;
}
if (
publishObject.fail &&
typeof this.subscriptionObserver.error === 'function'
) {
try {
this.subscriptionObserver.error(publishObject);
} catch (e) {
/* */
}
return;
}
}
}