Skip to content

Commit

Permalink
Attempt to fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
guillemcordoba committed Apr 17, 2024
1 parent 6b22c82 commit c2ff882
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
1 change: 0 additions & 1 deletion nix/sweettest.nix
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,5 @@ in craneLib.cargoNextest (commonArgs // {
inherit cargoArtifacts;
cargoNextestExtraArgs = "-p ${crate} --locked -j 1";


DNA_PATH = dna;
})
2 changes: 1 addition & 1 deletion packages/stores/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@holochain-open-dev/stores",
"version": "0.300.0-dev.3",
"version": "0.300.0-dev.4",
"description": "Re-export of svelte/store, with additional utilities to build reusable holochain-open-dev modules",
"author": "[email protected]",
"main": "dist/index.js",
Expand Down
13 changes: 13 additions & 0 deletions packages/stores/src/holochain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export function collectionStore<
let links: Link[];

const maybeSet = (newLinksValue: Link[]) => {
if (!active) return;
const orderedNewLinks = uniquifyLinks(newLinksValue).sort(
sortLinksByTimestampAscending
);
Expand All @@ -81,6 +82,7 @@ export function collectionStore<
};

const fetch = async () => {
if (!active) return;
const nlinks = await fetchCollection().finally(() => {
if (active) {
setTimeout(() => fetch(), pollIntervalMs);
Expand Down Expand Up @@ -177,6 +179,7 @@ export function latestVersionOfEntryStore<
let active = true;
let latestVersion: EntryRecord<T> | undefined;
const fetch = async () => {
if (!active) return;
try {
const nlatestVersion = await fetchLatestVersion();
if (nlatestVersion) {
Expand Down Expand Up @@ -209,6 +212,7 @@ export function latestVersionOfEntryStore<
};
fetch();
const unsubs = client.onSignal((originalSignal) => {
if (!active) return;
if (!(originalSignal as ActionCommittedSignal<any, any>).type) return;
const signal = originalSignal as ActionCommittedSignal<any, any>;

Expand Down Expand Up @@ -262,6 +266,8 @@ export function allRevisionsOfEntryStore<
let active = true;
let allRevisions: Array<EntryRecord<T>>;
const fetch = async () => {
if (!active) return;

const nAllRevisions = await fetchAllRevisions().finally(() => {
if (active) {
setTimeout(() => fetch(), pollIntervalMs);
Expand All @@ -280,6 +286,7 @@ export function allRevisionsOfEntryStore<
};
await fetch();
const unsubs = client.onSignal(async (originalSignal) => {
if (!active) return;
if (!(originalSignal as ActionCommittedSignal<any, any>).type) return;
const signal = originalSignal as ActionCommittedSignal<any, any>;

Expand Down Expand Up @@ -333,6 +340,7 @@ export function deletesForEntryStore<
let active = true;
let deletes: Array<SignedActionHashed<Delete>>;
const fetch = async () => {
if (!active) return;
const ndeletes = await fetchDeletes().finally(() => {
if (active) {
setTimeout(() => fetch(), pollIntervalMs);
Expand All @@ -351,6 +359,7 @@ export function deletesForEntryStore<
};
await fetch();
const unsubs = client.onSignal((originalSignal) => {
if (!active) return;
if (!(originalSignal as ActionCommittedSignal<any, any>).type) return;
const signal = originalSignal as ActionCommittedSignal<any, any>;

Expand Down Expand Up @@ -450,6 +459,7 @@ export function liveLinksStore<
let active = true;

const maybeSet = (newLinksValue: Link[]) => {
if (!active) return;
const orderedNewLinks = uniquifyLinks(newLinksValue).sort(
sortLinksByTimestampAscending
);
Expand All @@ -466,6 +476,7 @@ export function liveLinksStore<
}
};
const fetch = async () => {
if (!active) return;
const nlinks = await fetchLinks().finally(() => {
if (active) {
setTimeout(() => fetch(), pollIntervalMs);
Expand Down Expand Up @@ -550,6 +561,7 @@ export function deletedLinksStore<
[SignedActionHashed<CreateLink>, Array<SignedActionHashed<DeleteLink>>]
>
) => {
if (!active) return;
const orderedNewLinks = newDeletedLinks.sort(
sortDeletedLinksByTimestampAscending
);
Expand Down Expand Up @@ -579,6 +591,7 @@ export function deletedLinksStore<
}
};
const fetch = async () => {
if (!active) return;
const ndeletedLinks = await fetchDeletedLinks().finally(() => {
if (active) {
setTimeout(() => fetch(), pollIntervalMs);
Expand Down
21 changes: 21 additions & 0 deletions packages/stores/tests/holochain.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,27 @@ test("liveLinks only updates once if no new links exist", async () => {
});
});

test("liveLinks makes the request again after being unsubscribed from", async () => {
const links = [await fakeLink()];
let requests = 0;
const store = liveLinksStore(
new ZomeClient(new ZomeMock("", "")),
await fakeEntryHash(),
async () => {
requests += 1;
return links;
},
""
);

await toPromise(store);
await toPromise(store);
await toPromise(store);
await toPromise(store);

assert.equal(requests, 4)
});

test("collection store works", async () => {
const links = [await fakeLink()];
const collection = collectionStore(
Expand Down

0 comments on commit c2ff882

Please sign in to comment.