Skip to content

Commit

Permalink
feat: update store and data mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
cicec committed Apr 30, 2020
1 parent ac06c65 commit d1efba9
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions src/observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type PageOptions = WechatMiniprogram.Page.Options<AnyObject, AnyObject>

const isFunc = (a: unknown): a is Function => typeof a === 'function'

const toEntireJS = (source: AnyObject) => {
const toData = (source: AnyObject) => {
const target: AnyObject = {}

Object.getOwnPropertyNames(source).forEach((key) => {
Expand All @@ -21,21 +21,19 @@ const observer = {
return (options: PageOptions) => {
let dispose: IReactionDisposer

const data = options.data || {}
const onLoad = options.onLoad
const onUnload = options.onUnload
const { data = {}, onLoad, onUnload } = options

return Page({
...options,
data: { ...data, store: toEntireJS(store) },
data: { ...data, ...toData(store) },

onLoad(query) {
dispose = autorun(() => {
if (this.data) {
const diffs: AnyObject = diff(toEntireJS(store), this.data.store)
const diffs: AnyObject = diff({ ...this.data, ...toData(store) }, this.data)

for (const key in diffs) {
this.setData({ ['store.' + key]: diffs[key] })
if (key) this.setData({ [key]: diffs[key] })
}
}
})
Expand All @@ -56,21 +54,19 @@ const observer = {
return (options: ComponentOptions) => {
let dispose: IReactionDisposer

const data = options.data || {}
const attached = options.attached
const detached = options.detached
const { data = {}, attached, detached } = options

return Component({
...options,
data: { ...data, store: toEntireJS(store) },
data: { ...data, ...toData(store) },

attached() {
dispose = autorun(() => {
if (this.data) {
const diffs: AnyObject = diff(toEntireJS(store), this.data.store)
const diffs: AnyObject = diff({ ...this.data, ...toData(store) }, this.data)

for (const key in diffs) {
this.setData({ ['store.' + key]: diffs[key] })
if (key) this.setData({ [key]: diffs[key] })
}
}
})
Expand Down

0 comments on commit d1efba9

Please sign in to comment.