123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- import ListenerHandler from "@/classes/ListenerHandler.class";
- export default class CustomWebSocketMock {
- dispatcher: ListenerHandler;
- url: string;
- data: {
- dispatch?: {
- [key: string]: (...args: any[]) => any;
- };
- progress?: {
- [key: string]: (...args: any[]) => any;
- };
- on?: {
- [key: string]: any;
- };
- };
- onDisconnectCbs: {
- temp: any[];
- persist: any[];
- };
- executeDispatch: boolean;
- constructor(url) {
- this.dispatcher = new ListenerHandler();
- this.url = url;
- this.data = {
- dispatch: {},
- progress: {},
- on: {}
- };
- this.onDisconnectCbs = {
- temp: [],
- persist: []
- };
- this.executeDispatch = true;
- }
- on(target, cb, options?) {
- const onData = this.data.on && this.data.on[target];
- this.dispatcher.addEventListener(
- `on.${target}`,
- event => cb(event.detail() || onData),
- options
- );
- }
- dispatch(target, ...args) {
- const lastArg = args[args.length - 1];
- const _args = args.slice(0, -1);
- const dispatchData = () =>
- this.data.dispatch &&
- typeof this.data.dispatch[target] === "function"
- ? this.data.dispatch[target](..._args)
- : undefined;
- const progressData = () =>
- this.data.progress &&
- typeof this.data.progress[target] === "function"
- ? this.data.progress[target](..._args)
- : undefined;
- if (typeof lastArg === "function") {
- if (this.executeDispatch && dispatchData()) lastArg(dispatchData());
- else if (!this.executeDispatch)
- this.dispatcher.addEventListener(
- `dispatch.${target}`,
- event => lastArg(event.detail(..._args) || dispatchData()),
- false
- );
- } else if (typeof lastArg === "object") {
- if (this.executeDispatch) {
- if (progressData())
- progressData().forEach(data => {
- lastArg.onProgress(data);
- });
- if (dispatchData()) lastArg.cb(dispatchData());
- } else {
- this.dispatcher.addEventListener(
- `progress.${target}`,
- event => {
- if (event.detail(..._args))
- lastArg.onProgress(event.detail(..._args));
- else if (progressData())
- progressData().forEach(data => {
- lastArg.onProgress(data);
- });
- },
- false
- );
- this.dispatcher.addEventListener(
- `dispatch.${target}`,
- event =>
- lastArg.cb(event.detail(..._args) || dispatchData()),
- false
- );
- }
- }
- }
- // eslint-disable-next-line class-methods-use-this
- onConnect(cb) {
- cb();
- }
- onDisconnect(...args) {
- if (args[0] === true) this.onDisconnectCbs.persist.push(args[1]);
- else this.onDisconnectCbs.temp.push(args[0]);
- this.dispatcher.addEventListener(
- "socket.disconnect",
- () => {
- this.onDisconnectCbs.temp.forEach(cb => cb());
- this.onDisconnectCbs.persist.forEach(cb => cb());
- },
- false
- );
- }
- clearCallbacks() {
- this.onDisconnectCbs.temp = [];
- }
- // eslint-disable-next-line class-methods-use-this
- destroyModalListeners() {}
- trigger(type, target, data?) {
- this.dispatcher.dispatchEvent(
- new CustomEvent(`${type}.${target}`, {
- detail: (...args) => {
- if (typeof data === "function") return data(...args);
- if (typeof data === "undefined") return undefined;
- return JSON.parse(JSON.stringify(data));
- }
- })
- );
- }
- }
|