Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subscribe test #231

Merged
merged 3 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/blockstore/fragment-gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mabels this code is definitely in the wrong place, but it works just right -- fallback pattern for gateways that dont have a push capabiliity on the server or haven't implemented it yet

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);
});
}
}

Expand Down
45 changes: 45 additions & 0 deletions tests/fireproof/all-gateway.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,51 @@ 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<void>((r) => {
resolve = r;
});
const metaSubscribeResult = await metaGateway?.subscribe?.(metaUrl?.Ok(), async (data: Uint8Array) => {
const decodedData = new TextDecoder().decode(data);
expect(decodedData).toContain("[]");
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;
Expand Down