Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

补上分享时的入参 #55

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 40 additions & 35 deletions component.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,34 +93,35 @@ export default class Component {
if (!this._inited) {
console.error(this.id + ' 组件未自动初始化之前请勿调用setState(),如果在组件构造函数中请直接使用"this.state={}"赋值语法');
}
this._setStateQueue.push(nextState);
if (callback) {
this._setStateCallbacks.push(callback);
}
// this._setStateQueue.push(nextState);
// if (callback) {
// this._setStateCallbacks.push(callback);
// }

if (this._setStateTimer) return;
// if (this._setStateTimer) return;

this._setStateTimer = setTimeout(() => {
this._setStateTimer = 0;
// this._setStateTimer = setTimeout(() => {
// this._setStateTimer = 0;
let state = this.state;
let stateChanged = false;
this._setStateQueue.forEach((item) => {
// let stateChanged = false;
// this._setStateQueue.forEach((item) => {
let item = nextState;
if (typeof item === 'function') {
item = item(state, this.props);
}
if (!utils.shouldUpdate(state, item)) {
// 如果没有发生变化,则忽略更新,优化性能
if (__DEV__) {
console.log('%c%s setState(%o) ignored',
'color:#fcc',
this.id,
utils.getDebugObject(item)
);
}
return;
}

stateChanged = true;
// if (!utils.shouldUpdate(state, item)) {
// // 如果没有发生变化,则忽略更新,优化性能
// if (__DEV__) {
// console.log('%c%s setState(%o) ignored',
// 'color:#fcc',
// this.id,
// utils.getDebugObject(item)
// );
// }
// return;
// }

// stateChanged = true;

if (__DEV__) {
// Development 环境打印state变化
Expand All @@ -136,17 +137,17 @@ export default class Component {
} else {
state = Object.assign({}, state, item);
}
});
// });

this.state = state;
this._setStateQueue = [];
this._setStateCallbacks.forEach((fn) => fn());
this._setStateCallbacks = [];
// this._setStateQueue = [];
// this._setStateCallbacks.forEach((fn) => fn());
// this._setStateCallbacks = [];

if (!stateChanged) return;
// if (!stateChanged) return;

this._update();
});
this._update(item);
// });
}

/**
Expand Down Expand Up @@ -235,22 +236,26 @@ export default class Component {
* 更新组件
* @private
*/
_update() {
if (this._updateTimer) return;
this._updateTimer = setTimeout(() => {
this._updateTimer = 0;
_update(item) {
if (!item) item = this.state;
// if (this._updateTimer) return;
// this._updateTimer = setTimeout(() => {
// this._updateTimer = 0;

// 内部state数据更新后,自动更新页面数据

let path = this.path ? this.path + '.' : '';
let newData = {};
newData[path + 'props'] = this.props;
newData[path + 'state'] = this.state;
// newData[path + 'state'] = this.state;
for (let key in item) {
newData[path + 'state.' + key] = item[key];
}
this.page.updateData(newData);

// 更新子组件列表
this._updateChildren();
});
// });
}

/**
Expand Down
39 changes: 22 additions & 17 deletions create-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,26 @@ module.exports = function createPage(ComponentClass: Class<Component>) {
// if (__DEV__) {
// console.log('%c%s updateData(%o)', 'color:#2a8f99', page.__route__, utils.getDebugObject(newData));
// }
let data = page.data;

Object.keys(newData).forEach((path) => {
let dataMap = newData[path];
if (Array.isArray(dataMap)) {
// 如果是组件列表,需要与之前列表数据合并,这样主要为了在子组件嵌套情况下,不丢失底层子组件数据
let list = _get(data, path); //原有data中列表数据
let newList = dataMap.map((item) => buildListItem(list, item));
_set(data, path, newList);
} else {
_set(data, path.split('.'), dataMap);
}
});

page.setData(data);
// let data = page.data;

// console.log('newData: ', newData)

// Object.keys(newData).forEach((path) => {
// let dataMap = newData[path];
// if (Array.isArray(dataMap)) {
// // 如果是组件列表,需要与之前列表数据合并,这样主要为了在子组件嵌套情况下,不丢失底层子组件数据
// let list = _get(data, path); //原有data中列表数据
// let newList = dataMap.map((item) => buildListItem(list, item));
// _set(data, path, newList);
// } else {
// _set(data, path.split('.'), dataMap);
// }
// });

// console.log('data: ', data)

// page.setData(data);
page.setData(newData);
};

let root = page.root = new ComponentClass({});
Expand Down Expand Up @@ -142,8 +147,8 @@ module.exports = function createPage(ComponentClass: Class<Component>) {
};

if (ComponentClass.prototype.onShareAppMessage) {
config.onShareAppMessage = function () {
let share = this.root.onShareAppMessage();
config.onShareAppMessage = function (res) {
let share = this.root.onShareAppMessage(res);
if (__DEV__ && !share) {
console.error(this.root.id + ' onShareAppMessage() 没有返回分享数据');
}
Expand Down
7 changes: 2 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const noPromiseMethods = {
drawCanvas: 1,
canvasToTempFilePath: 1,
hideKeyboard: 1,
getBackgroundAudioManager: 1
};

const labrador = {
Expand Down Expand Up @@ -82,12 +83,8 @@ Object.keys(wx).forEach((key) => {
obj = obj || {};
return new Promise((resolve, reject) => {
obj.success = resolve;
obj.fail = (res) => {
if (res && res.errMsg) {
reject(new Error(res.errMsg));
} else {
obj.fail = res => {
reject(res);
}
};
wx[key](obj);
});
Expand Down