@dooboostore/dom-render
Version:
html view template engine
185 lines • 6.19 kB
JavaScript
import { DomRenderFinalProxy } from '../types/Types';
var CallBackType;
(function (CallBackType) {
CallBackType[CallBackType["FILTER"] = 0] = "FILTER";
CallBackType[CallBackType["MAP"] = 1] = "MAP";
CallBackType[CallBackType["SUBSCRIBE"] = 2] = "SUBSCRIBE";
})(CallBackType || (CallBackType = {}));
export class ChannelSubscription {
constructor(channel, subscriber) {
this.channel = channel;
this.subscriber = subscriber;
}
unsubscribe() {
this.subscriber.unsubscribe();
}
}
export class ChannelSubscriber {
constructor(channel) {
this.channel = channel;
this.callbacks = [];
}
exeCallback(data, metaData) {
for (const callback of this.callbacks) {
if (callback.type === CallBackType.FILTER && !callback.callback(data, metaData)) {
break;
}
else if (callback.type === CallBackType.MAP) {
data = callback.callback(data, metaData);
}
else if (callback.type === CallBackType.SUBSCRIBE) {
data = callback.callback(data, metaData);
break;
}
}
return data;
}
// chaining point
filter(callback) {
this.callbacks.push({ type: CallBackType.FILTER, callback });
return this;
}
map(callback) {
this.callbacks.push({ type: CallBackType.MAP, callback });
return this;
}
subscribe(callback) {
this.callbacks.push({ type: CallBackType.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);
}
}
export class Channel {
constructor(messenger, obj, key) {
this.messenger = messenger;
this.obj = obj;
this.key = key;
this.subscribers = new Set();
}
publish(key, data, action) {
var _a;
const rtns = [];
(_a = this.messenger.getChannels(key)) === null || _a === void 0 ? void 0 : _a.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);
}
;
}
export class Messenger {
constructor(config) {
this.config = config;
this.channels = new Set();
this.config.window.addEventListener(Messenger.EVENT_PUBLISH_KEY, (e) => {
var _a, _b;
const detail = e.detail;
// console.log('--->', detail)
const rtns = [];
(_a = this.getChannels(detail.key)) === null || _a === void 0 ? void 0 : _a.forEach(it => {
try {
it.subscribers.forEach(its => {
const rdata = its.exeCallback(detail.data, { action: detail.action });
rtns.push({ channel: it, data: rdata });
});
}
catch (e) {
console.error(e);
}
});
(_b = detail.result) === null || _b === void 0 ? void 0 : _b.call(detail, rtns);
});
this.config.window.addEventListener(Messenger.EVENT_SUBSCRIBE_KEY, (e) => {
const detail = e.detail;
// console.log('--->', 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 = DomRenderFinalProxy.final(new Channel(this, obj, key));
this.channels.add(channel);
// this.channels.get(key) ? this.channels.get(key)!.push(channel) : this.channels.set(key, [channel]);
return channel;
}
deleteChannel(channel) {
this.channels.delete(channel);
}
deleteChannelFromObj(obj) {
if (obj) {
this.channels.forEach(it => {
if (it.obj === obj) {
// console.log('dddddddddddd', 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';
//# sourceMappingURL=Messenger.js.map