-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStorageEventObserver.ts
209 lines (188 loc) · 6.18 KB
/
StorageEventObserver.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* global window:true */
import localforage from 'localforage';
import { processObserverList } from './facades';
import { LocalForageWithObservablePrivateProps } from './LocalForageWithObservablePrivateProps';
interface StorageEventPayload {
name: string;
storeName: string;
key: string;
methodName: string;
valueChange: boolean;
success: boolean;
fail: boolean;
error: string;
ticks: number;
}
const isSupported =
typeof window !== 'undefined' &&
typeof window.addEventListener === 'function' &&
typeof window.removeEventListener === 'function' &&
typeof JSON !== 'undefined' &&
JSON.stringify &&
JSON.parse &&
localforage.supports(localforage.LOCALSTORAGE);
const sysKeyPrefix = ['_localforage_sys', '_localforage_observable_sys'].join(
'/',
);
const db = isSupported ? window.localStorage : null;
let inited = false;
export default class StorageEventObserver {
private localforageInstance: LocalForageWithObservablePrivateProps | null;
constructor(localforageInstance: LocalForageWithObservableMethods) {
this.localforageInstance = localforageInstance as LocalForageWithObservablePrivateProps;
this._onStorageEventBinded = this._onStorageEvent.bind(this);
}
setup() {
if (!isSupported || inited) {
return;
}
window.addEventListener('storage', this._onStorageEventBinded, false);
inited = true;
}
destroy() {
this.localforageInstance = null;
if (inited) {
window.removeEventListener(
'storage',
this._onStorageEventBinded,
false,
);
inited = false;
}
}
_onStorageEventBinded: (e: StorageEvent) => Promise<void>;
_onStorageEvent(e: StorageEvent) {
if (
!this.localforageInstance ||
e.key !== sysKeyPrefix ||
!e.newValue
) {
return;
}
try {
const payload = JSON.parse(
e.newValue,
) as LocalForageObservableChange & {
name: string;
storeName: string;
};
if (!payload) {
return;
}
const dbInfo = this.localforageInstance._dbInfo;
if (
dbInfo.name !== payload.name ||
dbInfo.storeName !== payload.storeName
) {
return;
}
return this.localforageInstance
.ready()
.then(() => {
const changeArgs = {
key: payload.key,
methodName: payload.methodName,
oldValue: null,
newValue: null,
success: payload.success,
fail: payload.fail,
error: payload.error,
valueChange: payload.valueChange,
crossTabNotification: 'StorageEvent',
originalEvent: e,
} as LocalForageObservableChange;
if (payload.methodName === 'setItem' && payload.success) {
return this.localforageInstance!.getItem(
payload.key,
).then((newValue: any) => {
changeArgs.newValue = newValue;
return changeArgs;
});
}
return changeArgs;
})
.then((changeArgs: LocalForageObservableChange) => {
// this will run only in case the crossTabChangeDetection
// is enaled on the other page or there is at least one
// changeDetection Observable
if (changeArgs.valueChange) {
processObserverList(
this.localforageInstance!._observables
.changeDetection,
changeArgs,
);
}
processObserverList(
this.localforageInstance!._observables.callDetection,
changeArgs,
);
});
} catch (ex) {
return Promise.reject(ex);
}
}
publish(changeArgs: LocalForageObservableChange) {
if (!isSupported || !db) {
return;
}
const dbInfo = this.localforageInstance!._dbInfo;
let errorString;
try {
if (changeArgs.error) {
errorString = JSON.stringify(changeArgs.error);
}
} catch (ex) {
// empty
}
const payload = {
name: dbInfo.name,
storeName: dbInfo.storeName,
key: changeArgs.key,
methodName: changeArgs.methodName,
valueChange: changeArgs.valueChange,
success: changeArgs.success,
fail: changeArgs.fail,
error: errorString,
ticks: +new Date(),
} as StorageEventPayload;
const value = JSON.stringify(payload);
db.setItem(sysKeyPrefix, value);
}
}
// {
// target: Window → index.html,
// isTrusted: true,
// key: "_localforage_sys/_localforage_observable_sys",
// oldValue: {
// name: localforage,
// storeName: keyvaluepairs,
// key: notObservedKey,
// methodName: setItem,
// ticks: 1486492946207
// },
// newValue: {
// name: localforage,
// storeName: keyvaluepairs,
// methodName: clear,
// ticks: 1486492946213
// },
// url: "....html",
// storageArea: Storage,
// currentTarget: Window → index.html,
// eventPhase: 2,
// bubbles: false,
// cancelable: false
// }
// {
// target: Window → index.html,
// isTrusted: true,
// key: "x",
// oldValue: "1486402032655",
// newValue: "1486402081880",
// url: "....html",
// storageArea: Storage,
// eventPhase: 0,
// bubbles: false,
// cancelable: false,
// defaultPrevented: false
// }