Last active
August 15, 2016 21:44
-
-
Save wbuchwalter/d1448395f0dee9212b70 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {INgRedux} from 'ng-redux'; | |
export default class NgReduxStub implements INgRedux { | |
private selectedState: any; | |
private target: any; | |
private mapState: (state: any) => Object; | |
push(selectedState: any) { | |
if (!_.isPlainObject(selectedState)) { | |
throw 'selectedState must be a plain object'; | |
} | |
this.selectedState = selectedState; | |
this.updateTarget(); | |
} | |
connect( | |
mapStateToTarget: (state: any) => Object, | |
mapDispatchToTarget?: Object | ((dispatch: Function) => Object) | |
): (target: Function | Object) => () => void { | |
this.mapState = mapStateToTarget; | |
return (target: Function | Object) => { | |
this.target = target; | |
this.updateTarget(); | |
return () => { }; | |
}; | |
} | |
updateTarget() { | |
if (!this.target || !this.mapState || !this.selectedState) { | |
return; | |
} | |
if (_.isFunction(this.target)) { | |
this.target(this.mapState(this.selectedState)); | |
return; | |
} | |
_.assign(this.target, this.mapState(this.selectedState)); | |
} | |
//This properties are useless (right now) for testing, only there to comply with the interface | |
getReducer(): Redux.Reducer { | |
return undefined; | |
}; | |
replaceReducer() { | |
return undefined; | |
} | |
dispatch() { | |
return undefined; | |
} | |
getState() { | |
return undefined; | |
} | |
subscribe() { | |
return undefined; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment