UNPKG

@dark-engine/core

Version:

The lightweight and powerful UI rendering engine without dependencies and written in TypeScript (Browser, Node.js, Android, iOS, Windows, Linux, macOS)

37 lines (36 loc) 1.22 kB
import { detectIsFunction, illegal } from '../utils'; import { __useLoc as useLoc } from '../internal'; import { scheduler } from '../scheduler'; import { useState } from '../use-state'; import { useEvent } from '../use-event'; import { $$scope } from '../scope'; function startTransition(callback) { const $scope = $$scope(); const id = scheduler.getLastId(); $scope.setIsTransition(true); try { callback(); if (id === scheduler.getLastId()) { const fn = $scope.getOnTransitionEnd(); detectIsFunction(fn) && fn(() => false); illegal('startTransition must plan a new render!'); } } finally { $scope.setIsTransition(false); } } function useTransition() { const [isPending, setIsPending] = useState(false); const loc = useLoc(); const $scope = $$scope(); const $startTransition = useEvent(callback => { setIsPending(true); $scope.setOnTransitionEnd(fn => setIsPending(fn(loc()))); startTransition(callback); $scope.setOnTransitionEnd(null); }); const $isPending = scheduler.detectIsTransition() ? false : isPending; return [$isPending, $startTransition]; } export { startTransition, useTransition }; //# sourceMappingURL=start-transition.js.map