From ec56fdfcfdbce8ac70b89530a2f102e7d9d96503 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sun, 12 Sep 2021 14:50:48 -0400 Subject: [PATCH] Further improvements --- utils/GunSmith/GunSmith.js | 56 +++++++++++++++++++------------------- utils/GunSmith/gun.js | 34 ++++++++++++++++++++--- 2 files changed, 58 insertions(+), 32 deletions(-) diff --git a/utils/GunSmith/GunSmith.js b/utils/GunSmith/GunSmith.js index c88709cf..95213818 100644 --- a/utils/GunSmith/GunSmith.js +++ b/utils/GunSmith/GunSmith.js @@ -47,15 +47,19 @@ const handleMsg = msg => { const idx = pendingPutsForPath.findIndex(pp => pp.id === id) if (pendingPut) { + pendingPutsForPath.splice(idx, 1) if (pendingPut.cb) { pendingPut.cb(ack) } - pendingPutsForPath.splice(idx, 1) } else { logger.error( `Could not find request for put message from gun subprocess. Data will be logged below.` ) - logger.info({ msg, pendingPut }) + console.log({ + msg, + pendingPut: pendingPut || 'No pending put found', + allPendingPuts: pendingPuts + }) } } } @@ -66,9 +70,8 @@ let currentGun let lastAlias = '' let lastPass = '' -/** @type {GunT.UserPair} */ -// eslint-disable-next-line init-declarations -let lastPair +/** @type {GunT.UserPair|null} */ +let lastPair = null /** @type {import('gun/types/options').IGunConstructorOptions} */ let lastOpts = {} @@ -79,6 +82,9 @@ let lastOpts = {} */ const auth = (alias, pass) => new Promise((res, rej) => { + lastAlias = '' + lastPass = '' + lastPair = null /** @type {Smith.SmithMsgAuth} */ const msg = { alias, @@ -200,8 +206,8 @@ function createReplica(path, afterMap = false) { } for (const l of listenersForThisRef) { // eslint-disable-next-line no-multi-assign - const listeners = (pathToListeners[path] = - pathToListeners[path] || new Set()) + const listeners = + pathToListeners[path] || (pathToListeners[path] = new Set()) listeners.delete(l) } @@ -210,8 +216,8 @@ function createReplica(path, afterMap = false) { listenersForThisRef.push(cb) // eslint-disable-next-line no-multi-assign - const listeners = (pathToListeners[path] = - pathToListeners[path] || new Set()) + const listeners = + pathToListeners[path] || (pathToListeners[path] = new Set()) listeners.add(cb) @@ -224,7 +230,7 @@ function createReplica(path, afterMap = false) { return this }, - once(cb, opts = { wait: 200 }) { + once(cb, opts = { wait: 500 }) { const tmp = createReplica(path, afterMap) if (afterMap) { // TODO @@ -240,6 +246,7 @@ function createReplica(path, afterMap = false) { if (cb) { cb(lastVal, path.split('>')[path.split('>').length - 1]) } + tmp.off() }, opts.wait) } return this @@ -251,16 +258,7 @@ function createReplica(path, afterMap = false) { /** @type {Smith.PendingPut} */ const pendingPut = { - cb(ack) { - const idx = pendingPutsForPath.indexOf(this) - if (idx > -1) { - pendingPutsForPath.splice(idx, 1) - } else { - logger.warn(`???`) - } - // eslint-disable-next-line no-unused-expressions - cb && cb(ack) - }, + cb: cb || (() => {}), data, id } @@ -274,7 +272,6 @@ function createReplica(path, afterMap = false) { path, type: 'put' } - currentGun.send(msg) return this }, @@ -355,6 +352,15 @@ function createUserReplica() { sea: lastPair } }, + get is() { + if (lastAlias && lastPair) { + return { + alias: lastAlias, + pub: lastPair.pub + } + } + return undefined + }, auth(alias, pass, cb) { auth(alias, pass) .then(pair => { @@ -381,15 +387,9 @@ function createUserReplica() { * @param {import('gun/types/options').IGunConstructorOptions} opts */ const Gun = opts => { - forge() lastOpts = opts + forge() - /** @type {Smith.SmithMsgInit} */ - const msg = { - opts, - type: 'init' - } - currentGun.send(msg) // We should ideally wait for a response but we'd break the constructor's // signature return createReplica('$root') diff --git a/utils/GunSmith/gun.js b/utils/GunSmith/gun.js index 7c333e45..530dcfcb 100644 --- a/utils/GunSmith/gun.js +++ b/utils/GunSmith/gun.js @@ -11,6 +11,19 @@ require('gun/nts') // @ts-ignore Gun.log = () => {} +/** + * @param {any} msg + */ +const sendMsg = msg => { + if (process.send) { + process.send(msg) + } else { + console.log( + 'Fatal error: Could not send a message from inside the gun process.' + ) + } +} + console.log('subprocess invoked') process.on('uncaughtException', e => { @@ -58,6 +71,10 @@ const handleMsg = msg => { } if (msg.type === 'init') { gun = /** @type {any} */ (new Gun(msg.opts)) + setInterval(() => { + // @ts-expect-error + console.log(Object.keys(gun.back('opt').peers)) + }, 2000) user = gun.user() } if (msg.type === 'auth') { @@ -71,8 +88,7 @@ const handleMsg = msg => { }, type: 'auth' } - // @ts-expect-error - process.send(msg) + sendMsg(msg) }) } if (msg.type === 'on') { @@ -95,8 +111,7 @@ const handleMsg = msg => { path: msg.path, type: 'on' } - // @ts-expect-error - process.send(res) + sendMsg(res) }) } if (msg.type === 'put') { @@ -112,6 +127,17 @@ const handleMsg = msg => { for (const key of keys) { node = node.get(key) } + + node.put(msg.data, ack => { + /** @type {Smith.GunMsgPut} */ + const reply = { + ack, + id: msg.id, + path: msg.path, + type: 'put' + } + sendMsg(reply) + }) } }