Skip to content

Commit

Permalink
susbcribe tests pass on local gateways
Browse files Browse the repository at this point in the history
  • Loading branch information
jchris committed Sep 20, 2024
1 parent 5742164 commit fa8b28f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
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);
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
6 changes: 2 additions & 4 deletions tests/fireproof/all-gateway.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit fa8b28f

Please sign in to comment.