97 lines
3.4 KiB
JavaScript
97 lines
3.4 KiB
JavaScript
export { S as SWRConfig } from './config-12s-Bi4rgVRk.mjs';
|
|
|
|
// 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 unstable_serialize = (key)=>serialize(key)[0];
|
|
|
|
export { unstable_serialize };
|