-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
108 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,90 @@ | ||
import { diff } from './diff' | ||
import { callHook } from './utils' | ||
|
||
export function applyPatchs (component, patchs, callback) { | ||
const destObject = {} | ||
|
||
for (let i = 0, len = patchs.length; i < len; i++) { | ||
const { value, path } = patchs[i] | ||
destObject[path] = value | ||
} | ||
|
||
component.setData(destObject, callback) | ||
} | ||
|
||
// update page and component | ||
export function updateComponents (store, callback) { | ||
let total = 0 | ||
const { | ||
hooks, | ||
GLOBALWORD, | ||
depComponents, | ||
} = store | ||
const len = depComponents.length | ||
|
||
if (len === 0) { | ||
callback() | ||
return | ||
} | ||
|
||
// call `callback`, when all component views are rendered | ||
const renderedCallback = () => { | ||
if (++total === len) { | ||
callback() | ||
} | ||
} | ||
|
||
for (let i = 0; i < len; i++) { | ||
const { | ||
isPage, | ||
component, | ||
didUpdate, | ||
willUpdate, | ||
createState, | ||
} = depComponents[i] | ||
|
||
if (component.data[GLOBALWORD]) { | ||
const newPartialState = createState() | ||
|
||
// the `willUpdate` function will optimize component | ||
if (typeof willUpdate === 'function') { | ||
if (willUpdate.call(store, component, newPartialState) === false) { | ||
renderedCallback() | ||
continue | ||
} | ||
} | ||
|
||
// the base path is `GLOBALWORD` | ||
// example: this.setData({ 'global.xx': xx }) | ||
const patchs = diff(component.data[GLOBALWORD], newPartialState, GLOBALWORD) | ||
|
||
if (patchs.length > 0) { | ||
// call global hooks | ||
const params = [component, newPartialState, patchs, isPage] | ||
if (callHook(hooks, 'willUpdate', params) === false) { | ||
renderedCallback() | ||
continue | ||
} | ||
|
||
// update component | ||
applyPatchs(component, patchs, renderedCallback) | ||
|
||
if (typeof didUpdate === 'function') { | ||
didUpdate.call(store, component, newPartialState, patchs) | ||
} | ||
|
||
callHook(hooks, 'didUpdate', [component, newPartialState, isPage]) | ||
|
||
// record patchs, allow playback view | ||
if (component.timeTravel) { | ||
component.timeTravel.push(patchs) | ||
} | ||
} else { | ||
renderedCallback() | ||
} | ||
} else { | ||
renderedCallback() | ||
} | ||
} | ||
import { diff } from './diff' | ||
import { callHook } from './utils' | ||
|
||
export function applyPatchs (component, patchs, callback) { | ||
const destObject = {} | ||
|
||
for (let i = 0, len = patchs.length; i < len; i++) { | ||
const { value, path } = patchs[i] | ||
destObject[path] = value | ||
} | ||
|
||
component.setData(destObject, callback) | ||
} | ||
|
||
// update page and component | ||
export function updateComponents (store, callback) { | ||
let total = 0 | ||
const { | ||
hooks, | ||
GLOBALWORD, | ||
depComponents, | ||
} = store | ||
const len = depComponents.length | ||
|
||
if (len === 0) { | ||
callback() | ||
return | ||
} | ||
|
||
// call `callback`, when all component views are rendered | ||
const renderedCallback = () => { | ||
if (++total === len) { | ||
callback() | ||
} | ||
} | ||
|
||
// the component maybe aleard unload, so, can't use `len` | ||
for (let i = 0; i < depComponents.length; i++) { | ||
const { | ||
isPage, | ||
component, | ||
didUpdate, | ||
willUpdate, | ||
createState, | ||
} = depComponents[i] | ||
|
||
if (component.data[GLOBALWORD]) { | ||
const newPartialState = createState() | ||
|
||
// the `willUpdate` function will optimize component | ||
if (typeof willUpdate === 'function') { | ||
if (willUpdate.call(store, component, newPartialState) === false) { | ||
renderedCallback() | ||
continue | ||
} | ||
} | ||
|
||
// the base path is `GLOBALWORD` | ||
// example: this.setData({ 'global.xx': xx }) | ||
const patchs = diff(component.data[GLOBALWORD], newPartialState, GLOBALWORD) | ||
|
||
if (patchs.length > 0) { | ||
// call global hooks | ||
const params = [component, newPartialState, patchs, isPage] | ||
if (callHook(hooks, 'willUpdate', params) === false) { | ||
renderedCallback() | ||
continue | ||
} | ||
|
||
// update component | ||
applyPatchs(component, patchs, renderedCallback) | ||
|
||
if (typeof didUpdate === 'function') { | ||
didUpdate.call(store, component, newPartialState, patchs) | ||
} | ||
|
||
callHook(hooks, 'didUpdate', [component, newPartialState, isPage]) | ||
|
||
// record patchs, allow playback view | ||
if (component.timeTravel) { | ||
component.timeTravel.push(patchs) | ||
} | ||
} else { | ||
renderedCallback() | ||
} | ||
} else { | ||
renderedCallback() | ||
} | ||
} | ||
} |