-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-mixin.js
335 lines (271 loc) · 9.86 KB
/
service-mixin.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
* @license MIT
* cinetech (muvisho)
* Copyright (c) 2023 Abraham Ukachi. The Muvisho Project Contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @name: Service
* @type: mixin
* @author: Abraham Ukachi <[email protected]>
* @contributor: Hajar Aslan <[email protected]>
*
* Example usage:
* 1-|> import { serviceMixin } from './src/helpers/mixins/service-mixin.js';
* -|>
* -|> Assign.object(App.prototype, serviceMixin);
* -|>
*
*
* 2-|> // Start or launch a service (inside a Class)
* -|>
* -|> const delay = 60; // <- every 60 second
* -|>
* -|> const storageServiceId = this.startService('Storage', (storageService) => {
* -|> this._storageServiceHandler(storageService)
* -|> }, delay);
* -|>
*
*
* 3-|> // Stop or kill a running service (inside a Class)
* -|>
* -|> const storageServiceId = 300;
* -|> this.stopService(storageServiceId);
* -|>
*
* 4-|> // Restart service (inside a Class)
* -|>
* -|> const storageServiceId = 300;
* -|> this.restartService(storageServiceId, (storageService) => { this._storageServiceHandler(storageService) });
* -|>
*/
// Import a service helper
// import { Service } from './helpers/service-mixin.js';
"use strict";
// ^^^^^^^^^ This keeps us on our toes, as it forces us to use all pre-defined variables, among other things 😅
// defining some constant variables...
const PREFIX_SERVICE_TIMER = 'serviceTimer';
/**
* List of currently running serivces
*
* @type { Array[Object] }
* @private
*/
const runningServices = [];
/**
* `serviceMixin`
* This is a mixin that is used to start, stop, restart and verify a service.
* NOTE: A recursive method is used in this mixin for better performance.
*
* Example usage:
* (See above examples)
*/
export const serviceMixin = {
/* >>> public methods <<< */
/**
* Method used to start a new service
*
* @param { String } name - the service name
* @param { Function } func - a callback function to be executed every `delay` seconds.
* @param { Number } delay - the time, in seconds the timer should delay in between executions of the specific function
*
* @returns { Number } sid - a unique service ID for the newly activated service.
*/
startService(name, func, delay) {
// Initialize the `sid` variable with a randomly generated service ID
let sid = this._getRandomServiceId();
// get the current timestamp as `timestamp`
let timestamp = this._getCurrentTimestamp();
// create a service object as `service` with the specified `name`, `sid`, `timestamp` and `delay`
let service = {name, sid, timestamp, delay};
// launch the service timer
this._launchServiceTimer(service, func);
// add this service to the `runningServices` list
runningServices.push(service);
// DEBUG [4dbsmaster]: tell me about it ;)
console.log(`\x1b[44m\x1b[2m[startService]: name => ${name} & sid => ${sid} & timestamp => ${timestamp}\x1b[0m`);
// return the `sid`
return sid;
},
/**
* Method used to stop a service with the given `serviceId`
*
* @param { Number } serviceId
*
* @returns { Boolean } Returns TRUE, if the the specified service was stopped successfully ;)
*/
stopService(serviceId) {
// Initialize the `result` variable by setting it to FALSE.
let result = false;
// do nothing if there's no `serviceId`
if (typeof serviceId === 'undefined') { return }
// IDEA: Check if the given `serviceId` exists
// If a service with this `serviceId` exists...
if (this.verifyServiceId(serviceId)) {
// ...get the corresponding service timer as `serviceTimer`
let serviceTimer = this._getServiceTimerById(serviceId);
// stop / cancel the `serviceTimer`
clearTimeout(serviceTimer);
// remove this service from the list
this._removeServiceById(serviceId);
// set `result` to TRUE
result = true;
}
// return `result`
return result;
},
/**
* Restarts the an already running service
*
* @param { Number } serviceId
* @param { Function } func
* @param { Number } delay
*
* @returns { Number } sid - a new service ID for the restarted service.
*/
restartService(serviceId, func, delay = null) {
// get the actual service as `service`
let service = this.getServiceById(serviceId);
// get the service name from `service` as `serviceName`
let serviceName = service.name;
// If no delay was given, use the current service delay
delay = delay ?? service.delay;
// stop the service
this.stopService(serviceId);
// restart the service
let sid = this.startService(serviceName, func, delay);
// return the sid;
return sid;
},
/**
* Verifies if the given `serviceId` exists in the list of running services.
*
* @param { Number } serviceId
*
* @returns { Boolean } - returns TRUE, if the specified service ID was found.
*/
verifyServiceId(serviceId) {
// look/find for exact index of the given `serviceId` in the list of `runningServices` as `serviceIndex`
let serviceIndex = runningServices.findIndex((service) => service.sid === serviceId);
// if the `serviceIndex` is not -1, that means it was found in the list
let foundService = (serviceIndex !== -1) ? true : false; // <- NN ik ;)
return foundService;
},
/**
* Returns a list of all currently running services
*
* @param { Boolean } onlyIds - If TRUE, only the service ids will be returned
*
* @returns { Array[Object] } allServices
*/
getAllServices(onlyIds = false) {
// Initialize the `allServices` variable
let allServices = runningServices;
if (onlyIds) {
allServices = allServices.map((service) => service.sid);
}
// return `allServices`
return allServices;
},
/**
* Returns the service of the given `serviceId`
*
* @param { Number } serviceId
*
* @returns { Object }
*/
getServiceById(serviceId) {
return runningServices.find((service) => service.sid === serviceId);
},
/* >>> public getters <<< */
/* >>> public setters <<< */
/* >>> private methods <<< */
/**
* Launches a service timer
* NOTE: This is a recursive function
*
* @param { Object } service - the service object containing the name, service id, timestamp and delay of the service to run
* @param { Function } func - A callback function
*/
_launchServiceTimer(service, func) {
// setup a service timer using the service id (e.g. 'serviceTimer324'
this[PREFIX_SERVICE_TIMER + service.sid] = setTimeout((timer) => {
// add a runtime to the service
service.runtime = this._getCurrentTimestamp() - service.timestamp;
// call back the `func`
func(service, timer);
// re-run the service launcher
this._launchServiceTimer(service, func);
}, service.delay * 1000);
},
/**
* Returns a random service ID.
* NOTE: This is a unique ID that hasn't been used by another service
*
* @return { Number } randomServiceId
*/
_getRandomServiceId() {
// Initialize the `randomServiceId` variable,
// by setting it to a random number between 1 and 1000
let randomServiceId = Math.floor(Math.random(1) * 1000);
// DEBUG [4dbsmaster]: tell me about it ;)
console.log(`\x1b[4;1;34m[_getRandomServiceId](1): randomServiceId => ${randomServiceId}\x1b[0m`);
console.log(`\x1b[4;1;34m[_getRandomServiceId](2): this.verifyServiceId => ${this.verifyServiceId(randomServiceId)}\x1b[0m`);
console.log(`\x1b[4;1;34m[_getRandomServiceId](3): runningServices => \x1b[0m`, runningServices);
// Making sure our service Id is actually random and unique...
// If `randomServiceID` already exists...
while (this.verifyServiceId(randomServiceId) == true) {
// ...generate a new service otherwise
randomServiceId = Math.floor(Math.random(1) * 1000);
}
// Return the `randomServiceId`
return randomServiceId;
},
/**
* Returns the current timestamp
*
* @returns { Number } - e.g. 1679161593103
*/
_getCurrentTimestamp() {
return (new Date()).getTime();
},
/**
* Returns the service timer of the given `serviceId`
*
* @param { Number } serviceId
* @returns { setTimeout } serviceTimer
*/
_getServiceTimerById(serviceId) {
return this[PREFIX_SERVICE_TIMER + serviceId];
},
/**
* Method used to remove a service from the `runningServices` list,
* using the specified `serviceId`
*
* @param { Number } serviceId
*/
_removeServiceById(serviceId) {
// get the index of this service as `serviceIndex`
let serviceIndex = runningServices.findIndex((service) => service.sid === serviceId);
// remove the service from `_runningServices`, using the given `serviceId`
runningServices.splice(serviceIndex, 1);
}
/* >>> private getters <<< */
/* >>> private setters <<< */
}; // <- End of `serviceMixin`