-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.js
executable file
·129 lines (121 loc) · 4.1 KB
/
queue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*==============================================================================
___ _ _ _
/ || | | | | |
\__ | | | | | | __
/ |/ |/_) |/ / \_/\/
\___/|__/| \_/|__/\__/ /\_/
|\
|/
Queue
https://github.com/Elkfox/queue
Author: Team Elkfox
Copyright (c) 2018 Elkfox Co Pty Ltd
https://elkfox.com
MIT License
==============================================================================*/
const Queue = function Queue (configuration) {
// Deafult settings
const defaultConfig = {
method: 'GET',
url: null,
dataType: '',
data: null,
success: function success (success) {
const successEvent = new CustomEvent(this.config.completedRequestEvent, { response: success })
document.dispatchEvent(successEvent)
},
error: function error (error) {
const errorEvent = new CustomEvent(this.config.failedRequestEvent, { response: error })
document.dispatchEvent(errorEvent)
},
completedAllRequestsEvent: 'Queue:requestsCompleted',
completedRequestEvent: 'Queue:requestCompleted',
failedRequestEvent: 'Queue:requestFailed',
requestStartedEvent: 'Queue:requestStarted',
errorEvent: 'Queue:error'
}
this.config = Object.assign(defaultConfig, configuration)
this.queue = []
this.processing = false
this.add = this.add.bind(this)
this.process = this.process.bind(this)
}
Queue.prototype.add = function addRequestToQueue (req) {
const request = Object.assign({}, this.config, req)
if (!request.url) {
const errorEvent = new CustomEvent(this.config.errorEvent, { response: 'Queue: No url provided' })
document.dispatchEvent(errorEvent)
}
const defaultSuccess = this.config.success
const defaultError = this.config.error
request.success = [request.success] || [defaultSuccess]
request.error = [request.error] || [defaultError]
this.queue.push(request)
// Check to see if the queue is already running
if (this.processing) {
return true
}
try {
const startedEvent = new Event(this.config.requestStarted)
document.dispatchEvent(startedEvent)
} catch (error) {
const errorEvent = new CustomEvent(this.config.errorEvent, { response: error })
document.dispatchEvent(errorEvent)
}
this.processing = false
this.process()
return this.processing
}
Queue.prototype.process = function processQueue () {
if (!this.queue.length) {
this.processing = false
const completedEvent = new Event(this.config.completedAllRequestsEvent)
document.dispatchEvent(completedEvent)
return
}
this.processing = true
const request = this.queue.shift()
request.success.push(this.process)
request.error.push(this.process)
try {
// jQuery.ajax(request)
// Create the ajax request
this.request = new XMLHttpRequest()
// Setup the success and error callbacks
this.request.onreadystatechange = function requestStateChanged () {
// If the request is completed
if (this.readyState === 4) {
// If the status code is okay
if (this.status === 200) {
// The response is a success
// Execute all functions in the success array
for (let func = 0; func < request.success.length; func += 1) {
request.success[func](this.response)
}
} else {
// The response is an error
// Execute all functions in the error array
for (let func = 0; func < request.error.length; func += 1) {
request.error[func](this.response)
}
}
}
}
} catch (error) {
const errorEvent = new CustomEvent(this.config.errorEvent, { response: error })
document.dispatchEvent(errorEvent)
}
// Open the request with the currect url and method
this.request.open(request.method, request.url)
// Define the response type, the default being json
this.request.responseType = request.dataType
// Send the request
if (typeof request.data === 'object') {
this.request.setRequestHeader('Content-Type', 'application/json')
this.request.send(JSON.stringify(request.data))
return request
}
this.request.send(request.data)
return request
}
module.exports = Queue;