Comparing version 1.0.2 to 1.0.3
class KruxModule { | ||
constructor(params, options = {}) { | ||
this.name = ""; | ||
this.state = {}; | ||
this.reduces = {}; | ||
this.middlewares = {}; | ||
this.gettersActions = {}; | ||
this.getters = {}; | ||
this.store = null; | ||
this.actionsCreators = {}; | ||
this._data = { | ||
"store": {}, | ||
"reduces": {}, | ||
"middlewares": {}, | ||
"mutations": {}, | ||
"actions": {}, | ||
"mutationsCreators": {}, | ||
"actionsCreators": {} | ||
}; | ||
// this.middlewares = {}; | ||
// this.actionsCreators = {}; | ||
// for(let i in options) { | ||
// this[i] = options[i]; | ||
// } | ||
if(params.name) { | ||
@@ -16,65 +24,35 @@ this.name = params.name; | ||
for(let i in options) { | ||
this[i] = options[i]; | ||
if(params.state) { | ||
this._data.state = params.state; | ||
} | ||
if(params.state) { | ||
this.state = params.state; | ||
if(params.reduces) { | ||
this._data.reduces = params.reduces; | ||
} | ||
if(params.getters) { | ||
this.gettersActions = params.getters; | ||
if(params.middlewares) { | ||
this._data.middlewares = params.middlewares; | ||
} | ||
for(let i in params.getters) { | ||
this.getters[i] = this.getGetterCreator(i); | ||
if(params.mutations) { | ||
for(let i in params.mutations) { | ||
params.mutations[i] = params.mutations[i].bind(this); | ||
} | ||
} | ||
if(params.reduces) { | ||
this.reduces = params.reduces; | ||
this._data.mutations = params.mutations; | ||
for(let i in this.reduces) { | ||
this.actionsCreators[i] = this.getActionCreator(i); | ||
for(let i in this._data.mutations) { | ||
this._data.mutationsCreators[i] = this.getActionCreator(i); | ||
} | ||
} | ||
this.middlewares = {}; | ||
if(params.middlewares) { | ||
this.middlewares = { | ||
...this.middlewares, | ||
...params.middlewares | ||
}; | ||
} | ||
if(params.actions) { | ||
this.middlewares = { | ||
...this.middlewares, | ||
...params.actions | ||
}; | ||
} | ||
this._data.actions = params.actions; | ||
for(let i in this.middlewares) { | ||
this.actionsCreators[i] = this.getActionCreator(i); | ||
for(let i in this._data.actions) { | ||
this._data.actionsCreators[i] = this.getActionCreator(i); | ||
} | ||
} | ||
} | ||
dispatch(action, ...args) { | ||
return this.store.dispatch({ | ||
"type": action, | ||
"data": args | ||
}); | ||
} | ||
getGetterCreator(name) { | ||
return (...args) => { | ||
let state = this.getState(); | ||
return this.gettersActions[name].apply(this, [ | ||
state, | ||
...args | ||
]); | ||
}; | ||
} | ||
getActionCreator(name) { | ||
@@ -91,10 +69,18 @@ let _this = this; | ||
onAction(state, action) { | ||
reduce() { | ||
return (state, action) => { | ||
return this.onReduce(state, action); | ||
} | ||
} | ||
onReduce(state, action) { | ||
if(!state) { | ||
state = this.state; | ||
state = this._data.state; | ||
} | ||
let [name, type] = action.type.split("/"); | ||
let reducer = null; | ||
if(this.name === name && this.reduces[type]) { | ||
if(this._data.reduces["*"]) { | ||
reducer = this._data.reduces["*"]; | ||
let nState = { | ||
@@ -104,23 +90,40 @@ ...state | ||
this.reduces[type].apply(this, [ | ||
nState, | ||
...action.data | ||
]); | ||
reducer.apply(this, [nState, action]); | ||
return nState; | ||
state = { | ||
...nState | ||
}; | ||
} | ||
return state; | ||
} | ||
if(this._data.reduces[action.type]) { | ||
reducer = this._data.reduces[action.type]; | ||
onMiddleware(store, action) { | ||
if(action.type) { | ||
let nState = { | ||
...state | ||
}; | ||
reducer.apply(this, [nState, action]); | ||
return nState; | ||
} | ||
else { | ||
let [name, type] = action.type.split("/"); | ||
if(this.name === name && this.middlewares[type]) { | ||
return this.middlewares[type].apply(this, action.data); | ||
if(this.name === name && this._data.mutations[type]) { | ||
reducer = this._data.mutations[type]; | ||
let nState = { | ||
...state | ||
}; | ||
reducer.apply(this, [ | ||
nState, | ||
...action.data | ||
]); | ||
return nState; | ||
} | ||
} | ||
return null; | ||
return state; | ||
} | ||
@@ -136,21 +139,10 @@ | ||
return (action) => { | ||
if(action.type) { | ||
let [name, type] = action.type.split("/"); | ||
next(action); | ||
if(this.name === name && this.middlewares[type]) { | ||
return Promise.resolve( | ||
_this.onMiddleware(store, action) | ||
).then((data) => { | ||
if(data !== null && typeof data !== "undefined") { | ||
return Promise.resolve(next(action)).then((res) => { | ||
return data; | ||
}); | ||
} | ||
return Promise.resolve( | ||
_this.onMiddleware(store, action) | ||
).then((data) => { | ||
return next(action); | ||
}); | ||
} | ||
} | ||
return next(action); | ||
return data; | ||
}); | ||
}; | ||
@@ -161,14 +153,21 @@ }; | ||
reduce() { | ||
return (state, action) => { | ||
return this.onAction(state, action); | ||
onMiddleware(store, action) { | ||
if(this._data.middlewares["*"]) { | ||
this._data.middlewares["*"].apply(this, [action]); | ||
} | ||
} | ||
setActions(store, redux) { | ||
for(let i in this.actionsCreators) { | ||
if(!this[i]) { | ||
this[i] = redux.bindActionCreators(this.actionsCreators[i], store.dispatch); | ||
if(this._data.middlewares[action.type]) { | ||
return this._data.middlewares[action.type].apply(this, [action]); | ||
} | ||
else { | ||
if(action.type) { | ||
let [name, type] = action.type.split("/"); | ||
if(this.name === name && this._data.actions[type]) { | ||
return this._data.actions[type].apply(this, action.data); | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
@@ -184,16 +183,30 @@ | ||
mapGetters(params) { | ||
let getters = {}; | ||
setActions(store, redux) { | ||
for(let i in this._data.mutationsCreators) { | ||
if(!this[i]) { | ||
this[i] = redux.bindActionCreators( | ||
this._data.mutationsCreators[i], | ||
store.dispatch | ||
); | ||
} | ||
} | ||
for(let i in params) { | ||
if(this.getters[params[i]]) { | ||
getters[i] = this.getters[params[i]]; | ||
for(let i in this._data.actionsCreators) { | ||
if(!this[i]) { | ||
this[i] = redux.bindActionCreators( | ||
this._data.actionsCreators[i], | ||
store.dispatch | ||
); | ||
} | ||
else { | ||
console.error(this.name, i); | ||
} | ||
} | ||
return getters; | ||
} | ||
getActions(params) { | ||
dispatch(action, ...args) { | ||
return this.store.dispatch({ | ||
"type": action, | ||
"data": args | ||
}); | ||
} | ||
@@ -200,0 +213,0 @@ } |
{ | ||
"name": "kpdux", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"author": "Kris Papercut <kearis666@gmail.com>", | ||
@@ -5,0 +5,0 @@ "license": "ISC", |
8061
234