This commit is contained in:
satori 2022-01-30 01:14:45 +08:00
parent a091caf890
commit 66aaf64c81
4 changed files with 138 additions and 0 deletions

View File

@ -1,2 +1,35 @@
# interrelated
js 数据双向关联
```javascript
import fmhub from './index.js'
const fm = new fmhub()
const users = ["0001", '0002', "0003", "0004"]
// 一组用户全部订阅频道一
users.forEach((item) => {
fm.set("频道一号", item)
})
// 操作所有订阅了频道一的用户
fm.atob("频道一号", (data) => {
console.log(data)
})
fm.btoa("0002", (data) => {
console.log(data)
})
// console print:
//
// 0001
// 0002
// 0003
// 0004
// 频道一号
//
```

27
demo.js Normal file
View File

@ -0,0 +1,27 @@
import fmhub from './index.js'
const fm = new fmhub()
const users = ["0001", '0002', "0003", "0004"]
// 一组用户全部订阅频道一
users.forEach((item) => {
fm.set("频道一号", item)
})
// 操作所有订阅了频道一的用户
fm.atob("频道一号", (data) => {
console.log(data)
})
fm.btoa("0002", (data) => {
console.log(data)
})
// console print:
//
// 0001
// 0002
// 0003
// 0004
// 频道一号
//

58
index.js Normal file
View File

@ -0,0 +1,58 @@
export default class interrelated {
constructor() {
this.channels = new Map()
this.users = new Map()
}
// 将两种类型的不同信息相关连
set(fid, uid) {
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)
}
// 取消关联
delete(fid, uid) {
let channel = this.channels.get(fid)
if (channel && channel.size) channel.delete(uid)
let user = this.users.get(uid)
if (user && user.size) user.delete(fid)
}
// 读取类型 A 关联的 B
atob(uid, callback) {
let channel = this.channels.get(uid)
if (channel && channel.size) {
channel.forEach((value, key) => {
callback(key)
})
}
}
// 读取类型 B 关联的 A
btoa(fid, callback) {
let user = this.users.get(fid)
if (user && user.size) {
user.forEach((value, key) => {
callback(key)
})
}
}
// 读取所有 a
aall(uid, callback) {
this.channels.forEach((value, key) => {
callback(key)
})
}
// 读取所有 b
ball(fid, callback) {
this.users.forEach((value, key) => {
callback(key)
})
}
}

20
package.json Normal file
View File

@ -0,0 +1,20 @@
{
"name": "interrelated",
"version": "1.0.0",
"description": "js 数据双向关联",
"main": "index.js",
"type": "module",
"scripts": {
"test": "demo.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/InvisibleFuture/interrelated.git"
},
"author": "satori.love",
"license": "MIT",
"bugs": {
"url": "https://github.com/InvisibleFuture/interrelated/issues"
},
"homepage": "https://github.com/InvisibleFuture/interrelated#readme"
}