Skip to content

Instantly share code, notes, and snippets.

@ruslanchek
Forked from tgroshon/AppDispatcher.js
Last active April 23, 2016 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruslanchek/9429363a3e0ecdb5739ed47e1091fad9 to your computer and use it in GitHub Desktop.
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.
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