Map.on() and improvements
This commit is contained in:
parent
43389b354a
commit
95955f70aa
3 changed files with 80 additions and 12 deletions
|
|
@ -17,6 +17,12 @@ const logger = require('../../config/log')
|
||||||
*/
|
*/
|
||||||
const pathToListeners = {}
|
const pathToListeners = {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maps a path to `map().on()` listeners
|
||||||
|
* @type {Record<string, Set<GunT.Listener>|undefined>}
|
||||||
|
*/
|
||||||
|
const pathToMapListeners = {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Path to pending puts. Oldest to newest
|
* Path to pending puts. Oldest to newest
|
||||||
* @type {Record<string, Smith.PendingPut[]?>}
|
* @type {Record<string, Smith.PendingPut[]?>}
|
||||||
|
|
@ -38,6 +44,17 @@ const handleMsg = msg => {
|
||||||
l(data, path.split('>')[path.split('>').length - 1])
|
l(data, path.split('>')[path.split('>').length - 1])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (msg.type === 'map.on') {
|
||||||
|
const { data, key, path } = msg
|
||||||
|
|
||||||
|
// eslint-disable-next-line no-multi-assign
|
||||||
|
const listeners =
|
||||||
|
pathToMapListeners[path] || (pathToMapListeners[path] = new Set())
|
||||||
|
|
||||||
|
for (const l of listeners) {
|
||||||
|
l(data, key)
|
||||||
|
}
|
||||||
|
}
|
||||||
if (msg.type === 'put') {
|
if (msg.type === 'put') {
|
||||||
const { ack, id, path } = msg
|
const { ack, id, path } = msg
|
||||||
|
|
||||||
|
|
@ -214,19 +231,33 @@ function createReplica(path, afterMap = false) {
|
||||||
},
|
},
|
||||||
on(cb) {
|
on(cb) {
|
||||||
listenersForThisRef.push(cb)
|
listenersForThisRef.push(cb)
|
||||||
|
if (afterMap) {
|
||||||
|
// eslint-disable-next-line no-multi-assign
|
||||||
|
const listeners =
|
||||||
|
pathToMapListeners[path] || (pathToMapListeners[path] = new Set())
|
||||||
|
|
||||||
// eslint-disable-next-line no-multi-assign
|
listeners.add(cb)
|
||||||
const listeners =
|
|
||||||
pathToListeners[path] || (pathToListeners[path] = new Set())
|
|
||||||
|
|
||||||
listeners.add(cb)
|
/** @type {Smith.SmithMsgMapOn} */
|
||||||
|
const msg = {
|
||||||
|
path,
|
||||||
|
type: 'map.on'
|
||||||
|
}
|
||||||
|
currentGun.send(msg)
|
||||||
|
} else {
|
||||||
|
// eslint-disable-next-line no-multi-assign
|
||||||
|
const listeners =
|
||||||
|
pathToListeners[path] || (pathToListeners[path] = new Set())
|
||||||
|
|
||||||
/** @type {Smith.SmithMsgOn} */
|
listeners.add(cb)
|
||||||
const msg = {
|
|
||||||
path,
|
/** @type {Smith.SmithMsgOn} */
|
||||||
type: 'on'
|
const msg = {
|
||||||
|
path,
|
||||||
|
type: 'on'
|
||||||
|
}
|
||||||
|
currentGun.send(msg)
|
||||||
}
|
}
|
||||||
currentGun.send(msg)
|
|
||||||
|
|
||||||
return this
|
return this
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,11 @@ namespace Smith {
|
||||||
type: 'on'
|
type: 'on'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface SmithMsgMapOn {
|
||||||
|
path: string
|
||||||
|
type: 'map.on'
|
||||||
|
}
|
||||||
|
|
||||||
export interface SmithMsgPut {
|
export interface SmithMsgPut {
|
||||||
id: string
|
id: string
|
||||||
data: GunT.ValidDataValue
|
data: GunT.ValidDataValue
|
||||||
|
|
@ -36,6 +41,7 @@ namespace Smith {
|
||||||
| SmithMsgInit
|
| SmithMsgInit
|
||||||
| SmithMsgAuth
|
| SmithMsgAuth
|
||||||
| SmithMsgOn
|
| SmithMsgOn
|
||||||
|
| SmithMsgMapOn
|
||||||
| SmithMsgPut
|
| SmithMsgPut
|
||||||
| BatchSmithMsg
|
| BatchSmithMsg
|
||||||
|
|
||||||
|
|
@ -47,11 +53,18 @@ namespace Smith {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface GunMsgOn {
|
export interface GunMsgOn {
|
||||||
data: any
|
data: GunT.ListenerData
|
||||||
path: string
|
path: string
|
||||||
type: 'on'
|
type: 'on'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface GunMsgMapOn {
|
||||||
|
data: GunT.ListenerData
|
||||||
|
path: string
|
||||||
|
key: string
|
||||||
|
type: 'map.on'
|
||||||
|
}
|
||||||
|
|
||||||
export interface GunMsgPut {
|
export interface GunMsgPut {
|
||||||
ack: GunT.Ack
|
ack: GunT.Ack
|
||||||
id: string
|
id: string
|
||||||
|
|
@ -59,5 +72,5 @@ namespace Smith {
|
||||||
type: 'put'
|
type: 'put'
|
||||||
}
|
}
|
||||||
|
|
||||||
export type GunMsg = GunMsgAuth | GunMsgOn | GunMsgPut
|
export type GunMsg = GunMsgAuth | GunMsgOn | GunMsgMapOn | GunMsgPut
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -104,7 +104,7 @@ const handleMsg = msg => {
|
||||||
for (const key of keys) {
|
for (const key of keys) {
|
||||||
node = node.get(key)
|
node = node.get(key)
|
||||||
}
|
}
|
||||||
node.on((data, key) => {
|
node.on(data => {
|
||||||
/** @type {Smith.GunMsgOn} */
|
/** @type {Smith.GunMsgOn} */
|
||||||
const res = {
|
const res = {
|
||||||
data,
|
data,
|
||||||
|
|
@ -114,6 +114,30 @@ const handleMsg = msg => {
|
||||||
sendMsg(res)
|
sendMsg(res)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
if (msg.type === 'map.on') {
|
||||||
|
const [root, ...keys] = msg.path.split('>')
|
||||||
|
|
||||||
|
/** @type {GunT.GUNNode} */
|
||||||
|
let node =
|
||||||
|
{
|
||||||
|
$root: gun,
|
||||||
|
$user: user
|
||||||
|
}[root] || gun.user(root)
|
||||||
|
|
||||||
|
for (const key of keys) {
|
||||||
|
node = node.get(key)
|
||||||
|
}
|
||||||
|
node.map().on((data, key) => {
|
||||||
|
/** @type {Smith.GunMsgMapOn} */
|
||||||
|
const res = {
|
||||||
|
data,
|
||||||
|
key,
|
||||||
|
path: msg.path,
|
||||||
|
type: 'map.on'
|
||||||
|
}
|
||||||
|
sendMsg(res)
|
||||||
|
})
|
||||||
|
}
|
||||||
if (msg.type === 'put') {
|
if (msg.type === 'put') {
|
||||||
const [root, ...keys] = msg.path.split('>')
|
const [root, ...keys] = msg.path.split('>')
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue