Flush puts from last instance

This commit is contained in:
Daniel Lugo 2021-09-13 14:13:18 -04:00
parent 72e68d8462
commit f43748c96c
4 changed files with 69 additions and 5 deletions

View file

@ -5,16 +5,15 @@
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"cSpell.words": [
"Epub",
"falsey",
"acked",
"endregion",
"epriv",
"Epub",
"falsey",
"GUNRPC",
"ISEA",
"PUBKEY",
"Reqs",
"uuidv",
"epriv"
"uuidv"
]
}

View file

@ -8,10 +8,13 @@
/// <reference path="GunT.ts" />
const RealGun = require('gun')
const uuid = require('uuid/v1')
const mapValues = require('lodash/mapValues')
const { fork } = require('child_process')
const logger = require('../../config/log')
const { mergePuts } = require('./misc')
const gunUUID = () => {
const RG = /** @type {any} */ (RealGun)
if (typeof RG.text === 'object' && typeof RG.text.random === 'function') {
@ -103,6 +106,21 @@ const handleMsg = msg => {
})
}
}
if (msg.type === 'multiPut') {
const { ack, ids, path } = msg
const pendingPutsForPath = pendingPuts[path] || (pendingPuts[path] = [])
const ackedPuts = pendingPutsForPath.filter(pp => ids.includes(pp.id))
pendingPuts[path] = pendingPuts[path].filter(pp => !ids.includes(pp.id))
ackedPuts.forEach(pp => {
if (pp.cb) {
pp.cb(ack)
}
})
}
}
/** @type {ReturnType<typeof fork>} */
@ -168,7 +186,28 @@ const autoAuth = () => {
return auth(lastAlias, lastPass)
}
const flushPendingPuts = () => {}
const flushPendingPuts = () => {
const ids = mapValues(pendingPuts, pendingPutsForPath =>
pendingPutsForPath.map(pp => pp.id)
)
const writes = mapValues(pendingPuts, pendingPutsForPath =>
pendingPutsForPath.map(pp => pp.data)
)
const finalWrites = mapValues(writes, writesForPath =>
mergePuts(writesForPath)
)
const messages = Object.entries(ids).map(([path, ids]) => {
/** @type {Smith.SmithMsgMultiPut} */
const msg = {
data: finalWrites[path],
ids,
path,
type: 'multiPut'
}
return msg
})
currentGun.send(messages)
}
const forge = () => {
if (currentGun) {

View file

@ -97,6 +97,7 @@ namespace Smith {
export interface GunMsgMultiPut {
ack: GunT.Ack
ids: string[]
path: string
type: 'multiPut'
}

View file

@ -189,6 +189,31 @@ const handleMsg = msg => {
sendMsg(reply)
})
}
if (msg.type === 'multiPut') {
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.put(msg.data, ack => {
/** @type {Smith.GunMsgMultiPut} */
const reply = {
ack: {
err: ack.err
},
ids: msg.ids,
type: 'multiPut'
}
sendMsg(reply)
})
}
}
process.on('message', handleMsg)