{"version":3,"file":"localStorageBroadcastChannel.mjs","names":[],"sources":["../../src/localStorageBroadcastChannel.ts"],"sourcesContent":["import { deprecated } from './deprecated';\n\ntype Listener = (e: MessageEvent) => void;\n\nconst KEY_PREFIX = '__lsbc__';\n\n/**\n * @deprecated This class will be completely removed in the next major version.\n * Use the native BroadcastChannel API directly instead.\n */\nexport class LocalStorageBroadcastChannel {\n private readonly eventTarget = window;\n private readonly channelKey: string;\n\n constructor(name: string) {\n deprecated('LocalStorageBroadcastChannel', 'Use the native BroadcastChannel API directly instead.');\n this.channelKey = KEY_PREFIX + name;\n this.setupLocalStorageListener();\n }\n\n public postMessage = (data: E): void => {\n if (typeof window === 'undefined') {\n // Silently do nothing\n return;\n }\n\n try {\n window.localStorage.setItem(this.channelKey, JSON.stringify(data));\n window.localStorage.removeItem(this.channelKey);\n } catch {\n // Silently do nothing\n }\n };\n\n public addEventListener = (eventName: 'message', listener: Listener): void => {\n this.eventTarget.addEventListener(this.prefixEventName(eventName), e => {\n listener(e as MessageEvent);\n });\n };\n\n private setupLocalStorageListener = () => {\n const notifyListeners = (e: StorageEvent) => {\n if (e.key !== this.channelKey || !e.newValue) {\n return;\n }\n\n try {\n const data = JSON.parse(e.newValue || '');\n const event = new MessageEvent(this.prefixEventName('message'), {\n data,\n });\n this.eventTarget.dispatchEvent(event);\n } catch {\n //\n }\n };\n\n window.addEventListener('storage', notifyListeners);\n };\n\n private prefixEventName(eventName: string): string {\n return this.channelKey + eventName;\n }\n}\n"],"mappings":";;;;AAIA,MAAM,aAAa;;;;;AAMnB,IAAa,+BAAb,MAA6C;CAC3C,AAAiB,cAAc;CAC/B,AAAiB;CAEjB,YAAY,MAAc;AACxB,aAAW,gCAAgC,wDAAwD;AACnG,OAAK,aAAa,aAAa;AAC/B,OAAK,2BAA2B;;CAGlC,AAAO,eAAe,SAAkB;AACtC,MAAI,OAAO,WAAW,YAEpB;AAGF,MAAI;AACF,UAAO,aAAa,QAAQ,KAAK,YAAY,KAAK,UAAU,KAAK,CAAC;AAClE,UAAO,aAAa,WAAW,KAAK,WAAW;UACzC;;CAKV,AAAO,oBAAoB,WAAsB,aAAgC;AAC/E,OAAK,YAAY,iBAAiB,KAAK,gBAAgB,UAAU,GAAE,MAAK;AACtE,YAAS,EAAkB;IAC3B;;CAGJ,AAAQ,kCAAkC;EACxC,MAAM,mBAAmB,MAAoB;AAC3C,OAAI,EAAE,QAAQ,KAAK,cAAc,CAAC,EAAE,SAClC;AAGF,OAAI;IACF,MAAM,OAAO,KAAK,MAAM,EAAE,YAAY,GAAG;IACzC,MAAM,QAAQ,IAAI,aAAa,KAAK,gBAAgB,UAAU,EAAE,EAC9D,MACD,CAAC;AACF,SAAK,YAAY,cAAc,MAAM;WAC/B;;AAKV,SAAO,iBAAiB,WAAW,gBAAgB;;CAGrD,AAAQ,gBAAgB,WAA2B;AACjD,SAAO,KAAK,aAAa"}