From b38fb967a03e66471ba30a413e1804f71de35c33 Mon Sep 17 00:00:00 2001 From: satori Date: Sun, 30 Jan 2022 02:59:29 +0800 Subject: [PATCH] version --- demo.js | 42 +++++++++++++++++++++++++++++++++++++++++- index.js | 24 ++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/demo.js b/demo.js index 2058559..80873be 100644 --- a/demo.js +++ b/demo.js @@ -17,6 +17,37 @@ fm.btoa("0002", (data) => { console.log(data) }) +console.log("----------------") +fm.delete("频道一号", "0003") +// 操作所有订阅了频道一的用户 +fm.atob("频道一号", (data) => { + console.log(data) +}) + +console.log("----------------") +fm.adelete("0001") +// 操作所有订阅了频道一的用户 +fm.atob("频道一号", (data) => { + console.log(data) +}) + +fm.btoa("0002", (data) => { + console.log(data) +}) + +console.log("----------------") +fm.bdelete("频道一号") +// 操作所有订阅了频道一的用户 +fm.atob("频道一号", (data) => { + console.log(data) +}) + +fm.btoa("0002", (data) => { + console.log(data) +}) + + + // console print: // // 0001 @@ -24,4 +55,13 @@ fm.btoa("0002", (data) => { // 0003 // 0004 // 频道一号 -// \ No newline at end of file +// ---------------- +// 0001 +// 0002 +// 0004 +// ---------------- +// 0002 +// 0004 +// 频道一号 +// ---------------- +// diff --git a/index.js b/index.js index b8f3edd..8073343 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,7 @@ export default class interrelated { let channel = this.channels.get(fid) || new Map() if (!channel.size) this.channels.set(fid, channel) channel.set(uid, true) + let user = this.users.get(uid) || new Map() if (!user.size) this.users.set(uid, user) user.set(fid, true) @@ -18,8 +19,11 @@ export default class interrelated { delete(fid, uid) { let channel = this.channels.get(fid) if (channel && channel.size) channel.delete(uid) + if (channel && channel.size === 0) this.channels.delete(fid) + let user = this.users.get(uid) if (user && user.size) user.delete(fid) + if (user && user.size === 0) this.users.delete(uid) } // 读取类型 A 关联的 B @@ -42,6 +46,26 @@ export default class interrelated { } } + // 移除 A 中所有指定数据 + adelete(uid) { + let user = this.users.get(uid) + if (user && user.size) user.forEach((value, key) => { + let channel = this.channels.get(key) + if (channel && channel.size) channel.delete(uid) + }) + this.users.delete(uid) + } + + // 移除 B 中所有指定数据 + bdelete(fid) { + let channel = this.channels.get(fid) + if (channel && channel.size) channel.forEach((value, key) => { + let user = this.users.get(key) + if (user && user.size) user.delete(fid) + }) + this.channels.delete(fid) + } + // 读取所有 a aall(uid, callback) { this.channels.forEach((value, key) => {