UNPKG

@dooboostore/dom-render

Version:
199 lines (198 loc) 6.14 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var Messenger_exports = {}; __export(Messenger_exports, { Channel: () => Channel, ChannelSubscriber: () => ChannelSubscriber, ChannelSubscription: () => ChannelSubscription, Messenger: () => Messenger }); module.exports = __toCommonJS(Messenger_exports); var import_Types = require("../types/Types"); var CallBackType = /* @__PURE__ */ ((CallBackType2) => { CallBackType2[CallBackType2["FILTER"] = 0] = "FILTER"; CallBackType2[CallBackType2["MAP"] = 1] = "MAP"; CallBackType2[CallBackType2["SUBSCRIBE"] = 2] = "SUBSCRIBE"; return CallBackType2; })(CallBackType || {}); class ChannelSubscription { constructor(channel, subscriber) { this.channel = channel; this.subscriber = subscriber; } unsubscribe() { this.subscriber.unsubscribe(); } } class ChannelSubscriber { constructor(channel) { this.channel = channel; this.callbacks = []; } exeCallback(data, metaData) { for (const callback of this.callbacks) { if (callback.type === 0 /* FILTER */ && !callback.callback(data, metaData)) { break; } else if (callback.type === 1 /* MAP */) { data = callback.callback(data, metaData); } else if (callback.type === 2 /* SUBSCRIBE */) { data = callback.callback(data, metaData); break; } } return data; } // chaining point filter(callback) { this.callbacks.push({ type: 0 /* FILTER */, callback }); return this; } map(callback) { this.callbacks.push({ type: 1 /* MAP */, callback }); return this; } subscribe(callback) { this.callbacks.push({ type: 2 /* SUBSCRIBE */, callback }); this.channel.subscribers.add(this); return new ChannelSubscription(this.channel, this); } unsubscribe() { this.channel.subscribers.delete(this); } deleteSubscriber() { this.unsubscribe(); this.channel.subscribers.delete(this); } } class Channel { constructor(messenger, obj, key) { this.messenger = messenger; this.obj = obj; this.key = key; this.subscribers = /* @__PURE__ */ new Set(); } publish(key, data, action) { const rtns = []; this.messenger.getChannels(key)?.forEach((it) => { try { it.subscribers.forEach((its) => { const rdata = its.exeCallback(data, { channel: this, action }); rtns.push({ channel: it, data: rdata }); }); } catch (e) { console.error(e); } }); return rtns; } allPublish(data, action) { const rtns = []; this.messenger.getAllChannelKeys().forEach((it) => { rtns.push(this.publish(it, data)); }); return rtns.flat(); } // string point filter(filterF) { const subscriber = new ChannelSubscriber(this); subscriber.filter(filterF); return subscriber; } map(filterF) { const subscriber = new ChannelSubscriber(this); subscriber.map(filterF); return subscriber; } subscribe(subscribeCallback) { const subscriber = new ChannelSubscriber(this); return subscriber.subscribe(subscribeCallback); } deleteChannel() { this.messenger.deleteChannel(this); } } const _Messenger = class _Messenger { constructor(config) { this.config = config; this.channels = /* @__PURE__ */ new Set(); this.config.window.addEventListener(_Messenger.EVENT_PUBLISH_KEY, (e) => { const detail = e.detail; const rtns = []; this.getChannels(detail.key)?.forEach((it) => { try { it.subscribers.forEach((its) => { const rdata = its.exeCallback(detail.data, { action: detail.action }); rtns.push({ channel: it, data: rdata }); }); } catch (e2) { console.error(e2); } }); detail.result?.(rtns); }); this.config.window.addEventListener(_Messenger.EVENT_SUBSCRIBE_KEY, (e) => { const detail = e.detail; const channel = this.createChannel(detail.obj, detail.key); detail.init(channel, channel.subscribe(detail.subscribe)); }); } static publish(window, detail) { window.dispatchEvent(new CustomEvent(_Messenger.EVENT_PUBLISH_KEY, { detail })); } static subscribe(window, detail) { window.dispatchEvent(new CustomEvent(_Messenger.EVENT_SUBSCRIBE_KEY, { detail })); } createChannel(obj, key = obj.constructor.name) { const channel = import_Types.DomRenderFinalProxy.final(new Channel(this, obj, key)); this.channels.add(channel); return channel; } deleteChannel(channel) { this.channels.delete(channel); } deleteChannelFromObj(obj) { if (obj) { this.channels.forEach((it) => { if (it.obj === obj) { this.deleteChannel(it); } }); } } addChannel(channel) { this.channels.add(channel); } getChannels(key) { if (typeof key === "object") { key = key.constructor.name; } else if (typeof key === "function") { key = key.name; } return Array.from(this.channels.values()).filter((it) => it.key === key); } getAllChannels() { return Array.from(this.channels.values()); } getAllChannelKeys() { return Array.from(this.channels.values()).map((it) => it.key); } }; _Messenger.EVENT_PUBLISH_KEY = "domRenderMessenger_publish"; _Messenger.EVENT_SUBSCRIBE_KEY = "domRenderMessenger_subscribe"; let Messenger = _Messenger; //# sourceMappingURL=Messenger.js.map