Initial commit
This commit is contained in:
commit
afed023152
8152 files changed
+2086461
No files matched your search
+3
@@ -0,0 +1,3 @@
|
||||
declare const INFINITE_PREFIX = "$inf$";
|
||||
|
||||
export { INFINITE_PREFIX };
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
declare const INFINITE_PREFIX = "$inf$";
|
||||
|
||||
export { INFINITE_PREFIX };
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
|
||||
const INFINITE_PREFIX = '$inf$';
|
||||
|
||||
exports.INFINITE_PREFIX = INFINITE_PREFIX;
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
const INFINITE_PREFIX = '$inf$';
|
||||
|
||||
export { INFINITE_PREFIX };
|
||||
+1
@@ -0,0 +1 @@
|
||||
export { E as ERROR_REVALIDATE_EVENT, F as FOCUS_EVENT, M as MUTATE_EVENT, R as RECONNECT_EVENT, U as UNLOAD_EVENT } from '../events-ny18dyc2.js';
|
||||
+1
@@ -0,0 +1 @@
|
||||
export { E as ERROR_REVALIDATE_EVENT, F as FOCUS_EVENT, M as MUTATE_EVENT, R as RECONNECT_EVENT, U as UNLOAD_EVENT } from '../events-ny18dyc2.js';
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
|
||||
var bunchee_group_0 = require('../config-context-s960zqob.js');
|
||||
|
||||
|
||||
|
||||
exports.ERROR_REVALIDATE_EVENT = bunchee_group_0.ERROR_REVALIDATE_EVENT;
|
||||
exports.FOCUS_EVENT = bunchee_group_0.FOCUS_EVENT;
|
||||
exports.MUTATE_EVENT = bunchee_group_0.MUTATE_EVENT;
|
||||
exports.RECONNECT_EVENT = bunchee_group_0.RECONNECT_EVENT;
|
||||
exports.UNLOAD_EVENT = bunchee_group_0.UNLOAD_EVENT;
|
||||
+1
@@ -0,0 +1 @@
|
||||
export { E as ERROR_REVALIDATE_EVENT, F as FOCUS_EVENT, M as MUTATE_EVENT, R as RECONNECT_EVENT, h as UNLOAD_EVENT } from '../config-context-ext53wcz.mjs';
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
import { h as Cache, P as ProviderConfiguration, i as ScopedMutator, U as Unloader, G as GlobalState, j as State, k as FullConfiguration, A as Arguments, d as MutatorCallback, g as MutatorOptions, K as Key, l as Fetcher, e as SWRConfiguration, a as SWRHook, M as Middleware } from '../types-l2s7dphv.js';
|
||||
export { B as BareFetcher, m as BlockingData, n as Broadcaster, C as CacheData, F as FetcherResponse, I as InternalConfiguration, o as IsLoadingResponse, p as KeyLoader, q as KeyedMutator, r as Mutator, s as MutatorConfig, t as MutatorFn, v as MutatorWrapper, w as PublicConfiguration, R as RevalidateCallback, x as RevalidateEvent, y as Revalidator, z as RevalidatorOptions, S as SWRConfig, b as SWRConfigValue, c as SWRResponse, D as StateDependencies, f as StrictTupleKey, E as UnloadOptions, H as cache, J as compare, L as defaultConfig, N as mutate, O as preload, Q as unload, T as useSWRConfig } from '../types-l2s7dphv.js';
|
||||
export { e as revalidateEvents } from '../events-ny18dyc2.js';
|
||||
export { INFINITE_PREFIX } from './constants.mjs';
|
||||
import react__default from 'react';
|
||||
export { s as serialize } from '../serialize-bwklnvdf.js';
|
||||
|
||||
declare const initCache: <Data = any>(provider: Cache<Data>, options?: Partial<ProviderConfiguration>) => [Cache<Data>, ScopedMutator, () => void, () => void, Unloader] | [Cache<Data>, ScopedMutator, undefined, undefined, Unloader] | undefined;
|
||||
|
||||
declare const IS_REACT_LEGACY = false;
|
||||
declare const IS_SERVER: boolean;
|
||||
declare const rAF: (f: (...args: any[]) => void) => number | ReturnType<typeof setTimeout>;
|
||||
declare const useIsomorphicLayoutEffect: typeof react__default.useEffect;
|
||||
declare const slowConnection: boolean | undefined;
|
||||
|
||||
declare const SWRGlobalState: WeakMap<Cache<any>, GlobalState>;
|
||||
|
||||
declare const stableHash: (arg: any) => string;
|
||||
|
||||
declare const isWindowDefined: boolean;
|
||||
declare const isDocumentDefined: boolean;
|
||||
declare const isLegacyDeno: boolean;
|
||||
declare const hasRequestAnimationFrame: () => boolean;
|
||||
declare const createCacheHelper: <Data = any, T = State<Data, any>>(cache: Cache, key: string | undefined) => readonly [() => T, (info: T) => void, (key: string, callback: (current: any, prev: any) => void) => () => void, () => any];
|
||||
|
||||
declare const noop: () => void;
|
||||
declare const UNDEFINED: undefined;
|
||||
declare const OBJECT: ObjectConstructor;
|
||||
declare const isUndefined: (v: any) => v is undefined;
|
||||
declare const isFunction: <T extends (...args: any[]) => any = (...args: any[]) => any>(v: unknown) => v is T;
|
||||
declare const mergeObjects: (a: any, b?: any) => any;
|
||||
declare const isPromiseLike: (x: unknown) => x is PromiseLike<unknown>;
|
||||
|
||||
declare const mergeConfigs: (a: Partial<FullConfiguration>, b?: Partial<FullConfiguration>) => Partial<FullConfiguration>;
|
||||
|
||||
type KeyFilter = (key?: Arguments) => boolean;
|
||||
declare function internalMutate<Data>(cache: Cache, _key: KeyFilter, _data?: Data | Promise<Data | undefined> | MutatorCallback<Data>, _opts?: boolean | MutatorOptions<Data>): Promise<Array<Data | undefined>>;
|
||||
declare function internalMutate<Data>(cache: Cache, _key: Arguments, _data?: Data | Promise<Data | undefined> | MutatorCallback<Data>, _opts?: boolean | MutatorOptions<Data>): Promise<Data | undefined>;
|
||||
|
||||
declare const normalize: <KeyType = Key, Data = any>(args: [KeyType] | [KeyType, Fetcher<Data> | null] | [KeyType, SWRConfiguration | undefined] | [KeyType, Fetcher<Data> | null, SWRConfiguration | undefined]) => [KeyType, Fetcher<Data> | null, Partial<SWRConfiguration<Data>>];
|
||||
|
||||
declare const withArgs: <SWRType>(hook: any) => SWRType;
|
||||
|
||||
type Callback = (...args: any[]) => any;
|
||||
declare const subscribeCallback: (key: string, callbacks: Record<string, Callback[]>, callback: Callback) => () => void;
|
||||
|
||||
declare const getTimestamp: () => number;
|
||||
|
||||
declare const preset: {
|
||||
readonly isOnline: () => boolean;
|
||||
readonly isVisible: () => boolean;
|
||||
};
|
||||
declare const defaultConfigOptions: ProviderConfiguration;
|
||||
|
||||
declare const withMiddleware: (useSWR: SWRHook, middleware: Middleware) => SWRHook;
|
||||
|
||||
export { Arguments, Cache, Fetcher, FullConfiguration, GlobalState, IS_REACT_LEGACY, IS_SERVER, Key, Middleware, MutatorCallback, MutatorOptions, OBJECT, ProviderConfiguration, SWRConfiguration, SWRGlobalState, SWRHook, ScopedMutator, State, UNDEFINED, Unloader, createCacheHelper, defaultConfigOptions, getTimestamp, hasRequestAnimationFrame, initCache, internalMutate, isDocumentDefined, isFunction, isLegacyDeno, isPromiseLike, isUndefined, isWindowDefined, mergeConfigs, mergeObjects, noop, normalize, preset, rAF, slowConnection, stableHash, subscribeCallback, useIsomorphicLayoutEffect, withArgs, withMiddleware };
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
import { a as Cache, P as ProviderConfiguration, b as ScopedMutator, U as Unloader, G as GlobalState, c as State, d as FullConfiguration, A as Arguments, M as MutatorCallback, e as MutatorOptions, K as Key, f as Fetcher, g as SWRConfiguration, h as SWRHook, i as Middleware } from '../types-m1fld6v2.js';
|
||||
export { B as BareFetcher, j as BlockingData, k as Broadcaster, C as CacheData, F as FetcherResponse, I as InternalConfiguration, l as IsLoadingResponse, m as KeyLoader, n as KeyedMutator, o as Mutator, p as MutatorConfig, q as MutatorFn, r as MutatorWrapper, s as PublicConfiguration, R as RevalidateCallback, t as RevalidateEvent, u as Revalidator, v as RevalidatorOptions, S as SWRConfig, w as SWRConfigValue, x as SWRResponse, y as StateDependencies, z as StrictTupleKey, D as UnloadOptions, E as cache, H as compare, J as defaultConfig, L as mutate, N as preload, O as unload, Q as useSWRConfig } from '../types-m1fld6v2.js';
|
||||
export { e as revalidateEvents } from '../events-ny18dyc2.js';
|
||||
export { INFINITE_PREFIX } from './constants.js';
|
||||
import react from 'react';
|
||||
export { s as serialize } from '../serialize-f94ud86v.js';
|
||||
|
||||
declare const initCache: <Data = any>(provider: Cache<Data>, options?: Partial<ProviderConfiguration>) => [Cache<Data>, ScopedMutator, () => void, () => void, Unloader] | [Cache<Data>, ScopedMutator, undefined, undefined, Unloader] | undefined;
|
||||
|
||||
declare const IS_REACT_LEGACY = false;
|
||||
declare const IS_SERVER: boolean;
|
||||
declare const rAF: (f: (...args: any[]) => void) => number | ReturnType<typeof setTimeout>;
|
||||
declare const useIsomorphicLayoutEffect: typeof react.useEffect;
|
||||
declare const slowConnection: boolean | undefined;
|
||||
|
||||
declare const SWRGlobalState: WeakMap<Cache<any>, GlobalState>;
|
||||
|
||||
declare const stableHash: (arg: any) => string;
|
||||
|
||||
declare const isWindowDefined: boolean;
|
||||
declare const isDocumentDefined: boolean;
|
||||
declare const isLegacyDeno: boolean;
|
||||
declare const hasRequestAnimationFrame: () => boolean;
|
||||
declare const createCacheHelper: <Data = any, T = State<Data, any>>(cache: Cache, key: string | undefined) => readonly [() => T, (info: T) => void, (key: string, callback: (current: any, prev: any) => void) => () => void, () => any];
|
||||
|
||||
declare const noop: () => void;
|
||||
declare const UNDEFINED: undefined;
|
||||
declare const OBJECT: ObjectConstructor;
|
||||
declare const isUndefined: (v: any) => v is undefined;
|
||||
declare const isFunction: <T extends (...args: any[]) => any = (...args: any[]) => any>(v: unknown) => v is T;
|
||||
declare const mergeObjects: (a: any, b?: any) => any;
|
||||
declare const isPromiseLike: (x: unknown) => x is PromiseLike<unknown>;
|
||||
|
||||
declare const mergeConfigs: (a: Partial<FullConfiguration>, b?: Partial<FullConfiguration>) => Partial<FullConfiguration>;
|
||||
|
||||
type KeyFilter = (key?: Arguments) => boolean;
|
||||
declare function internalMutate<Data>(cache: Cache, _key: KeyFilter, _data?: Data | Promise<Data | undefined> | MutatorCallback<Data>, _opts?: boolean | MutatorOptions<Data>): Promise<Array<Data | undefined>>;
|
||||
declare function internalMutate<Data>(cache: Cache, _key: Arguments, _data?: Data | Promise<Data | undefined> | MutatorCallback<Data>, _opts?: boolean | MutatorOptions<Data>): Promise<Data | undefined>;
|
||||
|
||||
declare const normalize: <KeyType = Key, Data = any>(args: [KeyType] | [KeyType, Fetcher<Data> | null] | [KeyType, SWRConfiguration | undefined] | [KeyType, Fetcher<Data> | null, SWRConfiguration | undefined]) => [KeyType, Fetcher<Data> | null, Partial<SWRConfiguration<Data>>];
|
||||
|
||||
declare const withArgs: <SWRType>(hook: any) => SWRType;
|
||||
|
||||
type Callback = (...args: any[]) => any;
|
||||
declare const subscribeCallback: (key: string, callbacks: Record<string, Callback[]>, callback: Callback) => () => void;
|
||||
|
||||
declare const getTimestamp: () => number;
|
||||
|
||||
declare const preset: {
|
||||
readonly isOnline: () => boolean;
|
||||
readonly isVisible: () => boolean;
|
||||
};
|
||||
declare const defaultConfigOptions: ProviderConfiguration;
|
||||
|
||||
declare const withMiddleware: (useSWR: SWRHook, middleware: Middleware) => SWRHook;
|
||||
|
||||
export { Arguments, Cache, Fetcher, FullConfiguration, GlobalState, IS_REACT_LEGACY, IS_SERVER, Key, Middleware, MutatorCallback, MutatorOptions, OBJECT, ProviderConfiguration, SWRConfiguration, SWRGlobalState, SWRHook, ScopedMutator, State, UNDEFINED, Unloader, createCacheHelper, defaultConfigOptions, getTimestamp, hasRequestAnimationFrame, initCache, internalMutate, isDocumentDefined, isFunction, isLegacyDeno, isPromiseLike, isUndefined, isWindowDefined, mergeConfigs, mergeObjects, noop, normalize, preset, rAF, slowConnection, stableHash, subscribeCallback, useIsomorphicLayoutEffect, withArgs, withMiddleware };
|
||||
+165
@@ -0,0 +1,165 @@
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
|
||||
var bunchee_group_0 = require('../config-context-s960zqob.js');
|
||||
var _internal_constants = require('./constants.js');
|
||||
var React = require('react');
|
||||
|
||||
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
||||
|
||||
var React__default = /*#__PURE__*/_interopDefault(React);
|
||||
|
||||
// @ts-expect-error
|
||||
const enableDevtools = bunchee_group_0.isWindowDefined && window.__SWR_DEVTOOLS_USE__;
|
||||
const use = enableDevtools ? window.__SWR_DEVTOOLS_USE__ : [];
|
||||
const setupDevTools = ()=>{
|
||||
if (enableDevtools) {
|
||||
// @ts-expect-error
|
||||
window.__SWR_DEVTOOLS_REACT__ = React__default.default;
|
||||
}
|
||||
};
|
||||
|
||||
const normalize = (args)=>{
|
||||
return bunchee_group_0.isFunction(args[1]) ? [
|
||||
args[0],
|
||||
args[1],
|
||||
args[2] || {}
|
||||
] : [
|
||||
args[0],
|
||||
null,
|
||||
(args[1] === null ? args[2] : args[1]) || {}
|
||||
];
|
||||
};
|
||||
|
||||
const useSWRConfig = ()=>{
|
||||
const parentConfig = React.useContext(bunchee_group_0.SWRConfigContext);
|
||||
const mergedConfig = React.useMemo(()=>bunchee_group_0.mergeObjects(bunchee_group_0.defaultConfig, parentConfig), [
|
||||
parentConfig
|
||||
]);
|
||||
return mergedConfig;
|
||||
};
|
||||
|
||||
const preload = (key_, fetcher)=>{
|
||||
// preload should be a no-op on the server
|
||||
if (bunchee_group_0.IS_SERVER) {
|
||||
return undefined;
|
||||
}
|
||||
const [key, fnArg] = bunchee_group_0.serialize(key_);
|
||||
const [, , , PRELOAD] = bunchee_group_0.SWRGlobalState.get(bunchee_group_0.cache);
|
||||
// Prevent preload to be called multiple times before used.
|
||||
if (PRELOAD[key]) {
|
||||
return PRELOAD[key];
|
||||
}
|
||||
const req = fetcher(fnArg);
|
||||
PRELOAD[key] = req;
|
||||
return req;
|
||||
};
|
||||
const middleware = (useSWRNext)=>(key_, fetcher_, config)=>{
|
||||
// fetcher might be a sync function, so this should not be an async function
|
||||
const fetcher = fetcher_ && ((...args)=>{
|
||||
const [key] = bunchee_group_0.serialize(key_);
|
||||
const [, , , PRELOAD] = bunchee_group_0.SWRGlobalState.get(bunchee_group_0.cache);
|
||||
if (key.startsWith(_internal_constants.INFINITE_PREFIX)) {
|
||||
// we want the infinite fetcher to be called.
|
||||
// handling of the PRELOAD cache happens there.
|
||||
return fetcher_(...args);
|
||||
}
|
||||
const req = PRELOAD[key];
|
||||
if (bunchee_group_0.isUndefined(req)) return fetcher_(...args);
|
||||
delete PRELOAD[key];
|
||||
return req;
|
||||
});
|
||||
return useSWRNext(key_, fetcher, config);
|
||||
};
|
||||
|
||||
const BUILT_IN_MIDDLEWARE = use.concat(middleware);
|
||||
|
||||
// It's tricky to pass generic types as parameters, so we just directly override
|
||||
// the types here.
|
||||
const withArgs = (hook)=>{
|
||||
return function useSWRArgs(...args) {
|
||||
// Get the default and inherited configuration.
|
||||
const fallbackConfig = useSWRConfig();
|
||||
// Normalize arguments.
|
||||
const [key, fn, _config] = normalize(args);
|
||||
// Merge configurations.
|
||||
const config = bunchee_group_0.mergeConfigs(fallbackConfig, _config);
|
||||
// Apply middleware
|
||||
let next = hook;
|
||||
const { use } = config;
|
||||
const middleware = (use || []).concat(BUILT_IN_MIDDLEWARE);
|
||||
for(let i = middleware.length; i--;){
|
||||
next = middleware[i](next);
|
||||
}
|
||||
return next(key, fn || config.fetcher || null, config);
|
||||
};
|
||||
};
|
||||
|
||||
// Add a callback function to a list of keyed callback functions and return
|
||||
// the unsubscribe function.
|
||||
const subscribeCallback = (key, callbacks, callback)=>{
|
||||
const keyedRevalidators = callbacks[key] || (callbacks[key] = []);
|
||||
keyedRevalidators.push(callback);
|
||||
return ()=>{
|
||||
const index = keyedRevalidators.indexOf(callback);
|
||||
if (index >= 0) {
|
||||
// O(1): faster than splice
|
||||
keyedRevalidators[index] = keyedRevalidators[keyedRevalidators.length - 1];
|
||||
keyedRevalidators.pop();
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
// Create a custom hook with a middleware
|
||||
const withMiddleware = (useSWR, middleware)=>{
|
||||
return (...args)=>{
|
||||
const [key, fn, config] = normalize(args);
|
||||
const uses = (config.use || []).concat(middleware);
|
||||
return useSWR(key, fn, {
|
||||
...config,
|
||||
use: uses
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
setupDevTools();
|
||||
|
||||
exports.IS_REACT_LEGACY = bunchee_group_0.IS_REACT_LEGACY;
|
||||
exports.IS_SERVER = bunchee_group_0.IS_SERVER;
|
||||
exports.OBJECT = bunchee_group_0.OBJECT;
|
||||
exports.SWRConfig = bunchee_group_0.SWRConfig;
|
||||
exports.SWRGlobalState = bunchee_group_0.SWRGlobalState;
|
||||
exports.UNDEFINED = bunchee_group_0.UNDEFINED;
|
||||
exports.cache = bunchee_group_0.cache;
|
||||
exports.compare = bunchee_group_0.compare;
|
||||
exports.createCacheHelper = bunchee_group_0.createCacheHelper;
|
||||
exports.defaultConfig = bunchee_group_0.defaultConfig;
|
||||
exports.defaultConfigOptions = bunchee_group_0.defaultConfigOptions;
|
||||
exports.getTimestamp = bunchee_group_0.getTimestamp;
|
||||
exports.hasRequestAnimationFrame = bunchee_group_0.hasRequestAnimationFrame;
|
||||
exports.initCache = bunchee_group_0.initCache;
|
||||
exports.internalMutate = bunchee_group_0.internalMutate;
|
||||
exports.isDocumentDefined = bunchee_group_0.isDocumentDefined;
|
||||
exports.isFunction = bunchee_group_0.isFunction;
|
||||
exports.isLegacyDeno = bunchee_group_0.isLegacyDeno;
|
||||
exports.isPromiseLike = bunchee_group_0.isPromiseLike;
|
||||
exports.isUndefined = bunchee_group_0.isUndefined;
|
||||
exports.isWindowDefined = bunchee_group_0.isWindowDefined;
|
||||
exports.mergeConfigs = bunchee_group_0.mergeConfigs;
|
||||
exports.mergeObjects = bunchee_group_0.mergeObjects;
|
||||
exports.mutate = bunchee_group_0.mutate;
|
||||
exports.noop = bunchee_group_0.noop;
|
||||
exports.preset = bunchee_group_0.preset;
|
||||
exports.rAF = bunchee_group_0.rAF;
|
||||
exports.revalidateEvents = bunchee_group_0.events;
|
||||
exports.serialize = bunchee_group_0.serialize;
|
||||
exports.slowConnection = bunchee_group_0.slowConnection;
|
||||
exports.stableHash = bunchee_group_0.stableHash;
|
||||
exports.unload = bunchee_group_0.unload;
|
||||
exports.useIsomorphicLayoutEffect = bunchee_group_0.useIsomorphicLayoutEffect;
|
||||
exports.INFINITE_PREFIX = _internal_constants.INFINITE_PREFIX;
|
||||
exports.normalize = normalize;
|
||||
exports.preload = preload;
|
||||
exports.subscribeCallback = subscribeCallback;
|
||||
exports.useSWRConfig = useSWRConfig;
|
||||
exports.withArgs = withArgs;
|
||||
exports.withMiddleware = withMiddleware;
|
||||
+121
@@ -0,0 +1,121 @@
|
||||
import { p as isWindowDefined, e as isFunction, q as SWRConfigContext, m as mergeObjects, d as defaultConfig, s as serialize, a as SWRGlobalState, o as cache, i as isUndefined, I as IS_SERVER, t as mergeConfigs } from '../config-context-ext53wcz.mjs';
|
||||
export { j as IS_REACT_LEGACY, O as OBJECT, S as SWRConfig, U as UNDEFINED, v as compare, c as createCacheHelper, w as defaultConfigOptions, g as getTimestamp, x as hasRequestAnimationFrame, y as initCache, f as internalMutate, z as isDocumentDefined, A as isLegacyDeno, b as isPromiseLike, k as mutate, n as noop, B as preset, r as rAF, C as revalidateEvents, D as slowConnection, G as stableHash, l as unload, u as useIsomorphicLayoutEffect } from '../config-context-ext53wcz.mjs';
|
||||
import { INFINITE_PREFIX } from './constants.mjs';
|
||||
import React, { useContext, useMemo } from 'react';
|
||||
|
||||
// @ts-expect-error
|
||||
const enableDevtools = isWindowDefined && window.__SWR_DEVTOOLS_USE__;
|
||||
const use = enableDevtools ? window.__SWR_DEVTOOLS_USE__ : [];
|
||||
const setupDevTools = ()=>{
|
||||
if (enableDevtools) {
|
||||
// @ts-expect-error
|
||||
window.__SWR_DEVTOOLS_REACT__ = React;
|
||||
}
|
||||
};
|
||||
|
||||
const normalize = (args)=>{
|
||||
return isFunction(args[1]) ? [
|
||||
args[0],
|
||||
args[1],
|
||||
args[2] || {}
|
||||
] : [
|
||||
args[0],
|
||||
null,
|
||||
(args[1] === null ? args[2] : args[1]) || {}
|
||||
];
|
||||
};
|
||||
|
||||
const useSWRConfig = ()=>{
|
||||
const parentConfig = useContext(SWRConfigContext);
|
||||
const mergedConfig = useMemo(()=>mergeObjects(defaultConfig, parentConfig), [
|
||||
parentConfig
|
||||
]);
|
||||
return mergedConfig;
|
||||
};
|
||||
|
||||
const preload = (key_, fetcher)=>{
|
||||
// preload should be a no-op on the server
|
||||
if (IS_SERVER) {
|
||||
return undefined;
|
||||
}
|
||||
const [key, fnArg] = serialize(key_);
|
||||
const [, , , PRELOAD] = SWRGlobalState.get(cache);
|
||||
// Prevent preload to be called multiple times before used.
|
||||
if (PRELOAD[key]) {
|
||||
return PRELOAD[key];
|
||||
}
|
||||
const req = fetcher(fnArg);
|
||||
PRELOAD[key] = req;
|
||||
return req;
|
||||
};
|
||||
const middleware = (useSWRNext)=>(key_, fetcher_, config)=>{
|
||||
// fetcher might be a sync function, so this should not be an async function
|
||||
const fetcher = fetcher_ && ((...args)=>{
|
||||
const [key] = serialize(key_);
|
||||
const [, , , PRELOAD] = SWRGlobalState.get(cache);
|
||||
if (key.startsWith(INFINITE_PREFIX)) {
|
||||
// we want the infinite fetcher to be called.
|
||||
// handling of the PRELOAD cache happens there.
|
||||
return fetcher_(...args);
|
||||
}
|
||||
const req = PRELOAD[key];
|
||||
if (isUndefined(req)) return fetcher_(...args);
|
||||
delete PRELOAD[key];
|
||||
return req;
|
||||
});
|
||||
return useSWRNext(key_, fetcher, config);
|
||||
};
|
||||
|
||||
const BUILT_IN_MIDDLEWARE = use.concat(middleware);
|
||||
|
||||
// It's tricky to pass generic types as parameters, so we just directly override
|
||||
// the types here.
|
||||
const withArgs = (hook)=>{
|
||||
return function useSWRArgs(...args) {
|
||||
// Get the default and inherited configuration.
|
||||
const fallbackConfig = useSWRConfig();
|
||||
// Normalize arguments.
|
||||
const [key, fn, _config] = normalize(args);
|
||||
// Merge configurations.
|
||||
const config = mergeConfigs(fallbackConfig, _config);
|
||||
// Apply middleware
|
||||
let next = hook;
|
||||
const { use } = config;
|
||||
const middleware = (use || []).concat(BUILT_IN_MIDDLEWARE);
|
||||
for(let i = middleware.length; i--;){
|
||||
next = middleware[i](next);
|
||||
}
|
||||
return next(key, fn || config.fetcher || null, config);
|
||||
};
|
||||
};
|
||||
|
||||
// Add a callback function to a list of keyed callback functions and return
|
||||
// the unsubscribe function.
|
||||
const subscribeCallback = (key, callbacks, callback)=>{
|
||||
const keyedRevalidators = callbacks[key] || (callbacks[key] = []);
|
||||
keyedRevalidators.push(callback);
|
||||
return ()=>{
|
||||
const index = keyedRevalidators.indexOf(callback);
|
||||
if (index >= 0) {
|
||||
// O(1): faster than splice
|
||||
keyedRevalidators[index] = keyedRevalidators[keyedRevalidators.length - 1];
|
||||
keyedRevalidators.pop();
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
// Create a custom hook with a middleware
|
||||
const withMiddleware = (useSWR, middleware)=>{
|
||||
return (...args)=>{
|
||||
const [key, fn, config] = normalize(args);
|
||||
const uses = (config.use || []).concat(middleware);
|
||||
return useSWR(key, fn, {
|
||||
...config,
|
||||
use: uses
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
setupDevTools();
|
||||
|
||||
export { INFINITE_PREFIX, IS_SERVER, SWRGlobalState, cache, defaultConfig, isFunction, isUndefined, isWindowDefined, mergeConfigs, mergeObjects, normalize, preload, serialize, subscribeCallback, useSWRConfig, withArgs, withMiddleware };
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
export { s as serialize } from '../serialize-bwklnvdf.js';
|
||||
export { S as SWRConfig } from '../types-l2s7dphv.js';
|
||||
export { INFINITE_PREFIX } from './constants.mjs';
|
||||
export { p as preload } from '../server-preload-cqglm8k4.js';
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
export { s as serialize } from '../serialize-f94ud86v.js';
|
||||
import { K as Key, B as BareFetcher, F as FetcherResponse, C as CacheData } from '../types-m1fld6v2.js';
|
||||
export { S as SWRConfig } from '../types-m1fld6v2.js';
|
||||
export { INFINITE_PREFIX } from './constants.js';
|
||||
|
||||
type PreloadFetcher<Data = unknown, SWRKey extends Key = Key> = SWRKey extends () => infer Arg ? (arg: Arg) => FetcherResponse<Data> : SWRKey extends infer Arg ? (arg: Arg) => FetcherResponse<Data> : never;
|
||||
declare const preload: <Data = any, SWRKey extends Key = Key, Fetcher extends BareFetcher = PreloadFetcher<Data, SWRKey>>(key_: SWRKey, fetcher: Fetcher) => CacheData<Data>;
|
||||
|
||||
export { preload };
|
||||
+113
@@ -0,0 +1,113 @@
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
|
||||
var index_js = require('./index.js');
|
||||
var constants_js = require('./constants.js');
|
||||
|
||||
// Shared state between server components and client components
|
||||
const noop = ()=>{};
|
||||
// Using noop() as the undefined value as undefined can be replaced
|
||||
// by something else. Prettier ignore and extra parentheses are necessary here
|
||||
// to ensure that tsc doesn't remove the __NOINLINE__ comment.
|
||||
// prettier-ignore
|
||||
const UNDEFINED = /*#__NOINLINE__*/ noop();
|
||||
const OBJECT = Object;
|
||||
const isUndefined = (v)=>v === UNDEFINED;
|
||||
const isFunction = (v)=>typeof v == 'function';
|
||||
|
||||
// use WeakMap to store the object->key mapping
|
||||
// so the objects can be garbage collected.
|
||||
// WeakMap uses a hashtable under the hood, so the lookup
|
||||
// complexity is almost O(1).
|
||||
const table = new WeakMap();
|
||||
const getTypeName = (value)=>OBJECT.prototype.toString.call(value);
|
||||
const isObjectTypeName = (typeName, type)=>typeName === `[object ${type}]`;
|
||||
// counter of the key
|
||||
let counter = 0;
|
||||
// A stable hash implementation that supports:
|
||||
// - Fast and ensures unique hash properties
|
||||
// - Handles unserializable values
|
||||
// - Handles object key ordering
|
||||
// - Generates short results
|
||||
//
|
||||
// This is not a serialization function, and the result is not guaranteed to be
|
||||
// parsable.
|
||||
const stableHash = (arg)=>{
|
||||
const type = typeof arg;
|
||||
const typeName = getTypeName(arg);
|
||||
const isDate = isObjectTypeName(typeName, 'Date');
|
||||
const isRegex = isObjectTypeName(typeName, 'RegExp');
|
||||
const isPlainObject = isObjectTypeName(typeName, 'Object');
|
||||
let result;
|
||||
let index;
|
||||
if (OBJECT(arg) === arg && !isDate && !isRegex) {
|
||||
// Object/function, not null/date/regexp. Use WeakMap to store the id first.
|
||||
// If it's already hashed, directly return the result.
|
||||
result = table.get(arg);
|
||||
if (result) return result;
|
||||
// Store the hash first for circular reference detection before entering the
|
||||
// recursive `stableHash` calls.
|
||||
// For other objects like set and map, we use this id directly as the hash.
|
||||
result = ++counter + '~';
|
||||
table.set(arg, result);
|
||||
if (Array.isArray(arg)) {
|
||||
// Array.
|
||||
result = '@';
|
||||
for(index = 0; index < arg.length; index++){
|
||||
result += stableHash(arg[index]) + ',';
|
||||
}
|
||||
table.set(arg, result);
|
||||
}
|
||||
if (isPlainObject) {
|
||||
// Object, sort keys.
|
||||
result = '#';
|
||||
const keys = OBJECT.keys(arg).sort();
|
||||
while(!isUndefined(index = keys.pop())){
|
||||
if (!isUndefined(arg[index])) {
|
||||
result += index + ':' + stableHash(arg[index]) + ',';
|
||||
}
|
||||
}
|
||||
table.set(arg, result);
|
||||
}
|
||||
} else {
|
||||
result = isDate ? arg.toJSON() : type == 'symbol' ? arg.toString() : type == 'string' ? JSON.stringify(arg) : '' + arg;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
const serialize = (key)=>{
|
||||
if (isFunction(key)) {
|
||||
try {
|
||||
key = key();
|
||||
} catch (err) {
|
||||
// dependencies not ready
|
||||
key = '';
|
||||
}
|
||||
}
|
||||
// Use the original key as the argument of fetcher. This can be a string or an
|
||||
// array of values.
|
||||
const args = key;
|
||||
// If key is not falsy, or not an empty array, hash it.
|
||||
key = typeof key == 'string' ? key : (Array.isArray(key) ? key.length : key) ? stableHash(key) : '';
|
||||
return [
|
||||
key,
|
||||
args
|
||||
];
|
||||
};
|
||||
|
||||
const preload = (key_, fetcher)=>{
|
||||
const [key, fnArg] = serialize(key_);
|
||||
return {
|
||||
[key]: fetcher(fnArg)
|
||||
};
|
||||
};
|
||||
|
||||
Object.defineProperty(exports, "SWRConfig", {
|
||||
enumerable: true,
|
||||
get: function () { return index_js.SWRConfig; }
|
||||
});
|
||||
Object.defineProperty(exports, "INFINITE_PREFIX", {
|
||||
enumerable: true,
|
||||
get: function () { return constants_js.INFINITE_PREFIX; }
|
||||
});
|
||||
exports.preload = preload;
|
||||
exports.serialize = serialize;
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
export { s as serialize } from '../serialize-hqt5osie.mjs';
|
||||
export { SWRConfig } from './index.mjs';
|
||||
export { INFINITE_PREFIX } from './constants.mjs';
|
||||
export { p as preload } from '../server-preload-jludd90x.mjs';
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
export { A as Arguments, B as BareFetcher, m as BlockingData, n as Broadcaster, h as Cache, C as CacheData, l as Fetcher, F as FetcherResponse, k as FullConfiguration, G as GlobalState, I as InternalConfiguration, o as IsLoadingResponse, K as Key, p as KeyLoader, q as KeyedMutator, M as Middleware, r as Mutator, d as MutatorCallback, s as MutatorConfig, t as MutatorFn, g as MutatorOptions, v as MutatorWrapper, P as ProviderConfiguration, w as PublicConfiguration, R as RevalidateCallback, x as RevalidateEvent, y as Revalidator, z as RevalidatorOptions, b as SWRConfigValue, e as SWRConfiguration, a as SWRHook, c as SWRResponse, i as ScopedMutator, j as State, D as StateDependencies, f as StrictTupleKey, E as UnloadOptions, U as Unloader } from '../types-l2s7dphv.js';
|
||||
import '../events-ny18dyc2.js';
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
export { A as Arguments, B as BareFetcher, j as BlockingData, k as Broadcaster, a as Cache, C as CacheData, f as Fetcher, F as FetcherResponse, d as FullConfiguration, G as GlobalState, I as InternalConfiguration, l as IsLoadingResponse, K as Key, m as KeyLoader, n as KeyedMutator, i as Middleware, o as Mutator, M as MutatorCallback, p as MutatorConfig, q as MutatorFn, e as MutatorOptions, r as MutatorWrapper, P as ProviderConfiguration, s as PublicConfiguration, R as RevalidateCallback, t as RevalidateEvent, u as Revalidator, v as RevalidatorOptions, w as SWRConfigValue, g as SWRConfiguration, h as SWRHook, x as SWRResponse, b as ScopedMutator, c as State, y as StateDependencies, z as StrictTupleKey, D as UnloadOptions, U as Unloader } from '../types-m1fld6v2.js';
|
||||
import '../events-ny18dyc2.js';
|
||||
+1
@@ -0,0 +1 @@
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
|
||||
Reference in new issue
Block a user