From 91e2ec08c5c6d786bb104cab3a7a70dba62bc13d Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Fri, 17 Sep 2021 16:15:43 -0400 Subject: [PATCH] Add a promisified put() to GunSmith --- utils/GunSmith/GunSmith.js | 11 +++++++++++ utils/GunSmith/GunSmith.spec.js | 18 ++++++++++++++++++ utils/GunSmith/Smith.ts | 5 +++++ 3 files changed, 34 insertions(+) diff --git a/utils/GunSmith/GunSmith.js b/utils/GunSmith/GunSmith.js index 3b262969..01dc37e1 100644 --- a/utils/GunSmith/GunSmith.js +++ b/utils/GunSmith/GunSmith.js @@ -612,6 +612,17 @@ function createReplica(path, afterMap = false) { } }) }) + }, + pPut(data) { + return new Promise((res, rej) => { + this.put(data, ack => { + if (ack.err) { + rej(new Error(ack.err)) + } else { + res() + } + }) + }) } } } diff --git a/utils/GunSmith/GunSmith.spec.js b/utils/GunSmith/GunSmith.spec.js index a1f02602..cb9d87c4 100644 --- a/utils/GunSmith/GunSmith.spec.js +++ b/utils/GunSmith/GunSmith.spec.js @@ -408,4 +408,22 @@ describe('gun smith', () => { done() release() }) + + it('provides an special thenable put()', async done => { + expect.assertions(1) + await whenReady() + + const a = words() + const b = words() + const node = instance.get(a).get(b) + const value = words() + + await node.pPut(value) + + const res = await node.then() + + expect(res).toBe(value) + done() + release() + }) }) diff --git a/utils/GunSmith/Smith.ts b/utils/GunSmith/Smith.ts index 0fdab762..e8d0af0d 100644 --- a/utils/GunSmith/Smith.ts +++ b/utils/GunSmith/Smith.ts @@ -34,6 +34,11 @@ namespace Smith { * if needed. */ specialThen(): Promise + /** + * A promise version of put(). + * @throws + */ + pPut(data: GunT.ValidDataValue): Promise } export type UserSmithNode = GunSmithNode & GunT.UserGUNNode