Skip to content

Commit

Permalink
usePreFetch hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Rod Lewis committed Jun 27, 2022
1 parent 6eb505b commit fce4fba
Show file tree
Hide file tree
Showing 14 changed files with 5 additions and 325 deletions.
20 changes: 0 additions & 20 deletions dist/lib/hooks/usePoll.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion dist/lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { useFetch } from './hooks/useFetch';
export { usePoll } from './hooks/usePoll';
export { usePreFetch } from './hooks/usePreFetch';
1 change: 0 additions & 1 deletion dist/lib/utils/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { createArrayOfUpdatesType } from "../types";
export * from "./state_tools";
export * from "./store_tools";
export * from "./poll";
export declare const DAY: number;
export declare function cleanupWorker(worker: Worker | undefined): void;
export declare const dataExpired: (maxAge: number, timestamp?: number | undefined) => boolean;
Expand Down
8 changes: 0 additions & 8 deletions dist/lib/utils/poll.d.ts

This file was deleted.

50 changes: 2 additions & 48 deletions dist/queryPlus.es.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/queryPlus.umd.js

Large diffs are not rendered by default.

29 changes: 0 additions & 29 deletions src/lib/hooks/usePoll.tsx

This file was deleted.

19 changes: 0 additions & 19 deletions src/lib/hooks/useStore.tsx

This file was deleted.

1 change: 0 additions & 1 deletion src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export {useFetch} from './hooks/useFetch';
export {usePoll} from './hooks/usePoll';
export {usePreFetch} from './hooks/usePreFetch';
1 change: 0 additions & 1 deletion src/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { createArrayOfUpdatesType, ArrayOfStringAnyTuple, StringAnyTuple }

export * from "./state_tools";
export * from "./store_tools";
export * from "./poll";

export const DAY = 24 * 60 * 60 * 1000;

Expand Down
35 changes: 0 additions & 35 deletions src/lib/utils/poll.ts

This file was deleted.

49 changes: 2 additions & 47 deletions src/lib/workers/fetch_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import {
store,
} from "../utils";

import PollWorker from "./polling_worker.js?worker&inline";

import type { WorkerResponseType, ValueType, FetchWorkerRequestType } from "../types";
import type { ValueType, FetchWorkerRequestType } from "../types";

const { remove, getData, setData, updateData } = store();

Expand All @@ -33,53 +31,10 @@ self.addEventListener(
const { type } = event.data;
let controller: AbortController | undefined = new AbortController();
let signal = controller?.signal;
let pollWorker = new PollWorker();

if (type === "cancel") {
controller?.abort();
pollWorker?.postMessage({ type: "cancel" });
pollWorker.terminate();
}
if (type === "poll") {
let {
existingData,
url,
options,
interval,
maxAttempts,
compareKeys,
} = event.data;
fetch(url, options ? { ...options, signal } : { signal })
.then(handleResponse)
.then(
(data: unknown) => {
if (isMatch(existingData, data, compareKeys)) {
self.postMessage({ type: "CACHED", data });
} else {
setData(url, { timestamp: Date.now(), data });
self.postMessage({ type: "DATA", data });
}
},
)
.catch(handleError)
.finally(() => {
pollWorker?.postMessage({
type,
url,
options,
interval,
maxAttempts,
existingData,
compareKeys,
});
});
pollWorker?.addEventListener(
"message",
({ data }: WorkerResponseType) => {
self.postMessage(data);
},
);
}

if (type === "pre-fetch") {
let { prefetch } = event.data;
prefetch.forEach(({middleware, url, options, maxAge}:FetchWorkerRequestType) => {
Expand Down
35 changes: 0 additions & 35 deletions src/lib/workers/polling_worker.ts

This file was deleted.

79 changes: 0 additions & 79 deletions src/lib/workers/store_worker.ts

This file was deleted.

0 comments on commit fce4fba

Please sign in to comment.