Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/heavy-crews-lose.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@toapi/client": patch
---

force invalidation immediately on mutation or manual revalidation
12 changes: 6 additions & 6 deletions packages/toapi-client/src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class Cache {
const cached = this.storage.get(url);
if (cached !== entry) return;
this.storage.delete(url);
this.pubSub.publish(new Set([url]));
this.pubSub.publish(new Set([url]), false);
},
Math.max(0, timeUntilRevalidation),
);
Expand All @@ -82,12 +82,12 @@ export class Cache {
return this.storage.get(url);
}

public invalidateUrl(url: string) {
public invalidateUrl(url: string, force: boolean) {
this.storage.delete(url);
return this.pubSub.publish(new Set([url]));
return this.pubSub.publish(new Set([url]), force);
}

public invalidateTags(tags: string[]) {
public invalidateTags(tags: string[], force: boolean) {
const urls = new Set<string>();
for (const tag of tags) {
const taggedUrls = this.tagIndex.get(tag);
Expand All @@ -101,13 +101,13 @@ export class Cache {
urls.add(url);
}
}
return this.pubSub.publish(urls);
return this.pubSub.publish(urls, force);
}

public invalidateAll() {
const urls = new Set(this.storage.keys());
this.storage.clear();
this.tagIndex.clear();
return this.pubSub.publish(urls);
return this.pubSub.publish(urls, false);
}
}
4 changes: 2 additions & 2 deletions packages/toapi-client/src/create-fetch-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ describe("createFetchClient", () => {
expect(getPost).toHaveBeenCalledTimes(2);
});

test("debounce", async () => {
test("force immediate invalidation", async () => {
vi.useFakeTimers();
const minTTL = 1000;
const debounceClient = createFetchClient<typeof api.routes>(
Expand All @@ -345,7 +345,7 @@ describe("createFetchClient", () => {
expect(cb).toHaveBeenCalledTimes(2);

await debounceClient.books.revalidate();
expect(cb).toHaveBeenCalledTimes(2);
expect(cb).toHaveBeenCalledTimes(3);

await vi.advanceTimersByTimeAsync(minTTL);
expect(cb).toHaveBeenCalledTimes(3);
Expand Down
9 changes: 6 additions & 3 deletions packages/toapi-client/src/create-fetch-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function createFetchClient<

listenForInvalidations({
fetch,
onInvalidate: (tags) => cache.invalidateTags(tags),
onInvalidate: (tags) => cache.invalidateTags(tags, false),
onConnect: () => cache.invalidateAll(),
logger: options.logger,
invalidationsUrl,
Expand All @@ -69,7 +69,7 @@ export function createFetchClient<
}

async function revalidate(url: string) {
await cache.invalidateUrl(url);
await cache.invalidateUrl(url, true);
}

function mutate(
Expand Down Expand Up @@ -102,7 +102,10 @@ export function createFetchClient<
});

const revalidated = response.then((res) =>
cache.invalidateTags(res.headers.get(TAGS_HEADER)?.split(" ") ?? []),
cache.invalidateTags(
res.headers.get(TAGS_HEADER)?.split(" ") ?? [],
true,
),
);

return Object.defineProperty(body, "revalidated", {
Expand Down
16 changes: 14 additions & 2 deletions packages/toapi-client/src/pub-sub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,19 @@ export class PubSub {
};
}

async publish(urls: Set<string>) {
async publish(urls: Set<string>, force: boolean) {
if (force) {
for (const url of urls) {
const timeout = this.debounceTimeouts.get(url);
clearTimeout(timeout);
this.debounceTimeouts.delete(url);
}
await Promise.all(
this.subscriptions.values().map((callback) => callback(urls)),
);
return;
}

for (const url of urls) {
if (this.debounceTimeouts.has(url)) {
// debounced, mark as requested and ignore now
Expand All @@ -39,7 +51,7 @@ export class PubSub {
}
}
// Consume pending invalidations and unlock the entire batch before replay.
if (pendingUrls.size) return this.publish(pendingUrls);
if (pendingUrls.size) return this.publish(pendingUrls, false);
}, this.minTTL);

for (const url of urls) {
Expand Down
Loading