From 2739adade44b09733c94caf858143d9549e1885d Mon Sep 17 00:00:00 2001 From: yelo Date: Thu, 20 Jun 2019 22:24:00 +0800 Subject: [PATCH] feat: support to mixing pageLifetimes --- src/core/layers/component.js | 18 +++++++++++++--- src/core/layers/unit.js | 9 ++------ src/utils/mix-strategies.js | 33 ++++++++++++++++++++---------- src/utils/wx-options-generator.js | 18 ++++++---------- test/cases/backward/component.js | 2 +- test/helpers/mina-sandbox/index.js | 1 - 6 files changed, 46 insertions(+), 35 deletions(-) diff --git a/src/core/layers/component.js b/src/core/layers/component.js index 8f782ec..9ee6e4d 100644 --- a/src/core/layers/component.js +++ b/src/core/layers/component.js @@ -9,9 +9,9 @@ import Unit from './unit' const MINA_COMPONENT_OPTIONS = ['properties', 'data', 'methods', 'behaviors', 'created', 'attached', 'ready', 'moved', 'detached', 'relations', 'externalClasses', 'options', 'observers', 'pageLifetimes'] const MINA_COMPONENT_HOOKS = ['created', 'attached', 'ready', 'moved', 'detached'] +const MINA_COMPONENT_PAGE_HOOKS = ['show', 'hide', 'resize'] const MINA_COMPONENT_METHODS = ['setData', 'hasBehavior', 'triggerEvent', 'createSelectorQuery', 'selectComponent', 'selectAllComponents', 'getRelationNodes', 'createIntersectionObserver'] const MINA_COMPONENT_ATTRIBUTES = ['is', 'id', 'dataset', 'data'] -const MINA_COMPONENT_PAGE_LIFETIME_HOOkS = ['show', 'hide', 'resize'] const ADDON_BEFORE_HOOKS = {} const ADDON_OPTIONS = ['mixins', 'compute'] @@ -20,7 +20,13 @@ const OVERWRITED_OPTIONS = ['properties', 'data', 'methods', 'observers', 'pageL const OVERWRITED_METHODS = ['setData'] const OVERWRITED_ATTRIBUTES = ['data'] -const COMPONENT_HOOKS = [...MINA_COMPONENT_HOOKS, ...values(ADDON_BEFORE_HOOKS)] +const COMPONENT_HOOKS = [ + ...MINA_COMPONENT_HOOKS, + ...values(ADDON_BEFORE_HOOKS), +] +const COMPONENT_PAGE_HOOKS = [ + ...MINA_COMPONENT_PAGE_HOOKS, +] const COMPONENT_INITIAL_OPTIONS = { mixins: [], @@ -31,6 +37,9 @@ const COMPONENT_INITIAL_OPTIONS = { compute () {}, // hooks: return { created: [], ...... } ...fromPairs(COMPONENT_HOOKS.map((name) => [name, []])), + pageLifetimes: { + ...fromPairs(COMPONENT_PAGE_HOOKS.map((name) => [name, []])), + }, methods: {}, relations: {}, options: {}, @@ -57,7 +66,7 @@ class Component extends Unit { properties: wxOptionsGenerator.properties(options.properties), observers: wxOptionsGenerator.observers(options.observers), methods: wxOptionsGenerator.methods(options.methods), - pageLifetimes: wxOptionsGenerator.pageLifetimes(options.pageLifetimes), + pageLifetimes: wxOptionsGenerator.lifecycles(MINA_COMPONENT_PAGE_HOOKS.filter((name) => options.pageLifetimes[name].length > 0), () => void 0, 'pageLifetimes'), ...wxOptionsGenerator.lifecycles(MINA_COMPONENT_HOOKS.filter((name) => options[name].length > 0), (name) => ADDON_BEFORE_HOOKS[name]), } @@ -93,6 +102,9 @@ class Component extends Unit { ...map(pick(options, COMPONENT_HOOKS), (name, handlers) => function (...args) { return handlers.reduce((memory, handler) => handler.apply(this, args.concat(memory)), void 0) }), + pageLifetimes: map(pick(options.pageLifetimes, COMPONENT_PAGE_HOOKS), (name, handlers) => function (...args) { + return handlers.reduce((memory, handler) => handler.apply(this, args.concat(memory)), void 0) + }), adapters: options.adapters, } // apply members into instance diff --git a/src/core/layers/unit.js b/src/core/layers/unit.js index a9d25e3..40411fe 100644 --- a/src/core/layers/unit.js +++ b/src/core/layers/unit.js @@ -1,7 +1,5 @@ import isPlainObject from 'is-plain-obj' -import map from 'just-map-object' -import { isEmpty, pick } from '../../utils/functions' -import globals from '../../utils/globals' +import { isEmpty } from '../../utils/functions' import strategies from '../../utils/mix-strategies' class Basic { @@ -23,10 +21,7 @@ class Basic { } let mixin = mixins - return { - ...options, - ...map(mixin, (key, extra) => strategies.merge(options[key], extra)), - } + return strategies(options, mixin) } static log (behavior, data) { diff --git a/src/utils/mix-strategies.js b/src/utils/mix-strategies.js index 21a0297..aba6c6a 100644 --- a/src/utils/mix-strategies.js +++ b/src/utils/mix-strategies.js @@ -1,14 +1,25 @@ -export default { - merge: function (source, extra) { - if (Array.isArray(source)) { - return source.concat(extra) - } - if (typeof source === 'object') { - return { - ...source, - ...extra, - } +import map from 'just-map-object' + +const strategies = (toOptions, fromOptions) => ({ + ...toOptions, + ...map(fromOptions, (key, extra) => { + const strat = strategies[key] || strategies.default + return strat(toOptions[key], extra) + }) +}) + +strategies.default = (toValue, fromValue) => { + if (Array.isArray(toValue)) { + return toValue.concat(fromValue) + } + if (typeof toValue === 'object') { + return { + ...toValue, + ...fromValue, } - return extra } + return fromValue } +strategies.pageLifetimes = (toValue, fromValue) => strategies(toValue, fromValue) + +export default strategies diff --git a/src/utils/wx-options-generator.js b/src/utils/wx-options-generator.js index 1adcdce..b4a73a3 100644 --- a/src/utils/wx-options-generator.js +++ b/src/utils/wx-options-generator.js @@ -10,18 +10,19 @@ export function methods (object) { } // generate lifecycles for wx-Component -export function lifecycles (hooks, getBeforeHookName) { +export function lifecycles (hooks, getBeforeHookName, namespace) { return fromPairs(hooks.map((origin) => { let before = getBeforeHookName(origin) return [ origin, function handler () { let context = this.__tina_instance__ - if (before && context[before]) { - context[before].apply(context, arguments) + let ns = namespace ? context[namespace] : context + if (before && ns[before]) { + ns[before].apply(context, arguments) } - if (context[origin]) { - return context[origin].apply(context, arguments) + if (ns[origin]) { + return ns[origin].apply(context, arguments) } }, ] @@ -70,10 +71,3 @@ export function observers (object) { method.apply(context, args) }) } - -export function pageLifetimes (object) { - return map(object || {}, (name, func) => function pageLifetime (...args) { - let context = this.__tina_instance__ - func.apply(context, args) - }) -} diff --git a/test/cases/backward/component.js b/test/cases/backward/component.js index 6c4c83c..a613969 100644 --- a/test/cases/backward/component.js +++ b/test/cases/backward/component.js @@ -553,7 +553,7 @@ test('`pageLifetimes` can be triggered', async (t) => { pageLifetimes: { show () { spy(this.data.foo) - } + }, }, data: { foo: 'bar', diff --git a/test/helpers/mina-sandbox/index.js b/test/helpers/mina-sandbox/index.js index c3c649b..4145632 100644 --- a/test/helpers/mina-sandbox/index.js +++ b/test/helpers/mina-sandbox/index.js @@ -62,7 +62,6 @@ class Component extends Unit { _emitObserver (name, ...values) { this.observers[name].call(this, ...values) } - _emitPageLifetimes (name, ...values) { this.pageLifetimes[name].call(this, ...values) }