@linzjs/step-ag-grid 35.0.2 → 35.1.0

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,48 @@
1
+ import { describe, expect, test, vi } from 'vitest';
2
+
3
+ import { publishStorageKey, subscribeStorageKey } from './storageKeySync';
4
+
5
+ const fakeStorage = () => ({ setItem: vi.fn() }) as unknown as Storage & { setItem: ReturnType<typeof vi.fn> };
6
+
7
+ describe('storageKeySync', () => {
8
+ test('saves the value and notifies the other subscribers of that key only', () => {
9
+ const storage = fakeStorage();
10
+ const source = vi.fn();
11
+ const other = vi.fn();
12
+ const otherKey = vi.fn();
13
+ const unsubscribes = [
14
+ subscribeStorageKey('key', source),
15
+ subscribeStorageKey('key', other),
16
+ subscribeStorageKey('otherKey', otherKey),
17
+ ];
18
+
19
+ publishStorageKey(storage, 'key', 'value', source);
20
+
21
+ expect(storage.setItem).toHaveBeenCalledWith('key', 'value');
22
+ expect(other).toHaveBeenCalledWith('value');
23
+ expect(source).not.toHaveBeenCalled();
24
+ expect(otherKey).not.toHaveBeenCalled();
25
+ unsubscribes.forEach((unsubscribe) => unsubscribe());
26
+ });
27
+
28
+ test('stops notifying after unsubscribe', () => {
29
+ const listener = vi.fn();
30
+ subscribeStorageKey('key', listener)();
31
+ publishStorageKey(fakeStorage(), 'key', 'value');
32
+ expect(listener).not.toHaveBeenCalled();
33
+ });
34
+
35
+ test('still notifies subscribers when saving fails', () => {
36
+ const storage = {
37
+ setItem: () => {
38
+ throw new Error('quota');
39
+ },
40
+ } as unknown as Storage;
41
+ const listener = vi.fn();
42
+ const unsubscribe = subscribeStorageKey('key', listener);
43
+ vi.spyOn(console, 'error').mockImplementation(() => {});
44
+ publishStorageKey(storage, 'key', 'value');
45
+ expect(listener).toHaveBeenCalledWith('value');
46
+ unsubscribe();
47
+ });
48
+ });
@@ -0,0 +1,24 @@
1
+ type StorageKeyListener = (value: string) => void;
2
+
3
+ const listeners = new Map<string, Set<StorageKeyListener>>();
4
+
5
+ /** Notifies when another component publishes a new value for a localStorage key in this page, including popped-out windows */
6
+ export const subscribeStorageKey = (key: string, listener: StorageKeyListener): (() => void) => {
7
+ const keyListeners = listeners.get(key) ?? new Set();
8
+ listeners.set(key, keyListeners);
9
+ keyListeners.add(listener);
10
+ return () => {
11
+ keyListeners.delete(listener);
12
+ if (keyListeners.size === 0) listeners.delete(key);
13
+ };
14
+ };
15
+
16
+ /** Saves the value and passes it to every other subscriber of the key */
17
+ export const publishStorageKey = (storage: Storage, key: string, value: string, source?: StorageKeyListener) => {
18
+ try {
19
+ storage.setItem(key, value);
20
+ } catch (ex) {
21
+ console.error(ex);
22
+ }
23
+ listeners.get(key)?.forEach((listener) => listener !== source && listener(value));
24
+ };