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)

62 lines (61 loc) 2.08 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); exports.createEffect = exports.dropEffects = exports.useEffect = exports.EffectType = void 0; const internal_1 = require('../internal'); const constants_1 = require('../constants'); const walk_1 = require('../walk'); const utils_1 = require('../utils'); const use_memo_1 = require('../use-memo'); const scope_1 = require('../scope'); const $$effect = Symbol('effect'); function createEffect(type) { return (effect, deps = [{}]) => { const $scope = (0, scope_1.$$scope)(); const cursor = (0, internal_1.__useCursor)(); const scope = (0, use_memo_1.useMemo)(() => ({ token: $$effect, cleanup: undefined }), []); cursor.markHost(constants_1.EFFECT_HOST_MASK); (0, use_memo_1.useMemo)(() => { const run = () => { scope.cleanup = effect(); if ( type === EffectType.ASYNC && (0, utils_1.detectIsFunction)(scope.cleanup) && !(0, walk_1.detectIsFiberAlive)(cursor) ) { scope.cleanup(); } }; switch (type) { case EffectType.INSERTION: $scope.addInsertionEffect(run); break; case EffectType.LAYOUT: $scope.addLayoutEffect(run); break; case EffectType.ASYNC: $scope.addAsyncEffect(run); break; default: break; } (0, utils_1.detectIsFunction)(scope.cleanup) && scope.cleanup(); return null; }, deps); }; } exports.createEffect = createEffect; function dropEffects(hook) { for (const { value } of hook.values) { value?.token === $$effect && (0, utils_1.detectIsFunction)(value.cleanup) && value.cleanup(); } } exports.dropEffects = dropEffects; var EffectType; (function (EffectType) { EffectType['ASYNC'] = 'ASYNC'; EffectType['LAYOUT'] = 'LAYOUT'; EffectType['INSERTION'] = 'INSERTION'; })(EffectType || (exports.EffectType = EffectType = {})); const useEffect = createEffect(EffectType.ASYNC); exports.useEffect = useEffect; //# sourceMappingURL=use-effect.js.map