Last active
March 23, 2019 13:04
-
-
Save Wicker25/0bfa5998adf3c077fff42a2e9225189a 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 { action, observable } from 'mobx'; | |
export function withStateTransition(target) { | |
return class StateTransitionInjector extends target { | |
@observable state = 'idle'; | |
@action | |
setState(state) { | |
this.state = state; | |
} | |
}; | |
} | |
export function stateTransition(startState, endState) { | |
return function(target, name, descriptor) { | |
const original = descriptor.value; | |
descriptor.value = async function(...args) { | |
this.setState(startState); | |
try { | |
await original.apply(this, args); | |
this.setState(endState); | |
} catch (e) { | |
this.setState('error'); | |
throw e; | |
} | |
}; | |
return descriptor; | |
}; | |
} | |
@withStateTransition | |
export class StateTransition {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment