Skip to content
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
45 changes: 31 additions & 14 deletions dist/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ var defaultConfig = {
methods: {}
};

function connect(options) {
var _options$mapStateToPr = options.mapStateToProps,
function getTargetInstance() {}
function connect(options, directTargetInstanceObj) {
var name = options.name,
_options$mapStateToPr = options.mapStateToProps,
mapStateToProps = _options$mapStateToPr === undefined ? [] : _options$mapStateToPr,
_options$mapGettersTo = options.mapGettersToProps,
mapGettersToProps = _options$mapGettersTo === undefined ? [] : _options$mapGettersTo,
Expand Down Expand Up @@ -72,24 +74,37 @@ function connect(options) {
(0, _assign2.default)(config.data, data);
(0, _assign2.default)(config.props, props);
return _extends({}, config, {
methods: _extends({}, config.methods, _createHelpers.createConnectHelpers.call(_global2.default, _global2.default, key, config)),
methods: _extends({}, config.methods, _createHelpers.createConnectHelpers.call(_global2.default, _global2.default, key, config, directTargetInstanceObj)),
didMount: function didMount() {
var _this = this;

var that = this;
// 组件可以添加 $ref 来拿相应的实例
var propsRef = this.props.$ref;
var key = namespace || instanceName || _global2.default.getCurrentPath() || _global2.default.getCurrentViewId() || -1;
var targetInstanceObj = _global2.default.getInstance(key);
var propsNamespace = this.props.$namespace;
var targetInstanceObj = void 0;
var key = propsNamespace || namespace || instanceName || _global2.default.getCurrentPath() || _global2.default.getCurrentViewId() || -1;
if (this.$page) {
var _store = this.$page.$store;
targetInstanceObj = {
config: _store.actions,
currentPath: getPath(this.$page.route),
name: getPath(this.$page.route),
store: _store
};
} else if (directTargetInstanceObj) {
targetInstanceObj = _extends({}, directTargetInstanceObj);
} else {
targetInstanceObj = _global2.default.getInstance(key);
}
if (!targetInstanceObj && typeof _didMount === 'function') {
console.warn('未绑定 store');
_didMount.call(this);
return;
}
// 当前component表达
var componentIs = getPath(this.is, 2);
var currentRoute = targetInstanceObj.store.getInstance().route;
console.info(componentIs + ' \u7EC4\u4EF6\u5DF2\u5173\u8054 ' + currentRoute + '_' + key + ' \u7684 store', targetInstanceObj);
var componentIs = name || !directTargetInstanceObj && getPath(this.is, 2) || 'unknown';
var currentStore = targetInstanceObj.store.getInstance();
var currentRoute = currentStore.namespace || currentStore.instanceName || currentStore.route || '';
// console.info(`${componentIs} 组件已关联 ${currentRoute}_${key} 的 store`, targetInstanceObj);
(0, _assign2.default)(this, {
storeConfig: targetInstanceObj.config,
storeInstance: targetInstanceObj.store
Expand All @@ -100,17 +115,19 @@ function connect(options) {
var initialData = _dataTransform.setDataByStateProps.call(that, mapStateToProps, store.getInstance().data, config, mapGettersToProps, store.getInstance());
this.setData(initialData);
// 自动注册进 components 实例, propsRef 开发者自己保证唯一性
_global2.default.registerComponents(propsRef || getPath(currentRoute) + ':' + componentIs, this);
if (!directTargetInstanceObj) {
_global2.default.registerComponents(propsRef || getPath(currentRoute) + ':' + componentIs, this);
}
if (mapStateToProps) {
// store 触发的更新
this.herculexUpdateLisitener = store.$emitter.addListener('updateState', function (_ref) {
var _ref$state = _ref.state,
state = _ref$state === undefined ? {} : _ref$state;

var nextData = _dataTransform.setDataByStateProps.call(that, mapStateToProps, state, config, mapGettersToProps, store.getInstance(), true);
var originBindViewId = _this.$page.$viewId || -1;
var currentViewId = getCurrentPages().pop() ? getCurrentPages().pop().$viewId || -1 : -1;
if (originBindViewId !== currentViewId) return;
// const originBindViewId = this.$page.$viewId || -1;
// const currentViewId = getCurrentPages().slice().pop() ? getCurrentPages().slice().pop().$viewId || -1 : -1;
// if (originBindViewId !== currentViewId) return;
that.setData(nextData);
});
}
Expand Down
32 changes: 28 additions & 4 deletions dist/createHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,21 +187,34 @@ function getConfigFromInstance(target) {
instance: target.getInstance()
};
}

function createConnectHelpers(global, key) {
var config = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var isInstance = arguments[3];
var targetInstanceObj = arguments[3];

return {
commitGlobal: commitGlobal.bind(this),
dispatchGlobal: dispatchGlobal.bind(this),
commit: function commit(type, payload, innerHelper) {
var _this = this;

if (this.$page) {
targetInstanceObj = {
mutations: this.$page.$store.mutations,
actions: this.$page.$store.actions,
getInstance: function getInstance() {
return _this.$page;
}
};
}
var finalKey = key || global.getCurrentPath() || global.getCurrentViewId() || -1;

var _ref = global.storeInstance ? getConfigFromInstance(global) : getConfigFromGlobal(global, finalKey),
var _ref = targetInstanceObj ? getConfigFromInstance(_extends({}, targetInstanceObj.config, targetInstanceObj)) : global.storeInstance ? getConfigFromInstance(global) : getConfigFromGlobal(global, finalKey),
instance = _ref.instance,
_ref$mutations = _ref.mutations,
mutations = _ref$mutations === undefined ? {} : _ref$mutations;

console.log('mutations', targetInstanceObj, mutations);
(0, _assign2.default)(mutations, config.mutations);
if (!type) {
throw new Error(type + ' not found');
Expand All @@ -222,10 +235,21 @@ function createConnectHelpers(global, key) {
},
dispatch: function dispatch(type, payload) {
return new (_ExternalPromise())(function ($return, $error) {
var finalKey, _ref2, instance, _ref2$mutations, mutations, _ref2$actions, actions, realType, actionFunc, self, res;
var _this2, finalKey, _ref2, instance, _ref2$mutations, mutations, _ref2$actions, actions, realType, actionFunc, self, res;

_this2 = this;

finalKey = key || global.getCurrentPath() || global.getCurrentViewId() || -1;
_ref2 = global.storeInstance ? getConfigFromInstance(global) : getConfigFromGlobal(global, finalKey), instance = _ref2.instance, _ref2$mutations = _ref2.mutations, mutations = _ref2$mutations === undefined ? {} : _ref2$mutations, _ref2$actions = _ref2.actions, actions = _ref2$actions === undefined ? {} : _ref2$actions;
if (this.$page) {
targetInstanceObj = {
mutations: this.$page.$store.mutations,
actions: this.$page.$store.actions,
getInstance: function getInstance() {
return _this2.$page;
}
};
}
_ref2 = targetInstanceObj ? getConfigFromInstance(_extends({}, targetInstanceObj.config, targetInstanceObj)) : global.storeInstance ? getConfigFromInstance(global) : getConfigFromGlobal(global, finalKey), instance = _ref2.instance, _ref2$mutations = _ref2.mutations, mutations = _ref2$mutations === undefined ? {} : _ref2$mutations, _ref2$actions = _ref2.actions, actions = _ref2$actions === undefined ? {} : _ref2$actions;

if (!type) {
return $error(new Error('action type not found'));
Expand Down
115 changes: 86 additions & 29 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ var _global = require('./global');

var _global2 = _interopRequireDefault(_global);

var _connect = require('./connect');
var _connect2 = require('./connect');

var _connect2 = _interopRequireDefault(_connect);
var _connect3 = _interopRequireDefault(_connect2);

var _provider = require('./provider');

Expand Down Expand Up @@ -101,6 +101,22 @@ var Store = function () {
this.when = this.when.bind(this);
}

Store.prototype.getCurrentPages = function (_getCurrentPages) {
function getCurrentPages() {
return _getCurrentPages.apply(this, arguments);
}

getCurrentPages.toString = function () {
return _getCurrentPages.toString();
};

return getCurrentPages;
}(function () {
// 对currentpage的实现, 弱依赖
// return [];
return typeof getCurrentPages === 'function' ? getCurrentPages() : [];
});

Store.prototype.getInstance = function getInstance() {
return this.storeInstance;
};
Expand All @@ -126,7 +142,7 @@ var Store = function () {
prevState = _ref.prevState;

var newData = (0, _dataTransform.setStoreDataByState)(_this.storeInstance.data, state);
var currentPageInstance = getCurrentPages().pop() || {};
var currentPageInstance = _this.getCurrentPages().slice().pop() || {};
var instanceView = _this.storeInstance.$viewId || -1;
var currentView = currentPageInstance.$viewId || -1;
// 已经不在当前页面的不再触发
Expand All @@ -149,14 +165,14 @@ var Store = function () {
var _this2 = this;

var emitter = this.$emitter;
var originViewInstance = getCurrentPages().pop() || {};
var originViewInstance = this.getCurrentPages().slice().pop() || {};
if (subscriber) {
this.storeUpdateLisitenerDispose = emitter.addListener('updateState', function (_ref2) {
var state = _ref2.state,
mutation = _ref2.mutation,
prevState = _ref2.prevState;

var currentPageInstance = getCurrentPages().pop() || {};
var currentPageInstance = _this2.getCurrentPages().slice().pop() || {};
var instanceView = originViewInstance.$viewId || -1;
var currentView = currentPageInstance.$viewId || -1;
// 已经不在当前页面的不再触发
Expand All @@ -173,11 +189,13 @@ var Store = function () {
};

Store.prototype.subscribeAction = function subscribeAction(actionSubscriber) {
var _this3 = this;

var emitter = this.$emitter;
var originViewInstance = getCurrentPages().pop() || {};
var originViewInstance = this.getCurrentPages().slice().pop() || {};
if (actionSubscriber) {
emitter.addListener('dispatchAction', function (action, next) {
var currentPageInstance = getCurrentPages().pop() || {};
var currentPageInstance = _this3.getCurrentPages().slice().pop() || {};
var instanceView = originViewInstance.$viewId || -1;
var currentView = currentPageInstance.$viewId || -1;
if (instanceView === currentView) {
Expand Down Expand Up @@ -208,6 +226,7 @@ var Store = function () {
var originOnUnload = config.onUnload;
var originOnShow = config.onShow;
var originOnHide = config.onHide;
var originOnReady = config.onReady;
var emitter = this.$emitter;
// mappers
if (config.mapActionsToMethod) {
Expand All @@ -220,7 +239,7 @@ var Store = function () {
(0, _mapHelpersToMethod.mapMutationsToMethod)(config.mapMutationsToMethod, config);
}
config.onHide = function () {
var currentPageInstance = getCurrentPages().pop() || {};
var currentPageInstance = that.getCurrentPages().slice().pop() || {};
_global2.default.emitter.emitEvent('updateCurrentPath', {
from: getPath(currentPageInstance.route),
fromViewId: currentPageInstance.$viewId || -1
Expand All @@ -229,7 +248,7 @@ var Store = function () {
this._isHided = true;
};
config.onUnload = function () {
var currentPageInstance = getCurrentPages().pop() || {};
var currentPageInstance = that.getCurrentPages().slice().pop() || {};
_global2.default.emitter.emitEvent('updateCurrentPath', {
from: getPath(currentPageInstance.route)
});
Expand All @@ -242,7 +261,7 @@ var Store = function () {
originOnUnload && originOnUnload.apply(this, arguments);
};
config.onShow = function (d) {
var currentPageInstance = getCurrentPages().pop() || {};
var currentPageInstance = that.getCurrentPages().slice().pop() || {};
// 消费 Resume 字段
var resumeData = _global2.default.messageManager.pop('$RESUME') || {};
_global2.default.emitter.emitEvent('updateCurrentPath', (0, _assign2.default)(currentPageInstance.$routeConfig || {}, {
Expand All @@ -269,25 +288,26 @@ var Store = function () {
}
};
config.onLoad = function (query) {
var _this3 = this;
var _this4 = this;

var onloadInstance = this;
this.$emitter = emitter;
this.$globalEmitter = _global2.default.emitter;
this.$message = _global2.default.messageManager;
this.$store = that;
this.$when = that.when;
console.log('eee', that.getCurrentPages());
// 先榜上更新 store 的 监听器
this.herculexUpdateLisitener = emitter.addListener('updateState', function (_ref3) {
var state = _ref3.state;

var newData = (0, _dataTransform.setStoreDataByState)(_this3.data, state);
var currentPageInstance = getCurrentPages().pop() || {};
var newData = (0, _dataTransform.setStoreDataByState)(_this4.data, state);
var currentPageInstance = that.getCurrentPages().slice().pop() || {};
var instanceView = onloadInstance.$viewId || -1;
var currentView = currentPageInstance.$viewId || -1;
// 已经不在当前页面的不再触发
if (instanceView === currentView) {
_this3.setData(newData);
if (instanceView === currentView || currentView === -1) {
_this4.setData(newData);
}
});
if (that.connectGlobal) {
Expand All @@ -304,26 +324,26 @@ var Store = function () {

// 增加nextprops的关联
this.herculexUpdateLisitenerGlobal = _global2.default.emitter.addListener('updateGlobalStore', function () {
var currentPageInstance = getCurrentPages().pop() || {};
var currentPageInstance = that.getCurrentPages().slice().pop() || {};
var instanceView = onloadInstance.$viewId || -1;
var currentView = currentPageInstance.$viewId || -1;
// 已经不在当前页面的不再触发
if (instanceView !== currentView) return;
if (instanceView !== currentView && currentView !== -1) return;
emitter.emitEvent('updateState', {
state: _extends({}, _this3.data, {
$global: _extends({}, _this3.data.$global, _global2.default.getGlobalState(_this3.mapGlobals))
state: _extends({}, _this4.data, {
$global: _extends({}, _this4.data.$global, _global2.default.getGlobalState(_this4.mapGlobals))
}),
mutation: {
type: 'sync_global_data'
},
prevState: _this3.data
prevState: _this4.data
});
});
}
this.subscribe = that.subscribe;
this.subscribeAction = that.subscribeAction;
// 设置页面 path 和 query
var currentPageInstance = getCurrentPages().pop() || {};
var currentPageInstance = that.getCurrentPages().slice().pop() || {};
var currentPath = getPath(currentPageInstance.route);
// 外面携带的数据
var contextData = _global2.default.messageManager.pop('$RESUME') || {};
Expand All @@ -340,13 +360,16 @@ var Store = function () {
var name = that.instanceName || currentPath || viewId || -1;
// 把命名空间灌到实例
this.instanceName = name;
_global2.default.registerInstance(name, {
config: { actions: that.actions, mutations: that.mutations, state: initialState },
store: that,
name: name,
currentPath: currentPath,
viewId: viewId
});
// TODO 检查是否影响支付宝小程序
// if (name !== -1) {
// global.registerInstance(name, {
// config: { actions: that.actions, mutations: that.mutations, state: initialState },
// store: that,
// name,
// currentPath,
// viewId
// });
// }
if (that.plugins) {
that.plugins.forEach(function (element) {
var pluginFunc = (0, _is.isString)(element) ? _innerPlugins3.default[element] : element;
Expand Down Expand Up @@ -374,8 +397,42 @@ var Store = function () {
originOnLoad.call(this, query, contextData);
}
};
config.onReady = function () {
var currentPageInstance = that.getCurrentPages().slice().pop() || {};
var currentPath = getPath(currentPageInstance.route);
var viewId = currentPageInstance.$viewId || -1;
var name = that.instanceName || currentPath || viewId || -1;
if (!_global2.default.getInstance(name) && name !== -1) {
_global2.default.registerInstance(name, {
config: { actions: that.actions, mutations: that.mutations, state: initialState },
store: that,
name: name,
currentPath: currentPath,
viewId: viewId
});
}
originOnReady && originOnReady.apply(this, arguments);
};
return _extends({}, config, _createHelpers2.default.call(this, that.actions, that.mutations, that.$emitter));
};

Store.prototype.connect = function connect(options) {
console.log('optopns', options);
var targetInstanceObj = {
// name: ''
store: this,
config: {
actions: this.actions,
mutations: this.mutations,
state: this.stateConfig,
getters: this.getters
},
getInstance: function getInstance() {
return this.store;
}
};
return (0, _connect3.default)(options, targetInstanceObj);
};
// connect(options) {
// const { mapStateToProps = [], mapGettersToProps } = options;
// const that = this;
Expand Down Expand Up @@ -410,5 +467,5 @@ var Store = function () {
}();

exports.default = Store;
exports.connect = _connect2.default;
exports.connect = _connect3.default;
exports.GlobalStore = _provider2.default;
Loading