Skip to content

Commit

Permalink
fix: fix update bug
Browse files Browse the repository at this point in the history
  • Loading branch information
imtaotao committed Dec 16, 2019
1 parent 8e72dbd commit 833d94b
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 107 deletions.
8 changes: 4 additions & 4 deletions demo/store/mpstore.esm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Mpstore.js v0.1.8
* Mpstore.js v0.1.9
* (c) 2019-2019 Imtaotao
* Released under the MIT License.
*/
Expand Down Expand Up @@ -514,7 +514,7 @@ function updateComponents(store, callback) {
}
};

for (var i = 0; i < len; i++) {
for (var i = 0; i < depComponents.length; i++) {
var _depComponents$i = depComponents[i],
isPage = _depComponents$i.isPage,
component = _depComponents$i.component,
Expand Down Expand Up @@ -816,7 +816,7 @@ function () {
this.depComponents = [];
this.GLOBALWORD = 'global';
this.isDispatching = false;
this.version = '0.1.8';
this.version = '0.1.9';
this.state = Object.freeze(createModule({}));
this.middleware = new Middleware(this);
}
Expand Down Expand Up @@ -1080,7 +1080,7 @@ function () {
return Store;
}();

var version = '0.1.8';
var version = '0.1.9';
var nativePage = Page;
var nativeComponent = Component;

Expand Down
8 changes: 4 additions & 4 deletions dist/mpstore.common.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Mpstore.js v0.1.8
* Mpstore.js v0.1.9
* (c) 2019-2019 Imtaotao
* Released under the MIT License.
*/
Expand Down Expand Up @@ -518,7 +518,7 @@ function updateComponents(store, callback) {
}
};

for (var i = 0; i < len; i++) {
for (var i = 0; i < depComponents.length; i++) {
var _depComponents$i = depComponents[i],
isPage = _depComponents$i.isPage,
component = _depComponents$i.component,
Expand Down Expand Up @@ -820,7 +820,7 @@ function () {
this.depComponents = [];
this.GLOBALWORD = 'global';
this.isDispatching = false;
this.version = '0.1.8';
this.version = '0.1.9';
this.state = Object.freeze(createModule({}));
this.middleware = new Middleware(this);
}
Expand Down Expand Up @@ -1084,7 +1084,7 @@ function () {
return Store;
}();

var version = '0.1.8';
var version = '0.1.9';
var nativePage = Page;
var nativeComponent = Component;

Expand Down
8 changes: 4 additions & 4 deletions dist/mpstore.es6m.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Mpstore.js v0.1.8
* Mpstore.js v0.1.9
* (c) 2019-2019 Imtaotao
* Released under the MIT License.
*/
Expand Down Expand Up @@ -383,7 +383,7 @@ function updateComponents (store, callback) {
callback();
}
};
for (let i = 0; i < len; i++) {
for (let i = 0; i < depComponents.length; i++) {
const {
isPage,
component,
Expand Down Expand Up @@ -645,7 +645,7 @@ class Store {
this.depComponents = [];
this.GLOBALWORD = 'global';
this.isDispatching = false;
this.version = '0.1.8';
this.version = '0.1.9';
this.state = Object.freeze(createModule({}));
this.middleware = new Middleware(this);
}
Expand Down Expand Up @@ -886,7 +886,7 @@ class Store {
}
}

const version = '0.1.8';
const version = '0.1.9';
const nativePage = Page;
const nativeComponent = Component;
function expandConfig (config, expandMethods, isPage) {
Expand Down
8 changes: 4 additions & 4 deletions dist/mpstore.esm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Mpstore.js v0.1.8
* Mpstore.js v0.1.9
* (c) 2019-2019 Imtaotao
* Released under the MIT License.
*/
Expand Down Expand Up @@ -514,7 +514,7 @@ function updateComponents(store, callback) {
}
};

for (var i = 0; i < len; i++) {
for (var i = 0; i < depComponents.length; i++) {
var _depComponents$i = depComponents[i],
isPage = _depComponents$i.isPage,
component = _depComponents$i.component,
Expand Down Expand Up @@ -816,7 +816,7 @@ function () {
this.depComponents = [];
this.GLOBALWORD = 'global';
this.isDispatching = false;
this.version = '0.1.8';
this.version = '0.1.9';
this.state = Object.freeze(createModule({}));
this.middleware = new Middleware(this);
}
Expand Down Expand Up @@ -1080,7 +1080,7 @@ function () {
return Store;
}();

var version = '0.1.8';
var version = '0.1.9';
var nativePage = Page;
var nativeComponent = Component;

Expand Down
4 changes: 2 additions & 2 deletions dist/mpstore.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rustle/mp-store",
"version": "0.1.8",
"version": "0.1.9",
"description": "A lightweight wechat miniprogram state management library",
"main": "dist/mpstore.common.js",
"module": "dist/mpstore.esm.js",
Expand Down
177 changes: 89 additions & 88 deletions src/update.js
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()
}
}
}

0 comments on commit 833d94b

Please sign in to comment.