From 17ce73e6a94ef4b2914cfa5c1851082c0dcdd7f1 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Tue, 8 Oct 2019 11:12:02 +0200 Subject: [PATCH] [REF] hooks: improve makeLifecycleHook --- src/hooks.ts | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/hooks.ts b/src/hooks.ts index adc1cd50..dc9a3d47 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -35,25 +35,33 @@ export function useState(state: T): T { // Life cycle hooks // ----------------------------------------------------------------------------- function makeLifecycleHook(method: string, reverse: boolean = false) { - return function(cb) { - const component: Component = Component._current; - if (component.__owl__[method]) { - const current = component.__owl__[method]; - if (reverse) { + if (reverse) { + return function(cb) { + const component: Component = Component._current; + if (component.__owl__[method]) { + const current = component.__owl__[method]; component.__owl__[method] = function() { current.call(component); cb.call(component); }; } else { + component.__owl__[method] = cb; + } + }; + } else { + return function(cb) { + const component: Component = Component._current; + if (component.__owl__[method]) { + const current = component.__owl__[method]; component.__owl__[method] = function() { cb.call(component); current.call(component); }; + } else { + component.__owl__[method] = cb; } - } else { - component.__owl__[method] = cb; - } - }; + }; + } } export const onMounted = makeLifecycleHook("mountedCB", true);