@beecode/msh-util 2.0.0-alpha → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/array-util.d.ts +23 -0
- package/lib/array-util.d.ts.map +1 -0
- package/lib/array-util.js +32 -0
- package/lib/class-factory-pattern.d.ts +25 -0
- package/lib/class-factory-pattern.d.ts.map +1 -0
- package/lib/class-factory-pattern.js +39 -0
- package/lib/express/error-handler.d.ts +17 -0
- package/lib/express/error-handler.d.ts.map +1 -0
- package/lib/express/error-handler.js +32 -0
- package/lib/index.d.ts +17 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +108 -0
- package/lib/joi-util.d.ts +47 -0
- package/lib/joi-util.d.ts.map +1 -0
- package/lib/joi-util.js +107 -0
- package/lib/memoize-factory.d.ts +16 -0
- package/lib/memoize-factory.d.ts.map +1 -0
- package/lib/memoize-factory.js +34 -0
- package/lib/object-util.d.ts +72 -0
- package/lib/object-util.d.ts.map +1 -0
- package/lib/object-util.js +173 -0
- package/lib/package.json +1 -0
- package/lib/regex-util.d.ts +12 -0
- package/lib/regex-util.d.ts.map +1 -0
- package/lib/regex-util.js +17 -0
- package/lib/single-threshold-promise.d.ts +31 -0
- package/lib/single-threshold-promise.d.ts.map +1 -0
- package/lib/single-threshold-promise.js +95 -0
- package/lib/singleton/async.d.ts +50 -0
- package/lib/singleton/async.d.ts.map +1 -0
- package/lib/singleton/async.js +138 -0
- package/lib/singleton/pattern.d.ts +34 -0
- package/lib/singleton/pattern.d.ts.map +1 -0
- package/lib/singleton/pattern.js +46 -0
- package/lib/string-util.d.ts +10 -0
- package/lib/string-util.d.ts.map +1 -0
- package/lib/string-util.js +23 -0
- package/lib/time-util.d.ts +74 -0
- package/lib/time-util.d.ts.map +1 -0
- package/lib/time-util.js +127 -0
- package/lib/time-zone.d.ts +467 -0
- package/lib/time-zone.d.ts.map +1 -0
- package/lib/time-zone.js +473 -0
- package/lib/timeout.d.ts +15 -0
- package/lib/timeout.d.ts.map +1 -0
- package/lib/timeout.js +24 -0
- package/lib/type-util.d.ts +50 -0
- package/lib/type-util.d.ts.map +1 -0
- package/lib/type-util.js +59 -0
- package/lib/types/global.d.js +5 -0
- package/lib/types/types.d.js +3 -0
- package/package.json +2 -2
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.ObjectUtil = void 0;
|
|
7
|
+
var _lodash = _interopRequireDefault(require("lodash.clonedeep"));
|
|
8
|
+
var _util = _interopRequireDefault(require("util"));
|
|
9
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
10
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
11
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
12
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
13
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
14
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
15
|
+
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
16
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
17
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
18
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
19
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
20
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
21
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
23
|
+
var ObjectUtil = exports.ObjectUtil = /*#__PURE__*/function () {
|
|
24
|
+
function ObjectUtil() {
|
|
25
|
+
_classCallCheck(this, ObjectUtil);
|
|
26
|
+
}
|
|
27
|
+
return _createClass(ObjectUtil, [{
|
|
28
|
+
key: "deepClone",
|
|
29
|
+
value:
|
|
30
|
+
/**
|
|
31
|
+
* Deep clone object. Returned object will have no references to the object passed through params
|
|
32
|
+
* @template T
|
|
33
|
+
* @param {T} objectToClone
|
|
34
|
+
* @return {T}
|
|
35
|
+
*/
|
|
36
|
+
function deepClone(objectToClone) {
|
|
37
|
+
return (0, _lodash["default"])(objectToClone);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Pick only properties from the property list. It is only allowed to pick properties from the first level
|
|
42
|
+
* @template T
|
|
43
|
+
* @template L
|
|
44
|
+
* @param {T} obj
|
|
45
|
+
* @param {L[]} keyList
|
|
46
|
+
* @return {Pick<T, L>}
|
|
47
|
+
*/
|
|
48
|
+
}, {
|
|
49
|
+
key: "pickByList",
|
|
50
|
+
value: function pickByList(obj, keyList) {
|
|
51
|
+
return keyList.reduce(function (pickedObj, key) {
|
|
52
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
53
|
+
pickedObj[key] = obj[key];
|
|
54
|
+
}
|
|
55
|
+
return pickedObj;
|
|
56
|
+
}, {}); // eslint-disable-line @typescript-eslint/prefer-reduce-type-parameter
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Pick objects properties using keys from the second object.
|
|
61
|
+
* @template T
|
|
62
|
+
* @template L
|
|
63
|
+
* @param {T} obj
|
|
64
|
+
* @param {Partial<T>} objWithPickKeys
|
|
65
|
+
* @return {Pick<T, L>}
|
|
66
|
+
*/
|
|
67
|
+
}, {
|
|
68
|
+
key: "pickByObjectKeys",
|
|
69
|
+
value: function pickByObjectKeys(obj, objWithPickKeys) {
|
|
70
|
+
var keys = Object.keys(objWithPickKeys);
|
|
71
|
+
return this.pickByList(obj, keys);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* This function will do stringify deeper that JSON.stringify.
|
|
76
|
+
* @param {any} entity - entity thant needs to be stringify
|
|
77
|
+
* @param {object} [options] - available options
|
|
78
|
+
* @param {boolean} [options.isSortable=false] - if object property should be sorted
|
|
79
|
+
* @param {boolean} [options.isPrettyPrinted=false] - if object and array properties should be printed in a new row
|
|
80
|
+
* @param {number} [options.prettyPrintCompactLevel=0] - if pretty print is on define the level of deepest children that are not
|
|
81
|
+
* going to be pretty. It doesn't matter if the siblings doesn't have the same depth.
|
|
82
|
+
* @return {string} - strung result
|
|
83
|
+
* @example
|
|
84
|
+
* console.log(new ObjectUtil().deepStringify(null)) // 'null'
|
|
85
|
+
* console.log(new ObjectUtil().deepStringify(undefined)) // 'undefined'
|
|
86
|
+
* console.log(new ObjectUtil().deepStringify({ a: 1 })) // '{\n\ta: 1\n}'
|
|
87
|
+
* // `{
|
|
88
|
+
* // a:1
|
|
89
|
+
* // }`
|
|
90
|
+
* console.log(new ObjectUtil().deepStringify({ b: 1, a: 2 }, {isSorted:true, compact: true})) // { a: 2, b: 1 }
|
|
91
|
+
*/
|
|
92
|
+
}, {
|
|
93
|
+
key: "deepStringify",
|
|
94
|
+
value: function deepStringify(
|
|
95
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
96
|
+
entity, options) {
|
|
97
|
+
var _ref = options !== null && options !== void 0 ? options : {},
|
|
98
|
+
_ref$isSorted = _ref.isSorted,
|
|
99
|
+
isSorted = _ref$isSorted === void 0 ? false : _ref$isSorted,
|
|
100
|
+
_ref$isPrettyPrinted = _ref.isPrettyPrinted,
|
|
101
|
+
isPrettyPrinted = _ref$isPrettyPrinted === void 0 ? false : _ref$isPrettyPrinted,
|
|
102
|
+
_ref$prettyPrintCompa = _ref.prettyPrintCompactLevel,
|
|
103
|
+
prettyPrintCompactLevel = _ref$prettyPrintCompa === void 0 ? 0 : _ref$prettyPrintCompa;
|
|
104
|
+
var compact = this._deepStringifyCompact({
|
|
105
|
+
isPrettyPrinted: isPrettyPrinted,
|
|
106
|
+
prettyPrintCompactLevel: prettyPrintCompactLevel
|
|
107
|
+
});
|
|
108
|
+
return _util["default"].inspect(entity, {
|
|
109
|
+
breakLength: Infinity,
|
|
110
|
+
compact: compact,
|
|
111
|
+
depth: Infinity,
|
|
112
|
+
maxArrayLength: Infinity,
|
|
113
|
+
maxStringLength: Infinity,
|
|
114
|
+
sorted: isSorted
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}, {
|
|
118
|
+
key: "_deepStringifyCompact",
|
|
119
|
+
value: function _deepStringifyCompact(params) {
|
|
120
|
+
var isPrettyPrinted = params.isPrettyPrinted,
|
|
121
|
+
prettyPrintCompactLevel = params.prettyPrintCompactLevel;
|
|
122
|
+
if (!isPrettyPrinted) {
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
return prettyPrintCompactLevel;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* We are converting objects to string (or null or undefined) and comparing if the result is equal
|
|
130
|
+
* @param a
|
|
131
|
+
* @param b
|
|
132
|
+
* @return {boolean}
|
|
133
|
+
*/
|
|
134
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
135
|
+
}, {
|
|
136
|
+
key: "deepEqual",
|
|
137
|
+
value: function deepEqual(a, b) {
|
|
138
|
+
return this.deepStringify(a, {
|
|
139
|
+
isSorted: true
|
|
140
|
+
}) === this.deepStringify(b, {
|
|
141
|
+
isSorted: true
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* This function is going to convert any null to undefined in the object that is passed to it.
|
|
147
|
+
* @template T
|
|
148
|
+
* @param {T} objectWithNulls
|
|
149
|
+
* @return {T}
|
|
150
|
+
* @example
|
|
151
|
+
* console.log(new ObjectUtil().deepNullToUndefined({ a: null, b: { c: null } })) // { a: undefined, b: { c: undefined } }
|
|
152
|
+
*/
|
|
153
|
+
}, {
|
|
154
|
+
key: "deepNullToUndefined",
|
|
155
|
+
value: function deepNullToUndefined(objectWithNulls) {
|
|
156
|
+
var _this = this;
|
|
157
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
158
|
+
return Object.entries(objectWithNulls).reduce(function (acc, cur) {
|
|
159
|
+
var _cur = _slicedToArray(cur, 2),
|
|
160
|
+
key = _cur[0],
|
|
161
|
+
value = _cur[1];
|
|
162
|
+
if (value === null) {
|
|
163
|
+
acc[key] = undefined;
|
|
164
|
+
} else if (_typeof(value) === 'object' && !(value instanceof Date)) {
|
|
165
|
+
acc[key] = _this.deepNullToUndefined(value);
|
|
166
|
+
} else {
|
|
167
|
+
acc[key] = value;
|
|
168
|
+
}
|
|
169
|
+
return acc;
|
|
170
|
+
}, {});
|
|
171
|
+
}
|
|
172
|
+
}]);
|
|
173
|
+
}();
|
package/lib/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type": "commonjs"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare const regexUtil: {
|
|
2
|
+
/**
|
|
3
|
+
* This is a UUID regex expression. This is usually used in express router to constrict the values passed as a path parameter (if you are using UUID as your identifier).
|
|
4
|
+
* @return {string}
|
|
5
|
+
* @example
|
|
6
|
+
* const { uuid } = regexUtil
|
|
7
|
+
* router.route(`/users/:userId(${uuid})`).get(getUsersById)
|
|
8
|
+
* //...
|
|
9
|
+
*/
|
|
10
|
+
uuid: "\\b[0-9a-f]{8}\\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\\b[0-9a-f]{12}\\b";
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=regex-util.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"regex-util.d.ts","sourceRoot":"","sources":["../src/regex-util.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,SAAS;IACrB;;;;;;;OAOG;;CAEH,CAAA"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.regexUtil = void 0;
|
|
7
|
+
var regexUtil = exports.regexUtil = {
|
|
8
|
+
/**
|
|
9
|
+
* This is a UUID regex expression. This is usually used in express router to constrict the values passed as a path parameter (if you are using UUID as your identifier).
|
|
10
|
+
* @return {string}
|
|
11
|
+
* @example
|
|
12
|
+
* const { uuid } = regexUtil
|
|
13
|
+
* router.route(`/users/:userId(${uuid})`).get(getUsersById)
|
|
14
|
+
* //...
|
|
15
|
+
*/
|
|
16
|
+
uuid: "\\b[0-9a-f]{8}\\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\\b[0-9a-f]{12}\\b"
|
|
17
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export type AnyFunctionPromiseNoParams<T> = () => Promise<T>;
|
|
2
|
+
/**
|
|
3
|
+
* SingleThresholdPromise returns a single promise, and subsequent calls made before the promise resolves will return the same promise.
|
|
4
|
+
* @example
|
|
5
|
+
* export const refreshTokenSingleThreshold = new SingleThresholdPromise(async () => {
|
|
6
|
+
* const oldRefreshToken = await refreshTokenService.get()
|
|
7
|
+
* const { accessToken, refreshToken } = await authService.refreshToken({
|
|
8
|
+
* refreshToken: oldRefreshToken,
|
|
9
|
+
* })
|
|
10
|
+
* return { accessToken, refreshToken }
|
|
11
|
+
* })
|
|
12
|
+
*
|
|
13
|
+
* export const authService = {
|
|
14
|
+
* refreshToken: async (): Promise<{ accessToken: string; refreshToken:string }> => {
|
|
15
|
+
* return refreshTokenSingleThreshold.promise()
|
|
16
|
+
* }
|
|
17
|
+
* }
|
|
18
|
+
*/
|
|
19
|
+
export declare class SingleThresholdPromise<T> {
|
|
20
|
+
protected _cache: {
|
|
21
|
+
promises?: {
|
|
22
|
+
resolve: (value: T | PromiseLike<T>) => void;
|
|
23
|
+
reject: (reason?: any) => void;
|
|
24
|
+
}[];
|
|
25
|
+
};
|
|
26
|
+
protected _factoryFn: AnyFunctionPromiseNoParams<T>;
|
|
27
|
+
constructor(factoryFn: AnyFunctionPromiseNoParams<T>);
|
|
28
|
+
protected _rejectPromises(): void;
|
|
29
|
+
promise(): Promise<T>;
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=single-threshold-promise.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"single-threshold-promise.d.ts","sourceRoot":"","sources":["../src/single-threshold-promise.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,0BAA0B,CAAC,CAAC,IAAI,MAAM,OAAO,CAAC,CAAC,CAAC,CAAA;AAE5D;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,sBAAsB,CAAC,CAAC;IACpC,SAAS,CAAC,MAAM,EAAE;QAEjB,QAAQ,CAAC,EAAE;YAAE,OAAO,EAAE,CAAC,KAAK,EAAE,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC;YAAC,MAAM,EAAE,CAAC,MAAM,CAAC,EAAE,GAAG,KAAK,IAAI,CAAA;SAAE,EAAE,CAAA;KAC7F,CAAK;IAEN,SAAS,CAAC,UAAU,EAAE,0BAA0B,CAAC,CAAC,CAAC,CAAA;gBAEvC,SAAS,EAAE,0BAA0B,CAAC,CAAC,CAAC;IAIpD,SAAS,CAAC,eAAe,IAAI,IAAI;IAO3B,OAAO,IAAI,OAAO,CAAC,CAAC,CAAC;CAkB3B"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.SingleThresholdPromise = void 0;
|
|
7
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
8
|
+
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator["return"] && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, "catch": function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
|
|
9
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
10
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
11
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
12
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
13
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
14
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
15
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
16
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
17
|
+
/**
|
|
18
|
+
* SingleThresholdPromise returns a single promise, and subsequent calls made before the promise resolves will return the same promise.
|
|
19
|
+
* @example
|
|
20
|
+
* export const refreshTokenSingleThreshold = new SingleThresholdPromise(async () => {
|
|
21
|
+
* const oldRefreshToken = await refreshTokenService.get()
|
|
22
|
+
* const { accessToken, refreshToken } = await authService.refreshToken({
|
|
23
|
+
* refreshToken: oldRefreshToken,
|
|
24
|
+
* })
|
|
25
|
+
* return { accessToken, refreshToken }
|
|
26
|
+
* })
|
|
27
|
+
*
|
|
28
|
+
* export const authService = {
|
|
29
|
+
* refreshToken: async (): Promise<{ accessToken: string; refreshToken:string }> => {
|
|
30
|
+
* return refreshTokenSingleThreshold.promise()
|
|
31
|
+
* }
|
|
32
|
+
* }
|
|
33
|
+
*/
|
|
34
|
+
var SingleThresholdPromise = exports.SingleThresholdPromise = /*#__PURE__*/function () {
|
|
35
|
+
function SingleThresholdPromise(factoryFn) {
|
|
36
|
+
_classCallCheck(this, SingleThresholdPromise);
|
|
37
|
+
_defineProperty(this, "_cache", {});
|
|
38
|
+
this._factoryFn = factoryFn;
|
|
39
|
+
}
|
|
40
|
+
return _createClass(SingleThresholdPromise, [{
|
|
41
|
+
key: "_rejectPromises",
|
|
42
|
+
value: function _rejectPromises() {
|
|
43
|
+
if (this._cache.promises) {
|
|
44
|
+
this._cache.promises.forEach(function (promise) {
|
|
45
|
+
return promise.reject(new Error('Cache was cleaned'));
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
delete this._cache.promises;
|
|
49
|
+
}
|
|
50
|
+
}, {
|
|
51
|
+
key: "promise",
|
|
52
|
+
value: function () {
|
|
53
|
+
var _promise = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
54
|
+
var _this = this;
|
|
55
|
+
var result;
|
|
56
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
57
|
+
while (1) switch (_context.prev = _context.next) {
|
|
58
|
+
case 0:
|
|
59
|
+
if (!('promises' in this._cache)) {
|
|
60
|
+
_context.next = 2;
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
return _context.abrupt("return", new Promise(function (resolve, reject) {
|
|
64
|
+
_this._cache.promises.push({
|
|
65
|
+
reject: reject,
|
|
66
|
+
resolve: resolve
|
|
67
|
+
});
|
|
68
|
+
}));
|
|
69
|
+
case 2:
|
|
70
|
+
this._cache.promises = [];
|
|
71
|
+
_context.next = 5;
|
|
72
|
+
return this._factoryFn()["catch"](function (err) {
|
|
73
|
+
_this._rejectPromises();
|
|
74
|
+
throw err;
|
|
75
|
+
});
|
|
76
|
+
case 5:
|
|
77
|
+
result = _context.sent;
|
|
78
|
+
this._cache.promises.forEach(function (promise) {
|
|
79
|
+
return promise.resolve(result);
|
|
80
|
+
});
|
|
81
|
+
delete this._cache.promises;
|
|
82
|
+
return _context.abrupt("return", result);
|
|
83
|
+
case 9:
|
|
84
|
+
case "end":
|
|
85
|
+
return _context.stop();
|
|
86
|
+
}
|
|
87
|
+
}, _callee, this);
|
|
88
|
+
}));
|
|
89
|
+
function promise() {
|
|
90
|
+
return _promise.apply(this, arguments);
|
|
91
|
+
}
|
|
92
|
+
return promise;
|
|
93
|
+
}()
|
|
94
|
+
}]);
|
|
95
|
+
}();
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export type AnyFunctionPromiseNoParams<T> = () => Promise<T>;
|
|
2
|
+
/**
|
|
3
|
+
* This is a singleton wrapper that is used to wrap around async function. We have additional functionality to clear the cache
|
|
4
|
+
* and reject any subscriptions to initial promise. And we can also check if there is anything i cache
|
|
5
|
+
* @example
|
|
6
|
+
* export const configSingleton = new SingletonAsync(async () => {
|
|
7
|
+
* await timeout(3000)
|
|
8
|
+
* return {
|
|
9
|
+
* env: process.env.NODE_ENV
|
|
10
|
+
* } as const
|
|
11
|
+
* })
|
|
12
|
+
*
|
|
13
|
+
* // using
|
|
14
|
+
* // cache value before we call promise
|
|
15
|
+
* console.log(configSingleton().cache()) // undefined
|
|
16
|
+
* console.log('NODE_ENV: ', await configSingleton().promise().env) // NODE_ENV: prod
|
|
17
|
+
* // cache value after we call promise
|
|
18
|
+
* console.log(configSingleton().cache()) // { env: 'prod' }
|
|
19
|
+
*/
|
|
20
|
+
export declare class SingletonAsync<T> {
|
|
21
|
+
protected _cache: {
|
|
22
|
+
singleton?: T;
|
|
23
|
+
promises?: {
|
|
24
|
+
resolve: (value: T | PromiseLike<T>) => void;
|
|
25
|
+
reject: (reason?: any) => void;
|
|
26
|
+
}[];
|
|
27
|
+
};
|
|
28
|
+
protected _factory: AnyFunctionPromiseNoParams<T>;
|
|
29
|
+
constructor(factory: AnyFunctionPromiseNoParams<T>);
|
|
30
|
+
/**
|
|
31
|
+
* Empty cached value and reject any subscribed promise that is waiting for the initial promise to be resolved.
|
|
32
|
+
*/
|
|
33
|
+
cleanCache(): void;
|
|
34
|
+
protected _rejectPromises(params: {
|
|
35
|
+
error: Error;
|
|
36
|
+
}): void;
|
|
37
|
+
/**
|
|
38
|
+
* Return singleton value in a promise. If there is no cached value then try to get it from factory.
|
|
39
|
+
* @template T
|
|
40
|
+
* @returns {Promise<T>}
|
|
41
|
+
*/
|
|
42
|
+
promise(): Promise<T>;
|
|
43
|
+
/**
|
|
44
|
+
* Return cached value, if there is no value cached return undefined.
|
|
45
|
+
* @template T
|
|
46
|
+
* @returns {T | undefined}
|
|
47
|
+
*/
|
|
48
|
+
cached(): T | undefined;
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=async.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async.d.ts","sourceRoot":"","sources":["../../src/singleton/async.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,0BAA0B,CAAC,CAAC,IAAI,MAAM,OAAO,CAAC,CAAC,CAAC,CAAA;AAE5D;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,cAAc,CAAC,CAAC;IAC5B,SAAS,CAAC,MAAM,EAAE;QACjB,SAAS,CAAC,EAAE,CAAC,CAAA;QAEb,QAAQ,CAAC,EAAE;YAAE,OAAO,EAAE,CAAC,KAAK,EAAE,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC;YAAC,MAAM,EAAE,CAAC,MAAM,CAAC,EAAE,GAAG,KAAK,IAAI,CAAA;SAAE,EAAE,CAAA;KAC7F,CAAK;IAEN,SAAS,CAAC,QAAQ,EAAE,0BAA0B,CAAC,CAAC,CAAC,CAAA;gBAErC,OAAO,EAAE,0BAA0B,CAAC,CAAC,CAAC;IAIlD;;OAEG;IACH,UAAU,IAAI,IAAI;IAKlB,SAAS,CAAC,eAAe,CAAC,MAAM,EAAE;QAAE,KAAK,EAAE,KAAK,CAAA;KAAE,GAAG,IAAI;IASzD;;;;OAIG;IACG,OAAO,IAAI,OAAO,CAAC,CAAC,CAAC;IAuB3B;;;;OAIG;IACH,MAAM,IAAI,CAAC,GAAG,SAAS;CAOvB"}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.SingletonAsync = void 0;
|
|
7
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
8
|
+
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator["return"] && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, "catch": function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
|
|
9
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
10
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
11
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
12
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
13
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
14
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
15
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
16
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
17
|
+
/**
|
|
18
|
+
* This is a singleton wrapper that is used to wrap around async function. We have additional functionality to clear the cache
|
|
19
|
+
* and reject any subscriptions to initial promise. And we can also check if there is anything i cache
|
|
20
|
+
* @example
|
|
21
|
+
* export const configSingleton = new SingletonAsync(async () => {
|
|
22
|
+
* await timeout(3000)
|
|
23
|
+
* return {
|
|
24
|
+
* env: process.env.NODE_ENV
|
|
25
|
+
* } as const
|
|
26
|
+
* })
|
|
27
|
+
*
|
|
28
|
+
* // using
|
|
29
|
+
* // cache value before we call promise
|
|
30
|
+
* console.log(configSingleton().cache()) // undefined
|
|
31
|
+
* console.log('NODE_ENV: ', await configSingleton().promise().env) // NODE_ENV: prod
|
|
32
|
+
* // cache value after we call promise
|
|
33
|
+
* console.log(configSingleton().cache()) // { env: 'prod' }
|
|
34
|
+
*/
|
|
35
|
+
var SingletonAsync = exports.SingletonAsync = /*#__PURE__*/function () {
|
|
36
|
+
function SingletonAsync(factory) {
|
|
37
|
+
_classCallCheck(this, SingletonAsync);
|
|
38
|
+
_defineProperty(this, "_cache", {});
|
|
39
|
+
this._factory = factory;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Empty cached value and reject any subscribed promise that is waiting for the initial promise to be resolved.
|
|
44
|
+
*/
|
|
45
|
+
return _createClass(SingletonAsync, [{
|
|
46
|
+
key: "cleanCache",
|
|
47
|
+
value: function cleanCache() {
|
|
48
|
+
delete this._cache.singleton;
|
|
49
|
+
this._rejectPromises({
|
|
50
|
+
error: new Error('Cache was cleaned')
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}, {
|
|
54
|
+
key: "_rejectPromises",
|
|
55
|
+
value: function _rejectPromises(params) {
|
|
56
|
+
var error = params.error;
|
|
57
|
+
if (this._cache.promises) {
|
|
58
|
+
this._cache.promises.forEach(function (promise) {
|
|
59
|
+
return promise.reject(error);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
delete this._cache.promises;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Return singleton value in a promise. If there is no cached value then try to get it from factory.
|
|
67
|
+
* @template T
|
|
68
|
+
* @returns {Promise<T>}
|
|
69
|
+
*/
|
|
70
|
+
}, {
|
|
71
|
+
key: "promise",
|
|
72
|
+
value: (function () {
|
|
73
|
+
var _promise = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
74
|
+
var _this = this;
|
|
75
|
+
var result;
|
|
76
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
77
|
+
while (1) switch (_context.prev = _context.next) {
|
|
78
|
+
case 0:
|
|
79
|
+
if (!('singleton' in this._cache)) {
|
|
80
|
+
_context.next = 2;
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
return _context.abrupt("return", this._cache.singleton);
|
|
84
|
+
case 2:
|
|
85
|
+
if (!('promises' in this._cache)) {
|
|
86
|
+
_context.next = 4;
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
return _context.abrupt("return", new Promise(function (resolve, reject) {
|
|
90
|
+
_this._cache.promises.push({
|
|
91
|
+
reject: reject,
|
|
92
|
+
resolve: resolve
|
|
93
|
+
});
|
|
94
|
+
}));
|
|
95
|
+
case 4:
|
|
96
|
+
this._cache.promises = [];
|
|
97
|
+
_context.next = 7;
|
|
98
|
+
return this._factory()["catch"](function (error) {
|
|
99
|
+
_this._rejectPromises({
|
|
100
|
+
error: error
|
|
101
|
+
});
|
|
102
|
+
throw error;
|
|
103
|
+
});
|
|
104
|
+
case 7:
|
|
105
|
+
result = _context.sent;
|
|
106
|
+
this._cache.singleton = result;
|
|
107
|
+
this._cache.promises.forEach(function (promise) {
|
|
108
|
+
return promise.resolve(result);
|
|
109
|
+
});
|
|
110
|
+
delete this._cache.promises;
|
|
111
|
+
return _context.abrupt("return", result);
|
|
112
|
+
case 12:
|
|
113
|
+
case "end":
|
|
114
|
+
return _context.stop();
|
|
115
|
+
}
|
|
116
|
+
}, _callee, this);
|
|
117
|
+
}));
|
|
118
|
+
function promise() {
|
|
119
|
+
return _promise.apply(this, arguments);
|
|
120
|
+
}
|
|
121
|
+
return promise;
|
|
122
|
+
}()
|
|
123
|
+
/**
|
|
124
|
+
* Return cached value, if there is no value cached return undefined.
|
|
125
|
+
* @template T
|
|
126
|
+
* @returns {T | undefined}
|
|
127
|
+
*/
|
|
128
|
+
)
|
|
129
|
+
}, {
|
|
130
|
+
key: "cached",
|
|
131
|
+
value: function cached() {
|
|
132
|
+
if ('singleton' in this._cache) {
|
|
133
|
+
return this._cache.singleton;
|
|
134
|
+
}
|
|
135
|
+
return undefined;
|
|
136
|
+
}
|
|
137
|
+
}]);
|
|
138
|
+
}();
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export type AnyFunctionNoParams<T> = () => T;
|
|
2
|
+
/**
|
|
3
|
+
* Singleton patter wrapper function
|
|
4
|
+
* @param {AnyFunctionNoParams<R>} factoryFn Factory function that is used to generate value that is going to be cached and return by
|
|
5
|
+
* singleton.
|
|
6
|
+
* @return {AnyFunctionNoParams<R>} Function result that returns cached value.
|
|
7
|
+
* @example
|
|
8
|
+
* export class SomeClass {
|
|
9
|
+
* constructor(protected _param: string){ }
|
|
10
|
+
* get param(): string {
|
|
11
|
+
* return this._param
|
|
12
|
+
* }
|
|
13
|
+
* }
|
|
14
|
+
* export const someClassSingleton = singletonPattern((): SomeClass => {
|
|
15
|
+
* return new SomeClass('some param value')
|
|
16
|
+
* })
|
|
17
|
+
*
|
|
18
|
+
* // using
|
|
19
|
+
* console.log('param: ', someClassSingleton().param) // param: some param value
|
|
20
|
+
*
|
|
21
|
+
* ///////////////////////////////////////////
|
|
22
|
+
* // Or we can use it with simple function //
|
|
23
|
+
* ///////////////////////////////////////////
|
|
24
|
+
* export const config = singletonPattern(() => {
|
|
25
|
+
* return {
|
|
26
|
+
* env: process.NODE_ENV,
|
|
27
|
+
* } as const
|
|
28
|
+
* })
|
|
29
|
+
*
|
|
30
|
+
* // using
|
|
31
|
+
* console.log('NODE_ENV: ', config().env) // NODE_ENV: prod
|
|
32
|
+
*/
|
|
33
|
+
export declare const singletonPattern: <R>(factoryFn: AnyFunctionNoParams<R>) => AnyFunctionNoParams<R>;
|
|
34
|
+
//# sourceMappingURL=pattern.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pattern.d.ts","sourceRoot":"","sources":["../../src/singleton/pattern.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,mBAAmB,CAAC,CAAC,IAAI,MAAM,CAAC,CAAA;AAE5C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,eAAO,MAAM,gBAAgB,iBAAkB,oBAAoB,CAAC,CAAC,KAAG,oBAAoB,CAAC,CAU5F,CAAA"}
|