@clerk/shared
Version:
Internal package utils used by the Clerk SDKs
47 lines (45 loc) • 1.49 kB
JavaScript
import { deprecated } from "./deprecated.mjs";
//#region src/localStorageBroadcastChannel.ts
const KEY_PREFIX = "__lsbc__";
/**
* @deprecated This class will be completely removed in the next major version.
* Use the native BroadcastChannel API directly instead.
*/
var LocalStorageBroadcastChannel = class {
eventTarget = window;
channelKey;
constructor(name) {
deprecated("LocalStorageBroadcastChannel", "Use the native BroadcastChannel API directly instead.");
this.channelKey = KEY_PREFIX + name;
this.setupLocalStorageListener();
}
postMessage = (data) => {
if (typeof window === "undefined") return;
try {
window.localStorage.setItem(this.channelKey, JSON.stringify(data));
window.localStorage.removeItem(this.channelKey);
} catch {}
};
addEventListener = (eventName, listener) => {
this.eventTarget.addEventListener(this.prefixEventName(eventName), (e) => {
listener(e);
});
};
setupLocalStorageListener = () => {
const notifyListeners = (e) => {
if (e.key !== this.channelKey || !e.newValue) return;
try {
const data = JSON.parse(e.newValue || "");
const event = new MessageEvent(this.prefixEventName("message"), { data });
this.eventTarget.dispatchEvent(event);
} catch {}
};
window.addEventListener("storage", notifyListeners);
};
prefixEventName(eventName) {
return this.channelKey + eventName;
}
};
//#endregion
export { LocalStorageBroadcastChannel };
//# sourceMappingURL=localStorageBroadcastChannel.mjs.map