diff --git a/README.md b/README.md index 824df76..85c7275 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,9 @@ channel channel channel: 细流 part-server: 调谐, 从不同服务器请求资源分片 webrtc://用户@域名:端口/信道标识/资源ID +封包格式 +资源ID 分片信息(位置) 分片数据 + 1. 每个节点都公开持有的资源列表, 和连接的节点列表 2. 每当资源变动时告知所有连接的节点 3. 与节点创建多个RTC时, 不发送多份, 以ID为准, id随机生成给不同机器, 无法通过ID锁定其它机器 diff --git a/public/entanglement.js b/public/entanglement.js index d45d169..a004176 100644 --- a/public/entanglement.js +++ b/public/entanglement.js @@ -162,7 +162,7 @@ export default class Entanglement { // 数据被修改时触发 set(name, data) { // 递归创建代理对象 - const createDeepProxy = (obj, path = []) => { + const useProxy = (obj, path = []) => { const proxy = new Proxy(obj, { set: (target, key, value) => { if (!Array.isArray(target) || key !== 'length') { @@ -177,12 +177,12 @@ export default class Entanglement { }) Object.keys(obj).forEach(key => { if (typeof obj[key] === 'object') { - obj[key] = createDeepProxy(obj[key], [...path, key]) + obj[key] = useProxy(obj[key], [...path, key]) } }) return proxy } - return Reflect.set(this.store, name, createDeepProxy(data)) + return Reflect.set(this.store, name, useProxy(data)) } // 读取一个通道 diff --git a/public/test.html b/public/test.html index faa9b1e..756584a 100644 --- a/public/test.html +++ b/public/test.html @@ -12,72 +12,82 @@

同步

diff --git a/public/weigets.js b/public/weigets.js index 2cdcdd0..24339aa 100644 --- a/public/weigets.js +++ b/public/weigets.js @@ -97,18 +97,65 @@ export function Dialog(options) { } // 深度代理, 用于监听数据的变化 -export function useProxy(obj, callback) { - return new Proxy(obj, { - get(target, key) { - if (typeof target[key] === 'object' && target[key] !== null) { - return useProxy(target[key], callback) +export function useProxy(data, callback, path = []) { + if (Array.isArray(data)) { + const array = class extends Array { + constructor(args) { + super(args) + } + push(args) { + super.push(args) + console.log('push', args) + this.__notify() + } + pop(...args) { + const result = super.pop(...args) + console.log('pop') + this.__notify() + return result + } + shift(...args) { + const result = super.shift(...args) + this.__notify() + return result + } + unshift(...args) { + super.unshift(...args) + this.__notify() + } + splice(...args) { + super.splice(...args) + this.__notify() + } + deleteProperty(...args) { + console.log('deleteProperty', ...args) + super.deleteProperty(...args) + this.__notify() + } + __notify() { + if (callback) callback() } - return target[key] - }, - set(target, key, value) { - target[key] = value - callback(target, key, value) - return true } - }) + //console.log('为数组每项递归创建代理') + data.forEach((item, index) => { + data[index] = useProxy(item, callback, [...path, index]) + }) + return new array(...data) + } + if (typeof data === 'object' && data !== null) { + //console.log('为对象属性递归创建代理') + Object.keys(data).forEach(key => { + if (typeof data[key] === 'object' && data[key] !== null) { + data[key] = useProxy(data[key], callback, [...path, key]) + } + }) + return new Proxy(data, { + deleteProperty(target, key) { + console.log('deleteProperty', key) + return delete target[key] + } + }) + } + console.log('为其它类型直接返回') + return data }