Skip to content

Commit

Permalink
feat: support to mixing pageLifetimes
Browse files Browse the repository at this point in the history
  • Loading branch information
imyelo committed Jun 20, 2019
1 parent 4290187 commit 2739ada
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 35 deletions.
18 changes: 15 additions & 3 deletions src/core/layers/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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: [],
Expand All @@ -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: {},
Expand All @@ -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]),
}

Expand Down Expand Up @@ -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
Expand Down
9 changes: 2 additions & 7 deletions src/core/layers/unit.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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) {
Expand Down
33 changes: 22 additions & 11 deletions src/utils/mix-strategies.js
Original file line number Diff line number Diff line change
@@ -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
18 changes: 6 additions & 12 deletions src/utils/wx-options-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
},
]
Expand Down Expand Up @@ -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)
})
}
2 changes: 1 addition & 1 deletion test/cases/backward/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ test('`pageLifetimes` can be triggered', async (t) => {
pageLifetimes: {
show () {
spy(this.data.foo)
}
},
},
data: {
foo: 'bar',
Expand Down
1 change: 0 additions & 1 deletion test/helpers/mina-sandbox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 2739ada

Please sign in to comment.