-
-
Save ruslanchek/9429363a3e0ecdb5739ed47e1091fad9 to your computer and use it in GitHub Desktop.
A Flux Dispatcher with an Action Queue sitting on top to allow dispatching actions at any time.
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 {Dispatcher} from 'flux'; | |
const DELAY_TIME = 100; | |
export class QDispatcher { | |
private dispatcher:Dispatcher = null; | |
private isProcessing:boolean = false; | |
private actionQueue:any[] = []; | |
constructor() { | |
this.dispatcher = new Dispatcher(); | |
} | |
private queueAction(payload:any):void { | |
this.actionQueue.push(payload); | |
if (!this.isProcessing) { | |
this.startProcessing(); | |
} | |
} | |
private startProcessing():void { | |
this.isProcessing = true; | |
while (this.actionQueue.length > 0) { | |
if (this.dispatcher.isDispatching()) { | |
return setTimeout(this.startProcessing, DELAY_TIME); | |
} | |
let payload:any = this.actionQueue.shift(); | |
this.dispatcher.dispatch(payload); | |
} | |
this.isProcessing = false | |
} | |
public isProcessing():boolean { | |
return this.isProcessing; | |
} | |
public dispatch(payload:any):void { | |
this.queueAction(payload); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment