-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
87 lines (87 loc) · 2.72 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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Context = void 0;
var async_hooks_1 = require("async_hooks");
/**
* the container interface that keeps the data stored in the Map
* to be used by the async hooks
*/
/* c8 ignore start */
var Context = /** @class */ (function () {
function Context() {
}
/* c8 ignore end */
Context.Loader = function () {
/**
* The storage that stores all the data of the context.
*/
this.store = new async_hooks_1.AsyncLocalStorage();
};
var _a;
_a = Context;
/**
* Method used to store data in the context.
* @param {object} data - the data to be stored.
* @param {object} [options] - options for data to be stored.
* @param {boolean} [options.overwrite] - if false, does not overwrite the data if key already exists.
* @returns {boolean} - true if the data was set succesfully, false otherwise.
*/
Context.set = function (data) {
var payload = _a.store.getStore();
if (!payload) {
_a.store.enterWith(data);
return true;
}
var keys = Object.keys(data);
if (keys.length) {
for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
var key = keys_1[_i];
if (typeof data[key] !== "undefined") {
payload[key] = data[key];
}
}
_a.store.enterWith(payload);
return true;
}
return false;
};
/**
* Method used to retrieve the data shared in the context.
* @param {string} key - the key of the data to be retrieved.
* @returns {T | undefined} the data stored in the context.
*/
Context.get = function (key) {
if (key === void 0) { key = null; }
var payload = _a.store.getStore();
if (payload) {
if (key) {
return payload[key];
}
return payload;
}
return undefined;
};
/**
* Method used to delete the data stored in the context.
* @param {string} [key] - the key of the data to be removed.
*/
Context.remove = function (key) {
var payload = _a.store.getStore();
if (payload) {
if (key) {
delete payload[key];
}
else {
var keys = Object.keys(payload);
for (var _i = 0, keys_2 = keys; _i < keys_2.length; _i++) {
var item = keys_2[_i];
delete payload[item];
}
}
_a.store.enterWith(payload);
}
};
return Context;
}());
exports.Context = Context;
//# sourceMappingURL=index.js.map