UNPKG

redom

Version:

Tiny turboboosted JavaScript library for creating user interfaces.

65 lines (50 loc) 1.26 kB
import { getEl } from "./util.js"; import { trigger } from "./mount.js"; export function unmount(parent, _child) { let child = _child; const parentEl = getEl(parent); const childEl = getEl(child); if (child === childEl && childEl.__redom_view) { // try to look up the view if not provided child = childEl.__redom_view; } if (childEl.parentNode) { doUnmount(child, childEl, parentEl); parentEl.removeChild(childEl); } return child; } export function doUnmount(child, childEl, parentEl) { const hooks = childEl.__redom_lifecycle; if (hooksAreEmpty(hooks)) { childEl.__redom_lifecycle = {}; return; } let traverse = parentEl; if (childEl.__redom_mounted) { trigger(childEl, "onunmount"); } while (traverse) { const parentHooks = traverse.__redom_lifecycle || {}; for (const hook in hooks) { if (parentHooks[hook]) { parentHooks[hook] -= hooks[hook]; } } if (hooksAreEmpty(parentHooks)) { traverse.__redom_lifecycle = null; } traverse = traverse.parentNode; } } function hooksAreEmpty(hooks) { if (hooks == null) { return true; } for (const key in hooks) { if (hooks[key]) { return false; } } return true; }