Skip to content

Commit

Permalink
Merge pull request #2 from StudentOfJS/prefetch
Browse files Browse the repository at this point in the history
Prefetch
  • Loading branch information
StudentOfJS authored Jun 27, 2022
2 parents 4eafb3e + fce4fba commit 473b19a
Show file tree
Hide file tree
Showing 15 changed files with 277 additions and 141 deletions.
32 changes: 6 additions & 26 deletions dist/lib/hooks/useFetch.d.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,23 @@
import { UnknownDataResponseType } from "../utils";
export interface FetchWorkerProps {
fetchOptions?: RequestInit | undefined;
maxAge?: number;
middleware?: (data: UnknownDataResponseType) => UnknownDataResponseType;
url: RequestInfo | URL;
update?: {
url: RequestInfo | URL;
options?: RequestInit | undefined;
};
}
/**
* useFetch is a React hook that can be initialized with no params.
* @example const { data, error, loading, fetchWorker } = useFetch()
* fetchWorker({
* url: 'https://swapi.dev/api/people/1/',
* middleware: (d:Record<string, any>) => { let keys = Object.keys(d); return {[keys[0]]: d[keys[0]], [keys[1]]: d[keys[1]]}}
* });
*
*
*/
import type { FetchWorkerProps } from "../types";
export declare function useFetch(): {
data: any;
loading: boolean;
preload: boolean;
error: undefined;
update: boolean;
fetchWorker: ({ url, fetchOptions, maxAge, middleware }: FetchWorkerProps) => Promise<void>;
fetchWorker: ({ url, options, maxAge, middleware }: FetchWorkerProps) => Promise<void>;
} | {
error: any;
loading: boolean;
preload: boolean;
data: UnknownDataResponseType;
data: import("../utils").UnknownDataResponseType;
update: boolean;
fetchWorker: ({ url, fetchOptions, maxAge, middleware }: FetchWorkerProps) => Promise<void>;
fetchWorker: ({ url, options, maxAge, middleware }: FetchWorkerProps) => Promise<void>;
} | {
loading: any;
data: UnknownDataResponseType;
data: import("../utils").UnknownDataResponseType;
error?: Error | undefined;
preload: boolean;
update: boolean;
fetchWorker: ({ url, fetchOptions, maxAge, middleware }: FetchWorkerProps) => Promise<void>;
fetchWorker: ({ url, options, maxAge, middleware }: FetchWorkerProps) => Promise<void>;
};
2 changes: 2 additions & 0 deletions dist/lib/hooks/usePreFetch.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import type { FetchWorkerBaseRequestType } from "../types";
export declare function usePreFetch(prefetch?: Array<FetchWorkerBaseRequestType>): void;
1 change: 1 addition & 0 deletions dist/lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { useFetch } from './hooks/useFetch';
export { usePreFetch } from './hooks/usePreFetch';
42 changes: 42 additions & 0 deletions dist/lib/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export type StringAnyTuple = [string, any]
export type ArrayOfStringAnyTuple = Array<StringAnyTuple>
export type createArrayOfUpdatesType = (oldRecord: Record<string, any>, newRecord: Record<string, any>) => ArrayOfStringAnyTuple

export type WorkerResponseType = MessageEvent<{
type: string;
data?: UnknownDataResponseType
}>
export interface FetchWorkerBaseRequestType {
options?: RequestInit | undefined
maxAge?: number
middleware?: (data: UnknownDataResponseType) => UnknownDataResponseType
url: RequestInfo | URL
}
export interface FetchWorkerRequestType extends FetchWorkerBaseRequestType {
middleware?: string
}

export interface FetchWorkerProps extends FetchWorkerBaseRequestType {
/*
update will be fetched after the response is received.
This is useful for updating after POST PUT or DELETE,
where the api doesn't return all the data and the GET
endpoint is different.
*/
update?: {
url: RequestInfo | URL,
options?: RequestInit | undefined
}
}
export interface PollWorkerProps {
url: RequestInfo | URL
interval: number,
options?: RequestInit | undefined
maxAttempts?: number,
existingData?: number,
compareKeys?: string[]
}

export type ValueType = { timestamp: number; maxAge: number; data: any };


9 changes: 4 additions & 5 deletions dist/lib/utils/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { createArrayOfUpdatesType } from "../types";
export * from "./state_tools";
export * from "./store_tools";
export declare const DAY: number;
export declare function cleanupWorker(worker: Worker | undefined): void;
export declare const dataExpired: (maxAge: number, timestamp?: number | undefined) => boolean;
Expand All @@ -7,9 +9,6 @@ export declare const flattenObjectToArray: (obj: Record<string, any>, parent?: s
export declare const flattenAndSortArray: (arr: Array<any>) => any[];
export declare const methodType: (options: RequestInit | undefined) => string;
export declare const isMatch: (a: unknown, b: unknown, compareKeys?: string[] | undefined) => boolean;
export declare type StringAnyTuple = [string, any];
export declare type ArrayOfStringAnyTuple = Array<StringAnyTuple>;
export declare type createArrayOfUpdatesType = (oldRecord: Record<string, any>, newRecord: Record<string, any>) => ArrayOfStringAnyTuple;
export declare const createArrayOfUpdates: createArrayOfUpdatesType;
export declare const serializeFunction: (f: Function) => string;
export declare const deserializeFunction: (s: string) => any;
export declare const serializeFunction: (f?: Function | undefined) => string | undefined;
export declare const deserializeFunction: (s?: string | undefined) => any;
6 changes: 6 additions & 0 deletions dist/lib/utils/store_tools.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export declare function store(): {
remove: (key: IDBValidKey) => Promise<void>;
getData: (key: IDBValidKey) => Promise<any>;
setData: (key: IDBValidKey, value: unknown) => Promise<void>;
updateData: (key: IDBValidKey, updater: (oldValue: any) => any) => Promise<void>;
};
34 changes: 27 additions & 7 deletions dist/queryPlus.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ var __spreadValues = (a, b) => {
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
import { useReducer, useRef, useEffect } from "react";
const encodedJs = "dmFyIGI9T2JqZWN0LmRlZmluZVByb3BlcnR5LEk9T2JqZWN0LmRlZmluZVByb3BlcnRpZXM7dmFyIFE9T2JqZWN0LmdldE93blByb3BlcnR5RGVzY3JpcHRvcnM7dmFyIEw9T2JqZWN0LmdldE93blByb3BlcnR5U3ltYm9sczt2YXIgVz1PYmplY3QucHJvdG90eXBlLmhhc093blByb3BlcnR5LFg9T2JqZWN0LnByb3RvdHlwZS5wcm9wZXJ0eUlzRW51bWVyYWJsZTt2YXIgUj0oYSxjLGkpPT5jIGluIGE/YihhLGMse2VudW1lcmFibGU6ITAsY29uZmlndXJhYmxlOiEwLHdyaXRhYmxlOiEwLHZhbHVlOml9KTphW2NdP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RoZW4obz0+e3A/ZmV0Y2gocC51cmwscC5vcHRpb25zKS50aGVuKEQpLnRoZW4oRSkuY2F0Y2goeT0+e3Rocm93IHl9KTpOKHMudG9TdHJpbmcoKSx5PT57bGV0IHg9RGF0ZS5ub3coKSxMPUEobykmJkEoeT09bnVsbD92b2lkIDA6eS5kYXRhKT9sKGwoe30seS5kYXRhKSxvKTpvO3JldHVybiBzZWxmLnBvc3RNZXNzYWdlKHt0eXBlOnUsZGF0YTpMfSkse3RpbWVzdGFtcDp4LG1heEFnZTpkLGRhdGE6TH19KS5jYXRjaCgoKT0+e2NvbnNvbGUuaW5mbygidXBkYXRlIHN0b3JlIGZhaWxlZCIpfSkuZmluYWxseSgoKT0+e3NlbGYucG9zdE1lc3NhZ2Uoe3R5cGU6IkNPTVBMRVRFIn0pfSl9KS5jYXRjaChTKX19KX0pKCk7Cg==";
const blob = typeof window !== "undefined" && window.Blob && new Blob([atob(encodedJs)], { type: "text/javascript;charset=utf-8" });
function WorkerWrapper() {
const objURL = blob && (window.URL || window.webkitURL).createObjectURL(blob);
Expand Down Expand Up @@ -55,13 +56,13 @@ function cleanupWorker(worker) {
worker == null ? void 0 : worker.terminate();
worker = void 0;
}
const serializeFunction = (f) => encodeURI(f.toString());
const serializeFunction = (f) => f ? encodeURI(f.toString()) : void 0;
function useFetch() {
const [state, dispatch] = useReducer(reducer, initialState);
const workerRef = useRef();
const fetchWorker = async ({
url,
fetchOptions,
options,
maxAge = DAY,
middleware
}) => {
Expand Down Expand Up @@ -104,13 +105,12 @@ function useFetch() {
break;
}
});
let serializedMw = middleware ? serializeFunction(middleware) : void 0;
worker == null ? void 0 : worker.postMessage({
type: "fetch",
url,
fetchOptions,
options,
existingData: state.data,
middleware: serializedMw,
middleware: serializeFunction(middleware),
maxAge
});
};
Expand All @@ -124,4 +124,24 @@ function useFetch() {
fetchWorker
}, state);
}
export { useFetch };
function usePreFetch(prefetch) {
const worker = useRef(new WorkerWrapper());
useEffect(() => {
var _a;
if (prefetch && worker.current) {
(_a = worker.current) == null ? void 0 : _a.postMessage({
type: "pre-fetch",
prefetch: prefetch.map((p) => __spreadProps(__spreadValues({}, p), {
middleware: serializeFunction(p.middleware)
}))
});
}
}, [prefetch, worker.current]);
useEffect(() => {
worker.current = new WorkerWrapper();
return () => {
cleanupWorker(worker.current);
};
}, []);
}
export { useFetch, usePreFetch };
Loading

0 comments on commit 473b19a

Please sign in to comment.