From f43748c96c249fc880964510855a904ab3f6954b Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Mon, 13 Sep 2021 14:13:18 -0400 Subject: [PATCH] Flush puts from last instance --- .vscode/settings.json | 7 +++---- utils/GunSmith/GunSmith.js | 41 +++++++++++++++++++++++++++++++++++++- utils/GunSmith/Smith.ts | 1 + utils/GunSmith/gun.js | 25 +++++++++++++++++++++++ 4 files changed, 69 insertions(+), 5 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 2e611532..65bb6134 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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" ] } diff --git a/utils/GunSmith/GunSmith.js b/utils/GunSmith/GunSmith.js index 276c5ce5..d8d55531 100644 --- a/utils/GunSmith/GunSmith.js +++ b/utils/GunSmith/GunSmith.js @@ -8,10 +8,13 @@ /// 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} */ @@ -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) { diff --git a/utils/GunSmith/Smith.ts b/utils/GunSmith/Smith.ts index b14fed6f..2f8825b6 100644 --- a/utils/GunSmith/Smith.ts +++ b/utils/GunSmith/Smith.ts @@ -97,6 +97,7 @@ namespace Smith { export interface GunMsgMultiPut { ack: GunT.Ack ids: string[] + path: string type: 'multiPut' } diff --git a/utils/GunSmith/gun.js b/utils/GunSmith/gun.js index bea2a118..95d7a459 100644 --- a/utils/GunSmith/gun.js +++ b/utils/GunSmith/gun.js @@ -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)