From 5742164567b507dd5433ebaf55949c8aa7f73d71 Mon Sep 17 00:00:00 2001 From: Chris Anderson Date: Thu, 19 Sep 2024 16:34:57 -0700 Subject: [PATCH 1/3] subscribe test --- tests/fireproof/all-gateway.test.ts | 48 +++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/fireproof/all-gateway.test.ts b/tests/fireproof/all-gateway.test.ts index e33fdb61..24d16a73 100644 --- a/tests/fireproof/all-gateway.test.ts +++ b/tests/fireproof/all-gateway.test.ts @@ -297,6 +297,54 @@ describe("noop Gateway", function () { }); }); +describe("noop Gateway subscribe", function () { + let db: Database; + + let metaStore: ExtendedStore; + + let metaGateway: ExtendedGateway; + + afterEach(async function () { + await db.close(); + await db.destroy(); + }); + beforeEach(async function () { + db = new Database("test-gateway-" + Math.random().toString(36).substring(7)); + + // Extract stores from the loader + metaStore = (await db.blockstore.loader?.metaStore()) as unknown as ExtendedStore; + + metaGateway = metaStore?.gateway; + }); + it("should subscribe to meta Gateway", async function () { + const metaUrl = await metaGateway?.buildUrl(metaStore?._url, "main"); + await metaGateway?.start(metaStore?._url); + + if (metaGateway.subscribe) { + let resolve: () => void; + let didCall = false; + const p = new Promise((r) => { + resolve = r; + }); + + const metaSubscribeResult = await metaGateway?.subscribe?.(metaUrl?.Ok(), async (data: Uint8Array) => { + const decodedData = new TextDecoder().decode(data); + expect(decodedData).toContain("parents"); + didCall = true + resolve(); + }); + expect(metaSubscribeResult?.Ok()).toBeTruthy(); + const ok = await db.put({ _id: "key1", hello: "world1" }); + expect(ok).toBeTruthy(); + expect(ok.id).toBe("key1"); + await p + expect(didCall).toBeTruthy(); + } + }); + + }); +}); + describe("Gateway", function () { let db: Database; // let carStore: ExtendedStore; From fa8b28f9c896783a36d5466f84caa165cbcf2720 Mon Sep 17 00:00:00 2001 From: Chris Anderson Date: Thu, 19 Sep 2024 17:03:48 -0700 Subject: [PATCH 2/3] susbcribe tests pass on local gateways --- src/blockstore/fragment-gateway.ts | 22 +++++++++++++++++++++- tests/fireproof/all-gateway.test.ts | 6 ++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/blockstore/fragment-gateway.ts b/src/blockstore/fragment-gateway.ts index ed0fe6f0..75753cd2 100644 --- a/src/blockstore/fragment-gateway.ts +++ b/src/blockstore/fragment-gateway.ts @@ -171,7 +171,27 @@ export class FragmentGateway implements bs.Gateway { if (this.innerGW.subscribe) { return this.innerGW.subscribe(url, callback); } else { - return Result.Err(this.logger.Error().Msg("Subscribe not supported").AsError()); + let lastData: Uint8Array | undefined = undefined; + let interval = 100; + const fetchData = async () => { + const result = await this.innerGW.get(url); + if (result.isOk()) { + const data = result.Ok(); + if (!lastData || !data.every((value, index) => lastData && value === lastData[index])) { + lastData = data; + callback(data); + interval = 100; // Reset interval when data changes + } else { + interval *= 2; // Double the interval when data is unchanged + } + } + timeoutId = setTimeout(fetchData, interval); + }; + let timeoutId = setTimeout(fetchData, interval); + + return Result.Ok(() => { + clearTimeout(timeoutId); + }); } } diff --git a/tests/fireproof/all-gateway.test.ts b/tests/fireproof/all-gateway.test.ts index 24d16a73..36f4d7ba 100644 --- a/tests/fireproof/all-gateway.test.ts +++ b/tests/fireproof/all-gateway.test.ts @@ -330,19 +330,17 @@ describe("noop Gateway subscribe", function () { const metaSubscribeResult = await metaGateway?.subscribe?.(metaUrl?.Ok(), async (data: Uint8Array) => { const decodedData = new TextDecoder().decode(data); expect(decodedData).toContain("parents"); - didCall = true + didCall = true; resolve(); }); expect(metaSubscribeResult?.Ok()).toBeTruthy(); const ok = await db.put({ _id: "key1", hello: "world1" }); expect(ok).toBeTruthy(); expect(ok.id).toBe("key1"); - await p + await p; expect(didCall).toBeTruthy(); } }); - - }); }); describe("Gateway", function () { From cb011496ac5336bac082a26bf1232e17355f69bb Mon Sep 17 00:00:00 2001 From: Chris Anderson Date: Thu, 19 Sep 2024 17:24:52 -0700 Subject: [PATCH 3/3] test is better --- tests/fireproof/all-gateway.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/fireproof/all-gateway.test.ts b/tests/fireproof/all-gateway.test.ts index 36f4d7ba..b00e04f6 100644 --- a/tests/fireproof/all-gateway.test.ts +++ b/tests/fireproof/all-gateway.test.ts @@ -326,10 +326,9 @@ describe("noop Gateway subscribe", function () { const p = new Promise((r) => { resolve = r; }); - const metaSubscribeResult = await metaGateway?.subscribe?.(metaUrl?.Ok(), async (data: Uint8Array) => { const decodedData = new TextDecoder().decode(data); - expect(decodedData).toContain("parents"); + expect(decodedData).toContain("[]"); didCall = true; resolve(); });