Skip to content

Commit

Permalink
fixed async prototype flag change state
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian committed Jul 11, 2017
1 parent 66db54e commit d446365
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
27 changes: 16 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ function isFunction(value){
return value instanceof Function;
}
function isObject(value){
return !! value && value.constructor === Object;
//return value instanceof Object;
// TODO: find out why this is braking the tests
//return !! value && value.constructor === Object;
return value instanceof Object && !isArray(value);
}

const isArray = Array.isArray;
Expand Down Expand Up @@ -197,17 +198,21 @@ function buildActionLayout(fileNameArray){
// check if newState's prototype is the shared Object?
//console.log (action.type, newState, ({}).__proto__ === newState.__proto__)

if(newAsyncVal && (isObject(newState) || isArray(newState))){
// I am a redux-auto proto
const _newProto_ = {async}
if(newAsyncVal){
if(isObject(newState)){

if(newState.__proto__.hasOwnProperty("async"))
_newProto_.__proto__ = newState.__proto__.__proto__;
else
_newProto_.__proto__ = newState.__proto__;
const newStateWithAsync = Object.create({async});
return Object.assign(newStateWithAsync,newState); // clone object

newState.__proto__ = _newProto_;
}
}else if(isArray(newState)){
// I am a redux-auto proto
const _newProto_ = Object.create(Array.prototype)
_newProto_.async = async;
newState = newState.slice(0); // clone array

newState.__proto__ = _newProto_;
} // END isArray
} // END if(newAsyncVal)
return newState

} // END reducers[reducerName] = (data, action) => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-auto",
"version": "1.6.0",
"version": "1.6.1",
"description": "automatically generate stories and actions from your folder and file structure",
"main": "index.js",
"scripts": {
Expand Down
5 changes: 4 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,10 @@ describe('initialization', () => {

})

webpackModules.set(propName,"init","action", ()=> Promise.resolve({posts:[1,2,3,4,5]}) )
webpackModules.set(propName,"init","action", function(payload,state){
expect(Array.isArray(state)).toBe(true)
return Promise.resolve({posts:[1,2,3,4,5]})
} )
RefrashStore();
// should be automatically called
})
Expand Down

0 comments on commit d446365

Please sign in to comment.