@fluid-topics/ft-wc-utils 1.2.39 → 1.2.41

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,15 @@
1
+ export declare const ALL_KEYS_VALUE = "__all__";
2
+ export interface SameWindowStorageEventProperty {
3
+ key: string;
4
+ oldValue?: string;
5
+ newValue?: string;
6
+ }
7
+ export declare class SameWindowStorageEvent extends CustomEvent<SameWindowStorageEventProperty> {
8
+ constructor(key: string, oldValue?: string, newValue?: string);
9
+ keyMatch(value: string): boolean;
10
+ }
11
+ declare global {
12
+ interface WindowEventMap {
13
+ ["same-window-storage"]: SameWindowStorageEvent;
14
+ }
15
+ }
@@ -0,0 +1,35 @@
1
+ /*
2
+ * https://developer.mozilla.org/en-US/docs/Web/API/Window/storage_event
3
+ * Storage event are trigger only on other tabs.
4
+ * It could be usefull to have event on sessionStorage or localStorage update
5
+ * For that we follow the Proxy'hack from https://stackoverflow.com/questions/26974084/listen-for-changes-with-localstorage-on-the-same-window
6
+ * */
7
+ export const ALL_KEYS_VALUE = "__all__";
8
+ export class SameWindowStorageEvent extends CustomEvent {
9
+ constructor(key, oldValue, newValue) {
10
+ super("same-window-storage", { detail: { key, oldValue, newValue } });
11
+ }
12
+ keyMatch(value) {
13
+ return this.detail.key.includes(value) || this.detail.key == ALL_KEYS_VALUE;
14
+ }
15
+ }
16
+ Storage.prototype.setItem = new Proxy(Storage.prototype.setItem, {
17
+ apply(setItem, storage, args) {
18
+ const before = storage.getItem(args[0]);
19
+ Reflect.apply(setItem, storage, args);
20
+ window.dispatchEvent(new SameWindowStorageEvent(args[0], before, args[1]));
21
+ },
22
+ });
23
+ Storage.prototype.removeItem = new Proxy(Storage.prototype.removeItem, {
24
+ apply(removeItem, storage, args) {
25
+ const before = storage.getItem(args[0]);
26
+ Reflect.apply(removeItem, storage, args);
27
+ window.dispatchEvent(new SameWindowStorageEvent(args[0], before));
28
+ },
29
+ });
30
+ Storage.prototype.clear = new Proxy(Storage.prototype.clear, {
31
+ apply(clear, storage, args) {
32
+ Reflect.apply(clear, storage, args);
33
+ window.dispatchEvent(new SameWindowStorageEvent(ALL_KEYS_VALUE));
34
+ },
35
+ });