commit 8943d47130ec8e9f78f0ed3493c7a516c89ca269
parent e2ef7cdd50bacb7bf2cc11be355acdd7a74572bd
Author: Florian Dold <dold@taler.net>
Date: Sat, 29 Aug 2026 19:40:58 +0200
web-util: remove unused map-wide update subscriptions
Diffstat:
2 files changed, 1 insertion(+), 29 deletions(-)
diff --git a/packages/web-util/src/utils/observable.test.ts b/packages/web-util/src/utils/observable.test.ts
@@ -2,28 +2,21 @@ import assert from "node:assert/strict";
import test from "node:test";
import { memoryMap } from "./observable.js";
-test("memory maps notify key and map subscribers", () => {
+test("memory maps notify key subscribers", () => {
const storage = memoryMap<string>();
let keyUpdates = 0;
- let anyUpdates = 0;
const unsubscribeKey = storage.onUpdate("selected", () => keyUpdates++);
- const unsubscribeAny = storage.onAnyUpdate(() => anyUpdates++);
storage.set("selected", "first");
assert.equal(keyUpdates, 1);
- assert.equal(anyUpdates, 1);
storage.set("other", "second");
assert.equal(keyUpdates, 1);
- assert.equal(anyUpdates, 2);
storage.clear();
assert.equal(keyUpdates, 2);
- assert.equal(anyUpdates, 3);
unsubscribeKey();
- unsubscribeAny();
storage.set("selected", "third");
assert.equal(keyUpdates, 2);
- assert.equal(anyUpdates, 3);
});
diff --git a/packages/web-util/src/utils/observable.ts b/packages/web-util/src/utils/observable.ts
@@ -1,17 +1,11 @@
export type ObservableMap<K, V> = Map<K, V> & {
- onAnyUpdate: (callback: () => void) => () => void;
onUpdate: (key: string, callback: () => void) => () => void;
};
function createObservableCallbacks() {
- const anyUpdateHandlers = new Set<() => void>();
const keyUpdateHandlers = new Map<string, Set<() => void>>();
return {
- onAnyUpdate(handler: () => void): () => void {
- anyUpdateHandlers.add(handler);
- return () => anyUpdateHandlers.delete(handler);
- },
onUpdate(key: string, handler: () => void): () => void {
const handlers = keyUpdateHandlers.get(key) ?? new Set<() => void>();
handlers.add(handler);
@@ -23,13 +17,11 @@ function createObservableCallbacks() {
},
notifyUpdate(key: string): void {
for (const handler of keyUpdateHandlers.get(key) ?? []) handler();
- for (const handler of anyUpdateHandlers) handler();
},
notifyClear(): void {
for (const handlers of keyUpdateHandlers.values()) {
for (const handler of handlers) handler();
}
- for (const handler of anyUpdateHandlers) handler();
},
};
}
@@ -40,7 +32,6 @@ export function memoryMap<T>(
): ObservableMap<string, T> {
const callbacks = createObservableCallbacks();
const theMemoryMap: ObservableMap<string, T> = {
- onAnyUpdate: callbacks.onAnyUpdate,
onUpdate: callbacks.onUpdate,
delete: (key: string) => {
const result = backend.delete(key);
@@ -148,18 +139,6 @@ export function localStorageMap(
}
const theLocalStorageMap: ObservableMap<string, string> = {
- onAnyUpdate: (handler) => {
- const eventWindow = typeof window === "undefined" ? undefined : window;
- function handleStorageEvent(event: StorageEvent) {
- if (storageEventMatches(event)) handler();
- }
- const unsubscribe = callbacks.onAnyUpdate(handler);
- eventWindow?.addEventListener("storage", handleStorageEvent);
- return () => {
- eventWindow?.removeEventListener("storage", handleStorageEvent);
- unsubscribe();
- };
- },
onUpdate: (key, handler) => {
function handleStorageEvent(ev: StorageEvent) {
if (storageEventMatches(ev) && (ev.key === null || ev.key === key)) {