This commit is contained in:
Daniel Lugo 2021-09-11 21:44:38 -04:00
parent 3ad9f0f5ef
commit f94646d67b
3 changed files with 22 additions and 10 deletions

View file

@ -2,12 +2,12 @@
* @format * @format
*/ */
const Common = require('shock-common') const Common = require('shock-common')
const Gun = require('gun') const Gun = require('../../../utils/GunSmith')
// @ts-ignore // @ts-ignore
require('gun/nts') require('gun/nts')
const logger = require('../../../config/log') const logger = require('../../../config/log')
// @ts-ignore // @ts-ignore
Gun.log = () => {} // Gun.log = () => {}
// @ts-ignore // @ts-ignore
require('gun/lib/open') require('gun/lib/open')
// @ts-ignore // @ts-ignore

View file

@ -64,6 +64,8 @@ let currentGun = fork('./gun')
let lastAlias = '' let lastAlias = ''
let lastPass = '' let lastPass = ''
/** @type {import('gun/types/options').IGunConstructorOptions} */
let lastOpts = {}
/** /**
* @param {string} alias * @param {string} alias
@ -120,6 +122,13 @@ const forge = () => {
const newGun = fork('./gun') const newGun = fork('./gun')
currentGun = newGun currentGun = newGun
/** @type {Smith.SmithMsgInit} */
const initMsg = {
opts: lastOpts,
type: 'init'
}
currentGun.send(initMsg)
currentGun.on('message', handleMsg) currentGun.on('message', handleMsg)
const lastGunListeners = Object.keys(pathToListeners).map(path => { const lastGunListeners = Object.keys(pathToListeners).map(path => {
@ -130,7 +139,6 @@ const forge = () => {
} }
return msg return msg
}) })
currentGun.send(lastGunListeners) currentGun.send(lastGunListeners)
autoAuth().then(() => { autoAuth().then(() => {
@ -151,9 +159,12 @@ function createReplica(path, afterMap = false) {
_: { _: {
get: '', get: '',
opt: { opt: {
// TODO
peers: {} peers: {}
}, },
put: {} put: {
// TODO
}
}, },
get(key) { get(key) {
if (afterMap) { if (afterMap) {
@ -202,17 +213,18 @@ function createReplica(path, afterMap = false) {
once(cb, opts = { wait: 200 }) { once(cb, opts = { wait: 200 }) {
const tmp = createReplica(path, afterMap) const tmp = createReplica(path, afterMap)
if (afterMap) { if (afterMap) {
// // TODO
} else { } else {
/** @type {GunT.ListenerData} */
let lastVal = null let lastVal = null
tmp.on((data, key) => { tmp.on(data => {
lastVal = data lastVal = data
}) })
setTimeout(() => { setTimeout(() => {
if (cb) { if (cb) {
cb(lastVal, path.split('>').lastItem) cb(lastVal, path.split('>')[path.split('>').length - 1])
} }
}, opts.wait) }, opts.wait)
} }
@ -360,6 +372,8 @@ function createUserReplica() {
* @param {import('gun/types/options').IGunConstructorOptions} opts * @param {import('gun/types/options').IGunConstructorOptions} opts
*/ */
const Gun = opts => { const Gun = opts => {
lastOpts = opts
/** @type {Smith.SmithMsgInit} */ /** @type {Smith.SmithMsgInit} */
const msg = { const msg = {
opts, opts,

View file

@ -23,9 +23,6 @@ let gun
// eslint-disable-next-line init-declarations // eslint-disable-next-line init-declarations
let user let user
/** @type {Set<string>} */
const pendingOnces = new Set()
/** /**
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
@ -49,6 +46,7 @@ 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))
user = gun.user()
} }
if (msg.type === 'auth') { if (msg.type === 'auth') {
const { alias, pass } = msg const { alias, pass } = msg