diff --git a/services/gunDB/contact-api/SimpleGUN.ts b/services/gunDB/contact-api/SimpleGUN.ts index d9270915..3c98b025 100644 --- a/services/gunDB/contact-api/SimpleGUN.ts +++ b/services/gunDB/contact-api/SimpleGUN.ts @@ -31,14 +31,14 @@ export interface Soul { put: Primitive | null | object | undefined } -export interface GUNNode { +export interface GUNNodeBase { _: Soul - get(key: string): GUNNode + map(): GUNNode - put(data: ValidDataValue | GUNNode, cb?: Callback): GUNNode + on(this: GUNNode, cb: Listener): void once(this: GUNNode, cb?: Listener): GUNNode - set(data: ValidDataValue | GUNNode, cb?: Callback): GUNNode + off(): void user(): UserGUNNode user(epub: string): GUNNode @@ -47,6 +47,12 @@ export interface GUNNode { then(cb: (v: ListenerData) => T): Promise } +export interface GUNNode extends GUNNodeBase { + get(key: string): GUNNode + put(data: ValidDataValue | GUNNode, cb?: Callback): GUNNode + set(data: ValidDataValue | GUNNode, cb?: Callback): GUNNode +} + export interface CreateAck { pub: string | undefined err: string | undefined diff --git a/services/gunDB/contact-api/utils/PGUNNode.ts b/services/gunDB/contact-api/utils/PGUNNode.ts new file mode 100644 index 00000000..5c74b0e1 --- /dev/null +++ b/services/gunDB/contact-api/utils/PGUNNode.ts @@ -0,0 +1,8 @@ +/** @format */ +import { GUNNode, GUNNodeBase, ValidDataValue } from '../SimpleGUN' + +export interface PGUNNode extends GUNNodeBase { + get(key: string): PGUNNode + put(data: ValidDataValue | GUNNode): Promise + set(data: ValidDataValue | GUNNode): Promise +} diff --git a/services/gunDB/contact-api/utils/index.js b/services/gunDB/contact-api/utils/index.js index 9993f137..b9121093 100644 --- a/services/gunDB/contact-api/utils/index.js +++ b/services/gunDB/contact-api/utils/index.js @@ -291,5 +291,6 @@ module.exports = { recipientToOutgoingID, reqWasAccepted, currHandshakeAddress, - tryAndWait + tryAndWait, + promisifyGunNode: require('./promisifygun') } diff --git a/services/gunDB/contact-api/utils/promisifygun.js b/services/gunDB/contact-api/utils/promisifygun.js new file mode 100644 index 00000000..cc045f9d --- /dev/null +++ b/services/gunDB/contact-api/utils/promisifygun.js @@ -0,0 +1,47 @@ +/** + * @format + * @typedef {import("../SimpleGUN").ValidDataValue} ValidDataValue + * @typedef {import('../SimpleGUN').GUNNode} GUNNode + * @typedef {import('./PGUNNode').PGUNNode} PGUNNode + */ + +/** + * @param {GUNNode} node + * @returns {PGUNNode} + */ +const promisify = node => { + const oldPut = node.put.bind(node) + const oldSet = node.set.bind(node) + const oldGet = node.get.bind(node) + + const _pnode = /** @type {unknown} */ (node) + const pnode = /** @type {PGUNNode} */ (_pnode) + + pnode.put = data => + new Promise((res, rej) => { + oldPut(data, ack => { + if (ack.err) { + rej(new Error(ack.err)) + } else { + res() + } + }) + }) + + pnode.set = data => + new Promise((res, rej) => { + oldSet(data, ack => { + if (ack.err) { + rej(new Error(ack.err)) + } else { + res() + } + }) + }) + + pnode.get = key => promisify(oldGet(key)) + + return pnode +} + +module.exports = promisify