Share Gaia assets and adapters, keep private profiles and development documents local, and update both public READMEs.
5 lines
No EOL
2 KiB
JavaScript
5 lines
No EOL
2 KiB
JavaScript
|
|
define(['require','modules/base/module','modules/mvvm/observable'],function(require){'use strict';var Module=require('modules/base/module');var Observable=require('modules/mvvm/observable');var StateModel=Module.create(function StateModel({onInit,onGetState,onSetState}){if(typeof onInit!=='function'||typeof onGetState!=='function'||typeof onSetState!=='function'){this.throw('invalid arguments');}
|
|
this.super(Observable).call(this);var argument=arguments[0];this._init(onInit.bind(argument),onGetState.bind(argument),onSetState.bind(argument));}).extend(Observable);Observable.defineObservableProperty(StateModel.prototype,'targetState',{value:null});Observable.defineObservableProperty(StateModel.prototype,'currentState',{readonly:true,value:null});Observable.defineObservableProperty(StateModel.prototype,'transitioning',{readonly:true,value:false});StateModel.prototype._init=function(onInit,onGetState,onSetState){this._onInit=onInit;this._onGetState=onGetState;this._onSetState=onSetState;this.targetState=this._onInit((realState)=>{if(!this._transitioning){this._currentState=realState;this.targetState=realState;}});this._currentState=this.targetState;this._onGetState().then((realState)=>{this._currentState=realState;if(this.targetState!==realState){this._setState(this.targetState);}
|
|
this.observe('targetState',(newState)=>{if(this._transitioning||newState===this._currentState){return;}
|
|
this._setState(newState);});}).catch((err)=>{this.error('initialization error '+err);});};StateModel.prototype._setState=function(state){this.debug('set state: '+state);this._transitioning=true;return this._onSetState(state).then((realState)=>{this.debug('set state completed: '+state+', real state: '+realState);this._currentState=realState;if(this.targetState!==realState){this._setState(this.targetState);}else{this._transitioning=false;}},(realState)=>{this.debug('set state failed: '+state+', real state: '+realState);this._currentState=realState;this.targetState=realState;this._transitioning=false;});};return StateModel;}); |