@dark-engine/platform-server
Version:
Dark renderer for server
77 lines (76 loc) • 2.29 kB
JavaScript
import {
ROOT,
Fiber,
CREATE_EFFECT_TAG,
UPDATE_EFFECT_TAG,
platform,
flatten,
detectIsUndefined,
detectIsFunction,
trueFn,
TagVirtualNode,
TaskPriority,
createReplacer,
setRootId,
$$scope,
scheduler,
} from '@dark-engine/core';
import { createNativeElement, toggle, commit, finishCommit } from '../dom';
import { detectIsBrowser, illegal, removeContent } from '../utils';
const isBrowser = detectIsBrowser();
const roots = new Map();
const raf = isBrowser && requestAnimationFrame.bind(this);
const caf = isBrowser && cancelAnimationFrame.bind(this);
const spawn = raf;
let isInjected = false;
function inject() {
platform.createElement = createNativeElement;
platform.toggle = toggle;
platform.raf = raf;
platform.caf = caf;
platform.spawn = spawn;
platform.commit = commit;
platform.finishCommit = finishCommit;
platform.detectIsDynamic = trueFn;
isInjected = true;
}
function render(element, container, hydrate) {
!isInjected && inject();
if (process.env.NODE_ENV !== 'production') {
if (!(container instanceof Element) && !(container instanceof Document)) {
illegal(`The render receives a valid element as container!`);
}
}
const isMounted = !detectIsUndefined(roots.get(container));
const isHydration = detectIsFunction(hydrate);
let rootId = null;
if (!isMounted) {
rootId = roots.size;
roots.set(container, rootId);
!isHydration && removeContent(container);
} else {
rootId = roots.get(container);
}
const $scope = $$scope(rootId);
if ($scope?.getIsInsertionEffect()) return;
const callback = () => {
setRootId(rootId);
const $scope = $$scope();
const rootFiber = $scope.getRoot();
const isUpdate = Boolean(rootFiber);
const fiber = new Fiber().mutate({
el: container,
inst: new TagVirtualNode(ROOT, {}, flatten([element || createReplacer()])),
alt: rootFiber,
tag: isUpdate ? UPDATE_EFFECT_TAG : CREATE_EFFECT_TAG,
});
$scope.resetMount();
$scope.setWorkInProgress(fiber);
$scope.setIsHydration(isHydration);
$scope.setUnitOfWork(fiber);
isHydration && hydrate();
};
scheduler.schedule(callback, { priority: TaskPriority.NORMAL });
}
export { render, roots, inject };
//# sourceMappingURL=render.js.map