@vertexvis/utils 0.12.0-canary.6 → 0.12.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.
@@ -9,6 +9,34 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
9
9
 
10
10
  var crypto__default = /*#__PURE__*/_interopDefaultLegacy(crypto);
11
11
 
12
+ /**
13
+ * Adds a listener to the given `target`, and returns a promise that
14
+ * resolves with the first event emitted of the given `type`.
15
+ *
16
+ * @param target The target to add an event listener to.
17
+ * @param type The event type to listen for.
18
+ * @param opts Options to pass to `addEventListener`.
19
+ * @returns A promise that resolves with the first event emitted of `type`.
20
+ */
21
+ function once(target, type, opts) {
22
+ return tslib.__awaiter(this, void 0, void 0, function () {
23
+ return tslib.__generator(this, function (_a) {
24
+ return [2 /*return*/, new Promise(function (resolve) {
25
+ function handler(event) {
26
+ target.removeEventListener(type, handler);
27
+ resolve(event);
28
+ }
29
+ target.addEventListener(type, handler, opts);
30
+ })];
31
+ });
32
+ });
33
+ }
34
+
35
+ var eventTargets = /*#__PURE__*/Object.freeze({
36
+ __proto__: null,
37
+ once: once
38
+ });
39
+
12
40
  function delay() {
13
41
  var args = [];
14
42
  for (var _i = 0; _i < arguments.length; _i++) {
@@ -41,28 +69,132 @@ function timeout() {
41
69
  for (var _i = 0; _i < arguments.length; _i++) {
42
70
  args[_i] = arguments[_i];
43
71
  }
44
- var ms = args[0];
45
- if (typeof ms === 'number') {
46
- var promise = args[1];
47
- var timeout_1 = new Promise(function (_, reject) {
48
- return setTimeout(function () { return reject(new Error("Promise timed out after " + ms + "ms")); }, ms);
72
+ return tslib.__awaiter(this, void 0, void 0, function () {
73
+ var ms, promise, timer_1, timeout_1, res;
74
+ return tslib.__generator(this, function (_a) {
75
+ switch (_a.label) {
76
+ case 0:
77
+ ms = args[0];
78
+ if (!(typeof ms === 'number')) return [3 /*break*/, 4];
79
+ promise = args[1];
80
+ timeout_1 = new Promise(function (_, reject) {
81
+ timer_1 = setTimeout(function () { return reject(new Error("Promise timed out after ".concat(ms, "ms"))); }, ms);
82
+ });
83
+ if (!(promise != null)) return [3 /*break*/, 2];
84
+ return [4 /*yield*/, Promise.race([promise, timeout_1])];
85
+ case 1:
86
+ res = _a.sent();
87
+ clearTimeout(timer_1);
88
+ return [2 /*return*/, res];
89
+ case 2: return [2 /*return*/, timeout_1];
90
+ case 3: return [3 /*break*/, 5];
91
+ case 4: return [2 /*return*/, Promise.reject('First argument to `timeout` must be a number')];
92
+ case 5: return [2 /*return*/];
93
+ }
49
94
  });
50
- if (promise != null) {
51
- return Promise.race([promise, timeout_1]);
52
- }
53
- else {
54
- return timeout_1;
95
+ });
96
+ }
97
+ /**
98
+ * Executes and reattempts execution of an asynchronous function if it throws an
99
+ * error. By default, this function will only retry once and reexecute
100
+ * immediately after the previous execution throws. You can configure the number
101
+ * of retry attempts and delays with the `maxRetries` and `delaysInMs` options.
102
+ *
103
+ * The `delaysInMs` is an array of delays in milliseconds for each retry
104
+ * attempt. If there are more retry attempts than delays, the last delay will be
105
+ * used.
106
+ *
107
+ * @param process The process to execute.
108
+ * @param opts Options to configure retry behavior.
109
+ * @returns A promise that resolves with a successful value, or the original
110
+ * rejected value if the process fails.
111
+ */
112
+ function retry(process, opts) {
113
+ if (opts === void 0) { opts = {}; }
114
+ return tslib.__awaiter(this, void 0, void 0, function () {
115
+ function execute(attempt, process, opts) {
116
+ return tslib.__awaiter(this, void 0, void 0, function () {
117
+ var _a, delaysInMs, _b, maxRetries, delayInMs, e_1;
118
+ return tslib.__generator(this, function (_c) {
119
+ switch (_c.label) {
120
+ case 0:
121
+ _a = opts.delaysInMs, delaysInMs = _a === void 0 ? [] : _a, _b = opts.maxRetries, maxRetries = _b === void 0 ? 1 : _b;
122
+ _c.label = 1;
123
+ case 1:
124
+ _c.trys.push([1, 4, , 8]);
125
+ delayInMs = attempt === 0 || delaysInMs.length === 0
126
+ ? 0
127
+ : delaysInMs[Math.min(attempt - 1, delaysInMs.length - 1)];
128
+ return [4 /*yield*/, delay(delayInMs)];
129
+ case 2:
130
+ _c.sent();
131
+ return [4 /*yield*/, process()];
132
+ case 3: return [2 /*return*/, _c.sent()];
133
+ case 4:
134
+ e_1 = _c.sent();
135
+ if (!(attempt < maxRetries)) return [3 /*break*/, 6];
136
+ return [4 /*yield*/, execute(attempt + 1, process, opts)];
137
+ case 5: return [2 /*return*/, _c.sent()];
138
+ case 6: throw e_1;
139
+ case 7: return [3 /*break*/, 8];
140
+ case 8: return [2 /*return*/];
141
+ }
142
+ });
143
+ });
55
144
  }
145
+ return tslib.__generator(this, function (_a) {
146
+ return [2 /*return*/, execute(0, process, opts)];
147
+ });
148
+ });
149
+ }
150
+ /**
151
+ * Returns a promise that either resolves with the result of `promise`, or a
152
+ * value that indicates the execution was aborted.
153
+ *
154
+ * **Note:** Because Promises in JS cannot be canceled, an abort signal will not
155
+ * cancel the execution of the promise.
156
+ *
157
+ * @param signal A signal that communicates the process should be aborted.
158
+ * @param promise A promise who's value will be returned if not aborted.
159
+ * @returns A value indicating if the process was aborted, or the value of
160
+ * `promise`.
161
+ */
162
+ function abort(signal, promise) {
163
+ return tslib.__awaiter(this, void 0, void 0, function () {
164
+ var controller, pendingAbort, result;
165
+ return tslib.__generator(this, function (_a) {
166
+ switch (_a.label) {
167
+ case 0:
168
+ controller = new AbortController();
169
+ pendingAbort = once(signal, 'abort', { signal: controller.signal });
170
+ return [4 /*yield*/, Promise.race([promise, pendingAbort])];
171
+ case 1:
172
+ result = _a.sent();
173
+ if (isAbortEvent(result)) {
174
+ return [2 /*return*/, { aborted: true }];
175
+ }
176
+ else {
177
+ controller.abort();
178
+ return [2 /*return*/, { aborted: false, result: result }];
179
+ }
180
+ }
181
+ });
182
+ });
183
+ }
184
+ function isAbortEvent(obj) {
185
+ if (obj instanceof Event) {
186
+ return obj.type === 'abort';
56
187
  }
57
- else {
58
- return Promise.reject('First argument to `timeout` must be a number');
59
- }
188
+ else
189
+ return false;
60
190
  }
61
191
 
62
192
  var async = /*#__PURE__*/Object.freeze({
63
193
  __proto__: null,
64
194
  delay: delay,
65
- timeout: timeout
195
+ timeout: timeout,
196
+ retry: retry,
197
+ abort: abort
66
198
  });
67
199
 
68
200
  /**
@@ -192,7 +324,7 @@ var isOpaque = function (color) {
192
324
  * `#`.
193
325
  */
194
326
  var toHexString = function (color) {
195
- return "#" + componentToHex(color.r) + componentToHex(color.g) + componentToHex(color.b);
327
+ return "#".concat(componentToHex(color.r)).concat(componentToHex(color.g)).concat(componentToHex(color.b));
196
328
  };
197
329
  var componentToHex = function (num) {
198
330
  var hex = num.toString(16);
@@ -287,8 +419,9 @@ var color = /*#__PURE__*/Object.freeze({
287
419
  var MapperValidationError = /** @class */ (function (_super) {
288
420
  tslib.__extends(MapperValidationError, _super);
289
421
  function MapperValidationError(errors) {
290
- var _this = _super.call(this, 'Validation error while mapping object.') || this;
422
+ var _this = _super.call(this, 'Validation error mapping object.') || this;
291
423
  _this.errors = errors;
424
+ Object.setPrototypeOf(_this, MapperValidationError.prototype);
292
425
  return _this;
293
426
  }
294
427
  return MapperValidationError;
@@ -304,7 +437,7 @@ function required(name) {
304
437
  return input;
305
438
  }
306
439
  else {
307
- return { errors: [name + " is required."] };
440
+ return { errors: ["".concat(name, " is required.")] };
308
441
  }
309
442
  };
310
443
  }
@@ -322,7 +455,7 @@ function requiredProp(prop) {
322
455
  return value;
323
456
  }
324
457
  else {
325
- return { errors: [prop + " is required"] };
458
+ return { errors: ["".concat(prop, " is required")] };
326
459
  }
327
460
  };
328
461
  }
@@ -2853,7 +2986,7 @@ var isEqual = function (a, b) {
2853
2986
  queryA === queryB);
2854
2987
  };
2855
2988
  var replacePath = function (path, uri) {
2856
- var pathWithForwardSlash = path[0] === '/' ? path : "/" + path;
2989
+ var pathWithForwardSlash = path[0] === '/' ? path : "/".concat(path);
2857
2990
  return tslib.__assign(tslib.__assign({}, uri), { path: pathWithForwardSlash });
2858
2991
  };
2859
2992
  var pathAsArray = function (uri) {
@@ -2929,17 +3062,17 @@ var queryAsMap = function (uri) {
2929
3062
  var toString = function (uri) {
2930
3063
  var result = '';
2931
3064
  if (uri.scheme != null && uri.scheme.length > 0) {
2932
- result = uri.scheme + ":";
3065
+ result = "".concat(uri.scheme, ":");
2933
3066
  }
2934
3067
  if (uri.authority != null && uri.authority.length > 0) {
2935
- result += "//" + uri.authority;
3068
+ result += "//".concat(uri.authority);
2936
3069
  }
2937
3070
  result += uri.path;
2938
3071
  if (uri.query != null && uri.query.length > 0) {
2939
- result += "?" + uri.query;
3072
+ result += "?".concat(uri.query);
2940
3073
  }
2941
3074
  if (uri.fragment != null && uri.fragment.length > 0) {
2942
- result += "#" + uri.fragment;
3075
+ result += "#".concat(uri.fragment);
2943
3076
  }
2944
3077
  return result;
2945
3078
  };
@@ -3124,10 +3257,50 @@ var EventDispatcher = /** @class */ (function () {
3124
3257
  function EventDispatcher() {
3125
3258
  this.listeners = [];
3126
3259
  }
3127
- EventDispatcher.prototype.on = function (listener) {
3260
+ EventDispatcher.prototype.on = function (listener, opts) {
3128
3261
  var _this = this;
3262
+ var _a;
3263
+ if (opts === void 0) { opts = {}; }
3129
3264
  this.listeners.push(listener);
3130
- return { dispose: function () { return _this.off(listener); } };
3265
+ var controller = new AbortController();
3266
+ controller.signal.addEventListener('abort', function () { return _this.off(listener); });
3267
+ (_a = opts.abort) === null || _a === void 0 ? void 0 : _a.addEventListener('abort', function () { return controller.abort(); });
3268
+ return { dispose: function () { return controller.abort(); } };
3269
+ };
3270
+ EventDispatcher.prototype.once = function (opts) {
3271
+ var _this = this;
3272
+ if (opts === void 0) { opts = {}; }
3273
+ return new Promise(function (resolve) {
3274
+ _this.on(function (event) { return resolve(event); }, opts);
3275
+ });
3276
+ };
3277
+ EventDispatcher.prototype.onceWhen = function (predicate, opts) {
3278
+ var _a;
3279
+ if (opts === void 0) { opts = {}; }
3280
+ return tslib.__awaiter(this, void 0, void 0, function () {
3281
+ var controller;
3282
+ var _this = this;
3283
+ return tslib.__generator(this, function (_b) {
3284
+ controller = new AbortController();
3285
+ (_a = opts.abort) === null || _a === void 0 ? void 0 : _a.addEventListener('abort', function () { return controller.abort(); });
3286
+ return [2 /*return*/, new Promise(function (resolve) {
3287
+ _this.when(predicate, function (event) {
3288
+ if (predicate(event)) {
3289
+ controller.abort();
3290
+ resolve(event);
3291
+ }
3292
+ }, tslib.__assign(tslib.__assign({}, opts), { abort: controller.signal }));
3293
+ })];
3294
+ });
3295
+ });
3296
+ };
3297
+ EventDispatcher.prototype.when = function (predicate, listener, opts) {
3298
+ if (opts === void 0) { opts = {}; }
3299
+ return this.on(function (event) {
3300
+ if (predicate(event)) {
3301
+ listener(event);
3302
+ }
3303
+ }, opts);
3131
3304
  };
3132
3305
  EventDispatcher.prototype.off = function (listener) {
3133
3306
  var index = this.listeners.indexOf(listener);
@@ -3145,6 +3318,7 @@ exports.Async = async;
3145
3318
  exports.BinaryReader = binaryReader;
3146
3319
  exports.Color = color;
3147
3320
  exports.EventDispatcher = EventDispatcher;
3321
+ exports.EventTargets = eventTargets;
3148
3322
  exports.Mapper = mapper;
3149
3323
  exports.Objects = objects;
3150
3324
  exports.Range = range;