Skip to content

Instantly share code, notes, and snippets.

@kareniel
Forked from wbuchwalter/nRedux.stub.ts
Created August 15, 2016 21:44
Show Gist options
  • Select an option

  • Save kareniel/ac08b1b4e7c817583da649452a8839fe to your computer and use it in GitHub Desktop.

Select an option

Save kareniel/ac08b1b4e7c817583da649452a8839fe to your computer and use it in GitHub Desktop.
export default function NgReduxStub() {
const ctrl = this
ctrl.selectedState = ""
ctrl.target = ""
ctrl.mapState = (state) => ({})
ctrl.push = (selectedState) => {
if (!isPlainObject(selectedState)) {
throw 'selectedState must be a plain object'
}
this.selectedState = selectedState;
this.updateTarget()
}
ctrl.connect = (mapStateToTarget, mapDispatchToTarget) => {
this.mapState = mapStateToTarget
return (target) => {
this.target = target
this.updateTarget()
return () => { }
}
}
ctrl.updateTarget = () => {
if (!this.target || !this.mapState || !this.selectedState) {
return
}
if (typeof this.target == 'function') {
this.target(this.mapState(this.selectedState))
return
}
Object.assign(this.target, this.mapState(this.selectedState))
}
//This properties are useless (right now) for testing, only there to comply with the interface
ctrl.getReducer = () => {
return undefined
}
ctrl.replaceReducer = () => {
return undefined
}
ctrl.dispatch = () => {
return undefined
}
ctrl.getState = () => {
return undefined
}
ctrl.subscribe = () => {
return undefined
}
}
function isObject(val) {
return val != null && typeof val === 'object' && Array.isArray(val) === false
}
function isObjectObject(o) {
return isObject(o) === true
&& Object.prototype.toString.call(o) === '[object Object]';
}
function isPlainObject(o) {
var ctor,prot
if (isObjectObject(o) === false) return false;
// If has modified constructor
ctor = o.constructor
if (typeof ctor !== 'function') return false;
// If has modified prototype
prot = ctor.prototype
if (isObjectObject(prot) === false) return false;
// If constructor does not have an Object-specific method
if (prot.hasOwnProperty('isPrototypeOf') === false) {
return false
}
// Most likely a plain Object
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment