Giter Site home page Giter Site logo

state-machine's Introduction

State Machine

利用状态机解决复杂流程

目的在于将步骤与每一步具体逻辑分层

###demo

function checkSessionIdExists(next){
    return next(sessionStorage.checkExists('sessionId') ? 'true' : 'false');
}

function getUserInfo(next){
    var token = sessionStorage.get('sessionId');
    return proxy.getUserInfo(token).then(function(res){
        return next(res.code === 20000 ? 'true' : 'error', res.user || {});
    }, function(res){
        var status = res.response.status;
        sessionStorage.remove('sessionId');
        return next(status && status === 403 ? '403' : 'error');
    });
}

function getToken(next){
    return proxy.getToken().then(function(res){
        var token = res.token;
        if(!token){
            return next('error');
        }
        sessionStorage.add('sessionId', token);
        next('true');
    }, function(res){
        var status = res.response.status;
        return next(status && status === 403 ? '403' : 'error');
    });
}

function getLayout(next, user){
    return proxy.getUserLayout(user.serialNumber).then(function(res) {
    var app = widget.getCacheWidget('app');
    var resources = res.resources;
    if(resources != null) {
        stateIns.getAction('userAction')(user);
        stateIns.getAction('layoutAction')(resources);
    } else {
        app.changeToErrorPage();
    }
    }.bind(this), function(err) {
        var app = widget.getCacheWidget('app');
        app.changeToErrorPage();
    });
}

function redirect(){
    return proxy.sso();
}

function error(){
    var app = widget.getCacheWidget('app');
    app.changeToErrorPage();
}

function State(process, current){
    this.current = current;
    this.steps = {};
    this.process = JSON.parse(JSON.stringify(process));
}

/**
 * 定义流程 状态切换 只关注当前的步骤与下一步映射
 */
var process = {
    'checkSessionIdExists': {
        'true': 'getUserInfo',
        'false': 'getToken'
    },
    'getUserInfo': {
        'true': 'getLayout',
        '403': 'getToken',
        'error': 'error'
    },
    'getToken': {
        'true': 'getUserInfo',
        '403': 'redirect',
        'error': 'error'
    },
    'getLayout': {},
    'redirect': {}
};

/**
 * 实例化
 */
var loginState = new State(process, 'checkSessionIdExists');

/**
 * 注册响应函数
 */
loginState
.register('checkSessionIdExists', checkSessionIdExists)
.register('getToken', getToken)
.register('getUserInfo', getUserInfo)
.register('getLayout', getLayout)
.register('redirect', redirect)
.register('error', error);

/**
 * 启动
 */
loginState.run();

state-machine's People

Contributors

qudi15 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.