1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-22 17:33:14 +01:00
onion/js/stores/error_queue_store.js
2015-12-10 18:56:19 +01:00

57 lines
1.5 KiB
JavaScript

'use strict';
import { alt } from '../alt';
import ErrorQueueActions from '../actions/error_queue_actions.js';
import { ErrorClasses } from '../constants/error_constants.js';
class ErrorQueueStore {
constructor() {
const { upload: { largeFileSize, tryDifferentBrowser } } = ErrorClasses;
this.errorQueues = {
'upload': {
queue: [largeFileSize, tryDifferentBrowser],
loop: true
}
};
// Add intial index to each error queue
Object
.keys(this.errorQueues)
.forEach((type) => {
this.errorQueues[type].index = 0;
});
// Bind the exported functions to this instance
this.getNextError = this.getNextError.bind(this);
this.exportPublicMethods({
getNextError: this.getNextError
});
this.bindActions(ErrorQueueActions);
}
getNextError(type) {
const errorQueue = this.errorQueues[type];
const { queue, index } = errorQueue;
ErrorQueueActions.shiftErrorQueue(type);
return queue[index];
}
onShiftErrorQueue(type) {
const errorQueue = this.errorQueues[type];
const { queue, loop } = errorQueue;
++errorQueue.index;
if (loop) {
// Loop back to the beginning if all errors have been exhausted
errorQueue.index %= queue.length;
}
}
}
export default alt.createStore(ErrorQueueStore, 'ErrorQueueStore');