This commit is contained in:
satori 2022-01-30 02:59:29 +08:00
parent 66aaf64c81
commit b38fb967a0
2 changed files with 65 additions and 1 deletions

42
demo.js
View File

@ -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
// 频道一号
//
// ----------------
// 0001
// 0002
// 0004
// ----------------
// 0002
// 0004
// 频道一号
// ----------------
//

View File

@ -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) => {