preact
This commit is contained in:
parent
6acf267d48
commit
b7614eb3f8
4794 changed files with 1280808 additions and 1546 deletions
36
frontend/node_modules/@prefresh/core/src/computeKey.js
generated
vendored
Normal file
36
frontend/node_modules/@prefresh/core/src/computeKey.js
generated
vendored
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import { signaturesForType } from './runtime/signaturesForType';
|
||||
|
||||
/**
|
||||
*
|
||||
* This part has been vendored from "react-refresh"
|
||||
* https://github.com/facebook/react/blob/master/packages/react-refresh/src/ReactFreshRuntime.js#L83
|
||||
*/
|
||||
export const computeKey = signature => {
|
||||
let fullKey = signature.key;
|
||||
let hooks;
|
||||
|
||||
try {
|
||||
hooks = signature.getCustomHooks();
|
||||
} catch (err) {
|
||||
signature.forceReset = true;
|
||||
return fullKey;
|
||||
}
|
||||
|
||||
for (let i = 0; i < hooks.length; i++) {
|
||||
const hook = hooks[i];
|
||||
if (typeof hook !== 'function') {
|
||||
signature.forceReset = true;
|
||||
return fullKey;
|
||||
}
|
||||
|
||||
const nestedHookSignature = signaturesForType.get(hook);
|
||||
if (nestedHookSignature === undefined) continue;
|
||||
|
||||
const nestedHookKey = computeKey(nestedHookSignature);
|
||||
if (nestedHookSignature.forceReset) signature.forceReset = true;
|
||||
|
||||
fullKey += '\n---\n' + nestedHookKey;
|
||||
}
|
||||
|
||||
return fullKey;
|
||||
};
|
||||
14
frontend/node_modules/@prefresh/core/src/constants.js
generated
vendored
Normal file
14
frontend/node_modules/@prefresh/core/src/constants.js
generated
vendored
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
export const VNODE_COMPONENT = '__c';
|
||||
export const NAMESPACE = '__PREFRESH__';
|
||||
export const COMPONENT_HOOKS = '__H';
|
||||
export const HOOKS_LIST = '__';
|
||||
export const EFFECTS_LIST = '__h';
|
||||
export const RERENDER_COUNT = '__r';
|
||||
export const CATCH_ERROR_OPTION = '__e';
|
||||
export const COMPONENT_DIRTY = '__d';
|
||||
export const COMPONENT_BITS = '__g';
|
||||
export const VNODE_DOM = '__e';
|
||||
export const VNODE_CHILDREN = '__k';
|
||||
export const HOOK_VALUE = '__';
|
||||
export const HOOK_ARGS = '__H';
|
||||
export const HOOK_CLEANUP = '__c';
|
||||
215
frontend/node_modules/@prefresh/core/src/index.js
generated
vendored
Normal file
215
frontend/node_modules/@prefresh/core/src/index.js
generated
vendored
Normal file
|
|
@ -0,0 +1,215 @@
|
|||
// Options for Preact.
|
||||
import './runtime/catchError';
|
||||
import './runtime/debounceRendering';
|
||||
import './runtime/vnode';
|
||||
import './runtime/unmount';
|
||||
|
||||
import { Component } from 'preact';
|
||||
|
||||
import {
|
||||
VNODE_COMPONENT,
|
||||
NAMESPACE,
|
||||
HOOKS_LIST,
|
||||
EFFECTS_LIST,
|
||||
COMPONENT_HOOKS,
|
||||
HOOK_ARGS,
|
||||
HOOK_VALUE,
|
||||
HOOK_CLEANUP,
|
||||
} from './constants';
|
||||
import { computeKey } from './computeKey';
|
||||
import { vnodesForComponent, mappedVNodes, lastSeen } from './runtime/vnodesForComponent';
|
||||
import { signaturesForType } from './runtime/signaturesForType';
|
||||
|
||||
let typesById = new Map();
|
||||
let pendingUpdates = [];
|
||||
|
||||
function sign(type, key, forceReset, getCustomHooks, status) {
|
||||
if (type) {
|
||||
let signature = signaturesForType.get(type);
|
||||
if (status === 'begin') {
|
||||
signaturesForType.set(type, {
|
||||
type,
|
||||
key,
|
||||
forceReset,
|
||||
getCustomHooks: getCustomHooks || (() => []),
|
||||
});
|
||||
|
||||
return 'needsHooks';
|
||||
} else if (status === 'needsHooks') {
|
||||
signature.fullKey = computeKey(signature);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function replaceComponent(OldType, NewType, resetHookState) {
|
||||
const vnodes = vnodesForComponent.get(OldType);
|
||||
if (!vnodes) return;
|
||||
|
||||
// migrate the list to our new constructor reference
|
||||
vnodesForComponent.delete(OldType);
|
||||
vnodesForComponent.set(NewType, vnodes);
|
||||
|
||||
mappedVNodes.set(OldType, NewType);
|
||||
|
||||
pendingUpdates = pendingUpdates.filter(p => p[0] !== OldType);
|
||||
|
||||
vnodes.forEach(node => {
|
||||
let vnode = node;
|
||||
if (vnode && vnode.__v && !vnode.__c && lastSeen.has(vnode.__v)) {
|
||||
vnode = lastSeen.get(vnode.__v);
|
||||
lastSeen.delete(vnode.__v);
|
||||
}
|
||||
|
||||
// if the vnode
|
||||
if (!vnode || !vnode.__c || !vnode.__c.__P) return;
|
||||
// update the type in-place to reference the new component
|
||||
vnode.type = NewType;
|
||||
|
||||
if (vnode[VNODE_COMPONENT]) {
|
||||
vnode[VNODE_COMPONENT].constructor = vnode.type;
|
||||
|
||||
try {
|
||||
if (vnode[VNODE_COMPONENT] instanceof OldType) {
|
||||
const oldInst = vnode[VNODE_COMPONENT];
|
||||
|
||||
const newInst = new NewType(
|
||||
vnode[VNODE_COMPONENT].props,
|
||||
vnode[VNODE_COMPONENT].context
|
||||
);
|
||||
|
||||
vnode[VNODE_COMPONENT] = newInst;
|
||||
// copy old properties onto the new instance.
|
||||
// - Objects (including refs) in the new instance are updated with their old values
|
||||
// - Missing or null properties are restored to their old values
|
||||
// - Updated Functions are not reverted
|
||||
// - Scalars are copied
|
||||
for (let i in oldInst) {
|
||||
const type = typeof oldInst[i];
|
||||
if (!(i in newInst)) {
|
||||
newInst[i] = oldInst[i];
|
||||
} else if (type !== 'function' && typeof newInst[i] === type) {
|
||||
if (
|
||||
type === 'object' &&
|
||||
newInst[i] != null &&
|
||||
newInst[i].constructor === oldInst[i].constructor
|
||||
) {
|
||||
Object.assign(newInst[i], oldInst[i]);
|
||||
} else {
|
||||
newInst[i] = oldInst[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
/* Functional component */
|
||||
vnode[VNODE_COMPONENT].constructor = NewType;
|
||||
}
|
||||
|
||||
if (resetHookState) {
|
||||
if (
|
||||
vnode[VNODE_COMPONENT][COMPONENT_HOOKS] &&
|
||||
vnode[VNODE_COMPONENT][COMPONENT_HOOKS][HOOKS_LIST] &&
|
||||
vnode[VNODE_COMPONENT][COMPONENT_HOOKS][HOOKS_LIST].length
|
||||
) {
|
||||
vnode[VNODE_COMPONENT][COMPONENT_HOOKS][HOOKS_LIST].forEach(
|
||||
possibleEffect => {
|
||||
if (
|
||||
possibleEffect[HOOK_CLEANUP] &&
|
||||
typeof possibleEffect[HOOK_CLEANUP] === 'function'
|
||||
) {
|
||||
possibleEffect[HOOK_CLEANUP]();
|
||||
possibleEffect[HOOK_CLEANUP] = undefined;
|
||||
} else if (
|
||||
possibleEffect[HOOK_ARGS] &&
|
||||
possibleEffect[HOOK_VALUE] &&
|
||||
Object.keys(possibleEffect).length === 3
|
||||
) {
|
||||
const cleanupKey = Object.keys(possibleEffect).find(
|
||||
key => key !== HOOK_ARGS && key !== HOOK_VALUE
|
||||
);
|
||||
if (
|
||||
cleanupKey &&
|
||||
typeof possibleEffect[cleanupKey] == 'function'
|
||||
) {
|
||||
possibleEffect[cleanupKey]();
|
||||
possibleEffect[cleanupKey] = undefined;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
vnode[VNODE_COMPONENT][COMPONENT_HOOKS] = {
|
||||
[HOOKS_LIST]: [],
|
||||
[EFFECTS_LIST]: [],
|
||||
};
|
||||
} else if (
|
||||
vnode[VNODE_COMPONENT][COMPONENT_HOOKS] &&
|
||||
vnode[VNODE_COMPONENT][COMPONENT_HOOKS][HOOKS_LIST] &&
|
||||
vnode[VNODE_COMPONENT][COMPONENT_HOOKS][HOOKS_LIST].length
|
||||
) {
|
||||
vnode[VNODE_COMPONENT][COMPONENT_HOOKS][HOOKS_LIST].forEach(
|
||||
possibleEffect => {
|
||||
if (
|
||||
possibleEffect[HOOK_CLEANUP] &&
|
||||
typeof possibleEffect[HOOK_CLEANUP] === 'function'
|
||||
) {
|
||||
possibleEffect[HOOK_CLEANUP]();
|
||||
possibleEffect[HOOK_CLEANUP] = undefined;
|
||||
} else if (
|
||||
possibleEffect[HOOK_ARGS] &&
|
||||
possibleEffect[HOOK_VALUE] &&
|
||||
Object.keys(possibleEffect).length === 3
|
||||
) {
|
||||
const cleanupKey = Object.keys(possibleEffect).find(
|
||||
key => key !== HOOK_ARGS && key !== HOOK_VALUE
|
||||
);
|
||||
if (cleanupKey && typeof possibleEffect[cleanupKey] == 'function')
|
||||
possibleEffect[cleanupKey]();
|
||||
possibleEffect[cleanupKey] = undefined;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
vnode[VNODE_COMPONENT][COMPONENT_HOOKS][HOOKS_LIST].forEach(hook => {
|
||||
if (hook.__H && Array.isArray(hook.__H)) {
|
||||
hook.__H = undefined;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Component.prototype.forceUpdate.call(vnode[VNODE_COMPONENT]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
self[NAMESPACE] = {
|
||||
getSignature: type => signaturesForType.get(type),
|
||||
register: (type, id) => {
|
||||
if (typeof type !== 'function') return;
|
||||
|
||||
if (typesById.has(id)) {
|
||||
const existing = typesById.get(id);
|
||||
if (existing !== type) {
|
||||
pendingUpdates.push([existing, type]);
|
||||
typesById.set(id, type);
|
||||
}
|
||||
} else {
|
||||
typesById.set(id, type);
|
||||
}
|
||||
|
||||
if (!signaturesForType.has(type)) {
|
||||
signaturesForType.set(type, {
|
||||
getCustomHooks: () => [],
|
||||
type,
|
||||
});
|
||||
}
|
||||
},
|
||||
getPendingUpdates: () => pendingUpdates,
|
||||
flush: () => {
|
||||
pendingUpdates = [];
|
||||
},
|
||||
replaceComponent,
|
||||
sign,
|
||||
computeKey,
|
||||
};
|
||||
14
frontend/node_modules/@prefresh/core/src/runtime/catchError.js
generated
vendored
Normal file
14
frontend/node_modules/@prefresh/core/src/runtime/catchError.js
generated
vendored
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import { options } from 'preact';
|
||||
import {
|
||||
CATCH_ERROR_OPTION,
|
||||
} from '../constants';
|
||||
import { isDirty, unsetDirty } from '../utils';
|
||||
|
||||
const oldCatchError = options[CATCH_ERROR_OPTION];
|
||||
options[CATCH_ERROR_OPTION] = (error, vnode, oldVNode, info) => {
|
||||
if (isDirty(vnode)) {
|
||||
unsetDirty(vnode);
|
||||
}
|
||||
|
||||
if (oldCatchError) oldCatchError(error, vnode, oldVNode, info);
|
||||
};
|
||||
19
frontend/node_modules/@prefresh/core/src/runtime/debounceRendering.js
generated
vendored
Normal file
19
frontend/node_modules/@prefresh/core/src/runtime/debounceRendering.js
generated
vendored
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import { options } from 'preact';
|
||||
|
||||
import { RERENDER_COUNT } from '../constants';
|
||||
|
||||
const defer =
|
||||
typeof Promise == 'function'
|
||||
? Promise.prototype.then.bind(Promise.resolve())
|
||||
: setTimeout;
|
||||
|
||||
options.debounceRendering = process => {
|
||||
defer(() => {
|
||||
try {
|
||||
process();
|
||||
} catch (e) {
|
||||
process[RERENDER_COUNT] = 0;
|
||||
throw e;
|
||||
}
|
||||
});
|
||||
};
|
||||
2
frontend/node_modules/@prefresh/core/src/runtime/signaturesForType.js
generated
vendored
Normal file
2
frontend/node_modules/@prefresh/core/src/runtime/signaturesForType.js
generated
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
// Signatures for functional components and custom hooks.
|
||||
export const signaturesForType = new WeakMap();
|
||||
18
frontend/node_modules/@prefresh/core/src/runtime/unmount.js
generated
vendored
Normal file
18
frontend/node_modules/@prefresh/core/src/runtime/unmount.js
generated
vendored
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import { options } from 'preact';
|
||||
import { vnodesForComponent } from './vnodesForComponent';
|
||||
|
||||
const oldUnmount = options.unmount;
|
||||
options.unmount = vnode => {
|
||||
const type = (vnode || {}).type;
|
||||
if (typeof type === 'function' && vnodesForComponent.has(type)) {
|
||||
const vnodes = vnodesForComponent.get(type);
|
||||
if (vnodes) {
|
||||
const index = vnodes.indexOf(vnode);
|
||||
if (index !== -1) {
|
||||
vnodes.splice(index, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (oldUnmount) oldUnmount(vnode);
|
||||
};
|
||||
71
frontend/node_modules/@prefresh/core/src/runtime/vnode.js
generated
vendored
Normal file
71
frontend/node_modules/@prefresh/core/src/runtime/vnode.js
generated
vendored
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
import { options } from 'preact';
|
||||
import {
|
||||
vnodesForComponent,
|
||||
mappedVNodes,
|
||||
lastSeen,
|
||||
} from './vnodesForComponent';
|
||||
import { VNODE_COMPONENT } from '../constants';
|
||||
|
||||
const getMappedVnode = type => {
|
||||
if (mappedVNodes.has(type)) {
|
||||
return getMappedVnode(mappedVNodes.get(type));
|
||||
}
|
||||
|
||||
return type;
|
||||
};
|
||||
|
||||
const BUILT_IN_COMPONENTS = ['Fragment', 'Suspense', 'SuspenseList'];
|
||||
|
||||
const isBuiltIn = type => {
|
||||
return BUILT_IN_COMPONENTS.includes(type.name);
|
||||
};
|
||||
|
||||
const oldVnode = options.vnode;
|
||||
options.vnode = vnode => {
|
||||
if (vnode && typeof vnode.type === 'function' && !isBuiltIn(vnode.type)) {
|
||||
const foundType = getMappedVnode(vnode.type);
|
||||
if (foundType !== vnode.type) {
|
||||
vnode.type = foundType;
|
||||
if (
|
||||
vnode[VNODE_COMPONENT] &&
|
||||
'prototype' in vnode.type &&
|
||||
vnode.type.prototype.render
|
||||
) {
|
||||
vnode[VNODE_COMPONENT].constructor = vnode.type;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (oldVnode) oldVnode(vnode);
|
||||
};
|
||||
|
||||
const oldDiff = options.__b;
|
||||
options.__b = vnode => {
|
||||
if (vnode && typeof vnode.type === 'function' && !isBuiltIn(vnode.type)) {
|
||||
const vnodes = vnodesForComponent.get(vnode.type);
|
||||
if (!vnodes) {
|
||||
vnodesForComponent.set(vnode.type, [vnode]);
|
||||
} else {
|
||||
vnodes.push(vnode);
|
||||
}
|
||||
}
|
||||
|
||||
if (oldDiff) oldDiff(vnode);
|
||||
};
|
||||
|
||||
const oldDiffed = options.diffed;
|
||||
options.diffed = vnode => {
|
||||
if (vnode && typeof vnode.type === 'function') {
|
||||
const vnodes = vnodesForComponent.get(vnode.type);
|
||||
lastSeen.set(vnode.__v, vnode);
|
||||
if (vnodes) {
|
||||
const matchingDom = vnodes.filter(p => p.__c === vnode.__c);
|
||||
if (matchingDom.length > 1) {
|
||||
const i = vnodes.findIndex(p => p === matchingDom[0]);
|
||||
vnodes.splice(i, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (oldDiffed) oldDiffed(vnode);
|
||||
};
|
||||
4
frontend/node_modules/@prefresh/core/src/runtime/vnodesForComponent.js
generated
vendored
Normal file
4
frontend/node_modules/@prefresh/core/src/runtime/vnodesForComponent.js
generated
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
// all vnodes referencing a given constructor
|
||||
export const vnodesForComponent = new WeakMap();
|
||||
export const mappedVNodes = new WeakMap();
|
||||
export const lastSeen = new Map();
|
||||
21
frontend/node_modules/@prefresh/core/src/utils.js
generated
vendored
Normal file
21
frontend/node_modules/@prefresh/core/src/utils.js
generated
vendored
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import { COMPONENT_BITS, COMPONENT_DIRTY, VNODE_COMPONENT } from "./constants"
|
||||
|
||||
/** Component is queued for update */
|
||||
export const COMPONENT_DIRTY_BIT = 1 << 3;
|
||||
|
||||
export const isDirty = vnode => {
|
||||
if (vnode[VNODE_COMPONENT] && vnode[VNODE_COMPONENT][COMPONENT_DIRTY]) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (vnode[VNODE_COMPONENT] && (vnode[VNODE_COMPONENT][COMPONENT_BITS] & COMPONENT_DIRTY_BIT)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
export const unsetDirty = vnode => {
|
||||
if (vnode[VNODE_COMPONENT]) {
|
||||
if (vnode[VNODE_COMPONENT][COMPONENT_DIRTY]) vnode[VNODE_COMPONENT][COMPONENT_DIRTY] = false;
|
||||
if (vnode[VNODE_COMPONENT][COMPONENT_BITS]) vnode[VNODE_COMPONENT][COMPONENT_BITS] &= ~COMPONENT_DIRTY_BIT;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue