Further improvements
This commit is contained in:
parent
97be7cc6fd
commit
ec56fdfcfd
2 changed files with 58 additions and 32 deletions
|
|
@ -47,15 +47,19 @@ const handleMsg = msg => {
|
||||||
const idx = pendingPutsForPath.findIndex(pp => pp.id === id)
|
const idx = pendingPutsForPath.findIndex(pp => pp.id === id)
|
||||||
|
|
||||||
if (pendingPut) {
|
if (pendingPut) {
|
||||||
|
pendingPutsForPath.splice(idx, 1)
|
||||||
if (pendingPut.cb) {
|
if (pendingPut.cb) {
|
||||||
pendingPut.cb(ack)
|
pendingPut.cb(ack)
|
||||||
}
|
}
|
||||||
pendingPutsForPath.splice(idx, 1)
|
|
||||||
} else {
|
} else {
|
||||||
logger.error(
|
logger.error(
|
||||||
`Could not find request for put message from gun subprocess. Data will be logged below.`
|
`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 lastAlias = ''
|
||||||
let lastPass = ''
|
let lastPass = ''
|
||||||
/** @type {GunT.UserPair} */
|
/** @type {GunT.UserPair|null} */
|
||||||
// eslint-disable-next-line init-declarations
|
let lastPair = null
|
||||||
let lastPair
|
|
||||||
/** @type {import('gun/types/options').IGunConstructorOptions} */
|
/** @type {import('gun/types/options').IGunConstructorOptions} */
|
||||||
let lastOpts = {}
|
let lastOpts = {}
|
||||||
|
|
||||||
|
|
@ -79,6 +82,9 @@ let lastOpts = {}
|
||||||
*/
|
*/
|
||||||
const auth = (alias, pass) =>
|
const auth = (alias, pass) =>
|
||||||
new Promise((res, rej) => {
|
new Promise((res, rej) => {
|
||||||
|
lastAlias = ''
|
||||||
|
lastPass = ''
|
||||||
|
lastPair = null
|
||||||
/** @type {Smith.SmithMsgAuth} */
|
/** @type {Smith.SmithMsgAuth} */
|
||||||
const msg = {
|
const msg = {
|
||||||
alias,
|
alias,
|
||||||
|
|
@ -200,8 +206,8 @@ function createReplica(path, afterMap = false) {
|
||||||
}
|
}
|
||||||
for (const l of listenersForThisRef) {
|
for (const l of listenersForThisRef) {
|
||||||
// eslint-disable-next-line no-multi-assign
|
// eslint-disable-next-line no-multi-assign
|
||||||
const listeners = (pathToListeners[path] =
|
const listeners =
|
||||||
pathToListeners[path] || new Set())
|
pathToListeners[path] || (pathToListeners[path] = new Set())
|
||||||
|
|
||||||
listeners.delete(l)
|
listeners.delete(l)
|
||||||
}
|
}
|
||||||
|
|
@ -210,8 +216,8 @@ function createReplica(path, afterMap = false) {
|
||||||
listenersForThisRef.push(cb)
|
listenersForThisRef.push(cb)
|
||||||
|
|
||||||
// eslint-disable-next-line no-multi-assign
|
// eslint-disable-next-line no-multi-assign
|
||||||
const listeners = (pathToListeners[path] =
|
const listeners =
|
||||||
pathToListeners[path] || new Set())
|
pathToListeners[path] || (pathToListeners[path] = new Set())
|
||||||
|
|
||||||
listeners.add(cb)
|
listeners.add(cb)
|
||||||
|
|
||||||
|
|
@ -224,7 +230,7 @@ function createReplica(path, afterMap = false) {
|
||||||
|
|
||||||
return this
|
return this
|
||||||
},
|
},
|
||||||
once(cb, opts = { wait: 200 }) {
|
once(cb, opts = { wait: 500 }) {
|
||||||
const tmp = createReplica(path, afterMap)
|
const tmp = createReplica(path, afterMap)
|
||||||
if (afterMap) {
|
if (afterMap) {
|
||||||
// TODO
|
// TODO
|
||||||
|
|
@ -240,6 +246,7 @@ function createReplica(path, afterMap = false) {
|
||||||
if (cb) {
|
if (cb) {
|
||||||
cb(lastVal, path.split('>')[path.split('>').length - 1])
|
cb(lastVal, path.split('>')[path.split('>').length - 1])
|
||||||
}
|
}
|
||||||
|
tmp.off()
|
||||||
}, opts.wait)
|
}, opts.wait)
|
||||||
}
|
}
|
||||||
return this
|
return this
|
||||||
|
|
@ -251,16 +258,7 @@ function createReplica(path, afterMap = false) {
|
||||||
|
|
||||||
/** @type {Smith.PendingPut} */
|
/** @type {Smith.PendingPut} */
|
||||||
const pendingPut = {
|
const pendingPut = {
|
||||||
cb(ack) {
|
cb: cb || (() => {}),
|
||||||
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)
|
|
||||||
},
|
|
||||||
data,
|
data,
|
||||||
id
|
id
|
||||||
}
|
}
|
||||||
|
|
@ -274,7 +272,6 @@ function createReplica(path, afterMap = false) {
|
||||||
path,
|
path,
|
||||||
type: 'put'
|
type: 'put'
|
||||||
}
|
}
|
||||||
|
|
||||||
currentGun.send(msg)
|
currentGun.send(msg)
|
||||||
return this
|
return this
|
||||||
},
|
},
|
||||||
|
|
@ -355,6 +352,15 @@ function createUserReplica() {
|
||||||
sea: lastPair
|
sea: lastPair
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
get is() {
|
||||||
|
if (lastAlias && lastPair) {
|
||||||
|
return {
|
||||||
|
alias: lastAlias,
|
||||||
|
pub: lastPair.pub
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return undefined
|
||||||
|
},
|
||||||
auth(alias, pass, cb) {
|
auth(alias, pass, cb) {
|
||||||
auth(alias, pass)
|
auth(alias, pass)
|
||||||
.then(pair => {
|
.then(pair => {
|
||||||
|
|
@ -381,15 +387,9 @@ function createUserReplica() {
|
||||||
* @param {import('gun/types/options').IGunConstructorOptions} opts
|
* @param {import('gun/types/options').IGunConstructorOptions} opts
|
||||||
*/
|
*/
|
||||||
const Gun = opts => {
|
const Gun = opts => {
|
||||||
forge()
|
|
||||||
lastOpts = opts
|
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
|
// We should ideally wait for a response but we'd break the constructor's
|
||||||
// signature
|
// signature
|
||||||
return createReplica('$root')
|
return createReplica('$root')
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,19 @@ require('gun/nts')
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
Gun.log = () => {}
|
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')
|
console.log('subprocess invoked')
|
||||||
|
|
||||||
process.on('uncaughtException', e => {
|
process.on('uncaughtException', e => {
|
||||||
|
|
@ -58,6 +71,10 @@ const handleMsg = msg => {
|
||||||
}
|
}
|
||||||
if (msg.type === 'init') {
|
if (msg.type === 'init') {
|
||||||
gun = /** @type {any} */ (new Gun(msg.opts))
|
gun = /** @type {any} */ (new Gun(msg.opts))
|
||||||
|
setInterval(() => {
|
||||||
|
// @ts-expect-error
|
||||||
|
console.log(Object.keys(gun.back('opt').peers))
|
||||||
|
}, 2000)
|
||||||
user = gun.user()
|
user = gun.user()
|
||||||
}
|
}
|
||||||
if (msg.type === 'auth') {
|
if (msg.type === 'auth') {
|
||||||
|
|
@ -71,8 +88,7 @@ const handleMsg = msg => {
|
||||||
},
|
},
|
||||||
type: 'auth'
|
type: 'auth'
|
||||||
}
|
}
|
||||||
// @ts-expect-error
|
sendMsg(msg)
|
||||||
process.send(msg)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if (msg.type === 'on') {
|
if (msg.type === 'on') {
|
||||||
|
|
@ -95,8 +111,7 @@ const handleMsg = msg => {
|
||||||
path: msg.path,
|
path: msg.path,
|
||||||
type: 'on'
|
type: 'on'
|
||||||
}
|
}
|
||||||
// @ts-expect-error
|
sendMsg(res)
|
||||||
process.send(res)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if (msg.type === 'put') {
|
if (msg.type === 'put') {
|
||||||
|
|
@ -112,6 +127,17 @@ const handleMsg = msg => {
|
||||||
for (const key of keys) {
|
for (const key of keys) {
|
||||||
node = node.get(key)
|
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)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue