-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
124 lines (108 loc) · 3.56 KB
/
index.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
"use strict";
// Require Third-party Dependencies
const Config = require("@slimio/config");
const Scheduler = require("@slimio/scheduler");
const timer = require("@slimio/timer");
const is = require("@slimio/is");
const SafeEmitter = require("@slimio/safe-emitter");
const ms = require("ms");
/**
* @function getSchedulerOption
* @param {!number} interval
* @param {string} [startAt]
* @returns {object}
*/
function getSchedulerOption(interval, startAt) {
const startDate = typeof startAt === "string" ? startAt : Date.now();
let intervalUnitType = Scheduler.Types.Seconds;
if (typeof interval === "string") {
// eslint-disable-next-line
interval = ms(interval);
intervalUnitType = Scheduler.Types.Milliseconds;
}
return { interval, intervalUnitType, startDate };
}
/**
* @async
* @function profiles
* @param {!string} configPath
* @param {Function|null} [predicate]
* @returns {Promise<void>}
*
* @throws {TypeError}
* @throws {Error}
*/
async function profiles(configPath, predicate = null) {
if (typeof configPath !== "string") {
throw new TypeError("configPath must be a string");
}
if (predicate !== null && typeof predicate !== "function") {
throw new TypeError("predicate must be a null or a function");
}
// Create and read configuration
const cfg = new Config(configPath, {
createOnNoEntry: true,
autoReload: true,
writeOnSet: true
});
await cfg.read({ profiles: {} });
// Verify profiles section
{
const currProfileValue = cfg.get("profiles");
const currType = typeof currProfileValue;
if (currType !== "object" && currType !== "undefined") {
throw new Error(`configuration profiles section has been detected as: ${currType}`);
}
else if (!is.plainObject(currProfileValue)) {
cfg.set("profiles", {});
}
}
/** @type {Map<string, Profile>} */
let activeProfiles = new Map();
let events = new SafeEmitter();
cfg.observableOf("profiles").subscribe({
next(currProfiles) {
const fetched = new Set();
for (const [name, value] of Object.entries(currProfiles)) {
const { active = true, interval = 1000, startAt, ...payload } = value;
if (active) {
const timer = new Scheduler(getSchedulerOption(interval, startAt));
activeProfiles.set(name, { active, timer, payload });
fetched.add(name);
}
}
[...activeProfiles.keys()]
.filter((name) => !fetched.has(name))
.forEach((name) => activeProfiles.delete(name));
},
error(err) {
console.error(err);
}
});
const profilesInterval = timer.setInterval(async() => {
for (const [name, profile] of activeProfiles.entries()) {
if (!profile.timer.walk()) {
continue;
}
if (predicate !== null && !predicate(name, profile.payload)) {
continue;
}
events.emit("walk", name, profile.payload);
}
}, 100);
return {
events,
get(profileName) {
if (!activeProfiles.has(profileName)) {
return null;
}
return activeProfiles.get(profileName).payload;
},
free() {
timer.clearInterval(profilesInterval);
activeProfiles = undefined;
events = undefined;
}
};
}
module.exports = profiles;