zoukankan      html  css  js  c++  java
  • What is Flux?

    Pluralsight - React and Flux for Angular Developers

    1. An architectural concept. It a idea.

    2. Not a library.

    Key Thinking

    • One way data flow.
    • Events emmiters and dispatchers.

    Javascript Events emmiter

    • Fake events.Really just a arrays of function.
    • Not browser events.

    Emmiter idea code:

    function EventEmmiter(){
        this._events = {};
    }
    
    EventEmmiter.prototype.on = function(type, listener){
        this._events[type] = this._events[type] || [];
        this._events[type].push(listener);
    }
    
    EventEmmiter.prototype.emit = function(type){
        if(this._events[type]){
            this._events[type].forEach(function(listener){
                listener();
            });
        }
    }
    
    EventEmmiter.prototype.removeListener = function(type, listener){
        if(this._events[type]){
            this._events[type].splice(this._events[type].indexOf(listener), 1);
        } 
    }
    

    Dispatcher in javascript

    • Fake event listening. More objects and functions.
    • Functions that respond to particular actions.

    Dispatcher idea code:

    function Dispatcher(){
        this._lastID = 0;
        this._callbacks = {};
    }
    
    Dispatcher.prototype.register = function(callback){
        var id = 'CID_' + this._lastID++;
        this._callback[id] = callback;
    }
    
    Dispatcher.prototype.dispatch = function(action){
        for(var id in this._callback){
            this._callbacks[id](action);
        }
    }
    
    Dispatcher.prototype.waitFor = function(ids){
        //TODO
    }
    

    One way data flow


    Two way data flow


  • 相关阅读:
    java入门-使用idea创建web项目
    java入门-gitlab
    linux基础:source和sh的区别
    github基本使用
    docker-compose
    k8s学习笔记之六:flannel网络配置
    计算机网络
    python自学之路--python面试题
    ASP.NET前后端分离框架(转载)
    ASP.NET Core初步使用Quartz.NET(转载)
  • 原文地址:https://www.cnblogs.com/skating/p/6169000.html
Copyright © 2011-2022 走看看