|
| 1 | +export const hook = (vm, target, func) => { |
| 2 | + const _hookTarget = vm[target]; |
| 3 | + const hookFunc = (...args) => { |
| 4 | + func(vm, ...args); |
| 5 | + return _hookTarget.apply(vm, args) |
| 6 | + } |
| 7 | + vm[target] = hookFunc; |
| 8 | +} |
| 9 | + |
| 10 | +export function doSomething(...args) { |
| 11 | + console.log("my hook", args) |
| 12 | +} |
| 13 | + |
| 14 | +const foo = { |
| 15 | + bar(...args) { |
| 16 | + console.log('bar', args) |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +hook(foo, 'bar', doSomething); |
| 21 | +let params = new Array(40).fill({ a: 1 }) |
| 22 | +foo.bar(...params) |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +const makeCaller = (config) => { return new socketCaller(config) } |
| 27 | + |
| 28 | +let client = null; |
| 29 | +function factory(config) { |
| 30 | + if (client) { |
| 31 | + return client |
| 32 | + } |
| 33 | + client = makeCaller(config); |
| 34 | + return client; |
| 35 | +} |
| 36 | + |
| 37 | +var Client = { |
| 38 | + timer: null, |
| 39 | + ready: false, |
| 40 | + |
| 41 | + init() { |
| 42 | + return new Promise((resolve, reject) => { |
| 43 | + try { |
| 44 | + setTimeout(() => { |
| 45 | + this.ready = true; |
| 46 | + resolve() |
| 47 | + }, 30) |
| 48 | + } catch (error) { |
| 49 | + reject(error) |
| 50 | + } |
| 51 | + }) |
| 52 | + }, |
| 53 | + |
| 54 | + activated() { |
| 55 | + this.timer = setInterval(() => { |
| 56 | + this.receiveMsg({ topic: "alarm", body: { msg: "这是一条消息" } }) |
| 57 | + }, 1000); |
| 58 | + }, |
| 59 | + |
| 60 | + receiveMsg(msg) { |
| 61 | + this.onmessage(msg); |
| 62 | + }, |
| 63 | + |
| 64 | + onmessage() { }, |
| 65 | + |
| 66 | + destroy() { |
| 67 | + this.ready = false; |
| 68 | + clearInterval(this.timer); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +class socketCaller { |
| 73 | + options = null; |
| 74 | + client = {}; |
| 75 | + config = {}; |
| 76 | + idKey = "_uid" |
| 77 | + |
| 78 | + constructor(config) { |
| 79 | + this.config = config; |
| 80 | + this.subscribeMap = {} |
| 81 | + this.client = Object.create(Client) |
| 82 | + } |
| 83 | + |
| 84 | + init(options) { |
| 85 | + const { idKey } = options; |
| 86 | + if (idKey) { this.idKey = idKey } |
| 87 | + |
| 88 | + this.options = options; |
| 89 | + this.client.init().then(() => { |
| 90 | + this.client.activated() |
| 91 | + }) |
| 92 | + this.onmessage(); |
| 93 | + } |
| 94 | + |
| 95 | + subscribe(topic, vm, callback, hookName) { |
| 96 | + if (!topic) { |
| 97 | + throw new Error('topic is required'); |
| 98 | + } |
| 99 | + |
| 100 | + if (!vm[this.idKey]) { |
| 101 | + throw new Error('id is required'); |
| 102 | + } |
| 103 | + this.subscribeMap[topic] = this.subscribeMap[topic] || []; |
| 104 | + this.subscribeMap[topic].push({ instance: vm, [this.idKey]: vm[this.idKey], callback }); |
| 105 | + |
| 106 | + let _hookName = hookName || "$destroy"; |
| 107 | + const funchook = () => { |
| 108 | + return this.unsubscribe(topic, vm) |
| 109 | + } |
| 110 | + if (vm[_hookName] && typeof vm[_hookName] === "function") { |
| 111 | + hook(vm, _hookName, funchook) |
| 112 | + } |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | + unsubscribe(topic, vm) { |
| 117 | + if (!this.subscribeMap[topic]) { |
| 118 | + throw new Error(`the ${topic} Topic is missed OR Client is destroy, please check the Topic OR the Client is existed`); |
| 119 | + } |
| 120 | + this.subscribeMap[topic] = this.subscribeMap[topic].filter((_) => { |
| 121 | + return _[this.idKey] !== vm[this.idKey] |
| 122 | + }) |
| 123 | + } |
| 124 | + |
| 125 | + onmessage() { |
| 126 | + this.client.onmessage = (msg) => { |
| 127 | + const { topic } = msg; |
| 128 | + if (this.subscribeMap[topic]) { |
| 129 | + this.subscribeMap[topic].forEach(m => { m.callback(msg) }) |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + destroy() { |
| 135 | + Object.keys(this.subscribeMap).forEach((topic) => { if (this.subscribeMap[topic]) this.subscribeMap[topic].length = 0; }); |
| 136 | + this.subscribeMap = {}; |
| 137 | + this.client.destroy(); |
| 138 | + } |
| 139 | +} |
| 140 | +const config = { url: "my url" }; |
| 141 | +const options = { token: 'this is token', idKey: "_uid" } |
| 142 | + |
| 143 | +const caller = factory(config); |
| 144 | +caller.init(options) |
| 145 | + |
| 146 | +const topic = 'alarm'; |
| 147 | +const vm = { |
| 148 | + _uid: 1, |
| 149 | + $destroy() { |
| 150 | + console.log("我要摧毁了") |
| 151 | + } |
| 152 | +}; |
| 153 | +function callback(msg) { |
| 154 | + console.log("订阅消息", msg) |
| 155 | + console.log("订阅消息-消息体", msg.body); |
| 156 | +} |
| 157 | + |
| 158 | +caller.subscribe(topic, vm, callback); |
| 159 | + |
| 160 | +setTimeout(() => { |
| 161 | + vm.$destroy(); |
| 162 | +}, 2000) |
| 163 | + |
| 164 | +setTimeout(() => { |
| 165 | + caller.unsubscribe("alarm", vm); |
| 166 | +}, 1000) |
| 167 | + |
| 168 | +setTimeout(() => { |
| 169 | + caller.destroy(); |
| 170 | +}, 3100) |
0 commit comments