Skip to content

Commit

Permalink
更改reducer参数为对象传递
Browse files Browse the repository at this point in the history
  • Loading branch information
www3com committed Oct 16, 2017
1 parent 6f24147 commit f9dadac
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 90 deletions.
106 changes: 57 additions & 49 deletions lib/stateMgr/modelMgr.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
'use strict';
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
value: true
});
exports.dispatch = undefined;
exports.setModel = setModel;
exports.debug = debug;
exports.getReducer = getReducer;
exports.getState = getState;
exports.setState = setState;
exports.dispatch = dispatch;

var _map = require('../map');
var _map = require("../map");

var _map2 = _interopRequireDefault(_map);

Expand All @@ -23,62 +23,70 @@ var put = _map2.default.put; /**
var get = _map2.default.get;

function setModel(model) {
if (model == null) {
throw new Error("状态管理器缺少model");
}

var initSate = {};
if (model.state != null) {
initSate = model.state;
}
put('storageName', model.namespace);
put(model.namespace, { state: initSate, reducers: model.reducers });
if (model == null) {
throw new Error("状态管理器缺少model");
}

var initSate = {};
if (model.state != null) {
initSate = model.state;
}
if (model.namespace == null) {
model.namespace = "defaultNamespace";
}
put('storageName', model.namespace);
put(model.namespace, { state: initSate, reducers: model.reducers });
}

function debug(flag) {
put('_debug', true);
put('_debug', true);
}

function getReducer(type) {
var reducers = get(get('storageName')).reducers;
if (reducers == null) {
throw new Error("状态管理器缺少model");
}
var reducer = reducers[type];
if (reducer == null) {
throw new Error("模型未配置reducer:" + type);
}
return reducer;
var reducers = get(get('storageName')).reducers;
if (reducers == null) {
throw new Error("状态管理器缺少model");
}
var reducer = reducers[type];
if (reducer == null) {
throw new Error("模型未配置reducer:" + type);
}
return reducer;
}

function getState() {
var state = get(get('storageName')).state;
return state;
var state = get(get('storageName')).state;
return state;
}

function setState(state) {
var model = get(get('storageName'));
model.state = state;
put(get('storageName'), model);
var model = get(get('storageName'));
model.state = state;
put(get('storageName'), model);
}

function dispatch(that, action) {
var state = getState();
var reducer = getReducer(action.type);
var put = function put(res) {
Object.assign(state, res);
setState(state);
if (get('_debug')) {
console.log("状态发生变化,命名空间:" + get('storageName') + ",内容:", state);
}
that.setState({});
};

var result = reducer(state, action.params, put, function (action) {
dispatch(that, action);
});

if (result != null) {
put(result);
}
}
function _dispatch(that, action) {
var state = getState();
var reducer = getReducer(action.type);
var put = function put(res) {
Object.assign(state, res);
setState(state);
if (get('_debug')) {
console.log("状态发生变化,命名空间:" + get('storageName') + ",内容:", state);
}
that.setState({});
};

// let result = reducer(state, action.params, put, function(action) {
// dispatch(that, action);
// });

var result = reducer({ state: state, params: action.params, put: put, dispatch: function dispatch(action) {
_dispatch(that, action);
} });

if (result != null) {
put(result);
}
}
exports.dispatch = _dispatch;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "uadp-react",
"description": "uadp component",
"main": "lib/index.js",
"version": "1.4.9",
"version": "1.5.2",
"repository": {
"type": "git",
"url": "https://github.com/www3com/uadp-react"
Expand Down
88 changes: 48 additions & 40 deletions src/stateMgr/modelMgr.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,74 @@
* Created by wangjz on 2017/6/22.
*/
import map from '../map';

const put = map.put;
const get = map.get;

export function setModel(model) {
if(model == null) {
throw new Error("状态管理器缺少model");
}
if (model == null) {
throw new Error("状态管理器缺少model");
}

let initSate = {};
if(model.state != null) {
initSate = model.state;
}
put('storageName', model.namespace);
put(model.namespace, {state: initSate, reducers: model.reducers});
let initSate = {};
if (model.state != null) {
initSate = model.state;
}
if (model.namespace == null) {
model.namespace = "defaultNamespace";
}
put('storageName', model.namespace);
put(model.namespace, {state: initSate, reducers: model.reducers});
}

export function debug(flag) {
put('_debug', true);
put('_debug', true);
}

export function getReducer(type) {
let reducers = get(get('storageName')).reducers;
if(reducers == null) {
throw new Error("状态管理器缺少model");
}
let reducer = reducers[type];
if(reducer == null) {
throw new Error("模型未配置reducer:" + type);
}
return reducer;
let reducers = get(get('storageName')).reducers;
if (reducers == null) {
throw new Error("状态管理器缺少model");
}
let reducer = reducers[type];
if (reducer == null) {
throw new Error("模型未配置reducer:" + type);
}
return reducer;
}

export function getState() {
let state = get(get('storageName')).state;
return state;
let state = get(get('storageName')).state;
return state;
}

export function setState(state) {
let model = get(get('storageName'));
model.state = state;
put(get('storageName'), model);
let model = get(get('storageName'));
model.state = state;
put(get('storageName'), model);
}

export function dispatch(that, action) {
let state = getState();
let reducer = getReducer(action.type);
var put = function(res) {
Object.assign(state, res);
setState(state);
if(get('_debug')) {
console.log("状态发生变化,命名空间:" + get('storageName') + ",内容:", state);
}
that.setState({});
};
let state = getState();
let reducer = getReducer(action.type);
var put = function (res) {
Object.assign(state, res);
setState(state);
if (get('_debug')) {
console.log("状态发生变化,命名空间:" + get('storageName') + ",内容:", state);
}
that.setState({});
};

// let result = reducer(state, action.params, put, function(action) {
// dispatch(that, action);
// });

let result = reducer(state, action.params, put, function(action) {
dispatch(that, action);
});
let result = reducer({state: state, params: action.params, put: put, dispatch: function (action) {
dispatch(that, action);
}});

if(result != null) {
put(result);
}
if (result != null) {
put(result);
}
}

0 comments on commit f9dadac

Please sign in to comment.