@dynamic-labs/utils 0.18.10 → 0.18.11

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/CHANGELOG.md CHANGED
@@ -1,4 +1,13 @@
1
1
 
2
+ ### [0.18.11](https://github.com/dynamic-labs/DynamicAuth/compare/v0.18.10...v0.18.11) (2023-08-29)
3
+
4
+
5
+ ### Bug Fixes
6
+
7
+ * **bridge:** connect L2 wallet after updating network ([#3162](https://github.com/dynamic-labs/DynamicAuth/issues/3162)) ([6a90668](https://github.com/dynamic-labs/DynamicAuth/commit/6a906687d793fdfa03df87895f9c44b7a5e099dc))
8
+ * resend magic OTP on back click ([#3123](https://github.com/dynamic-labs/DynamicAuth/issues/3123)) ([#3150](https://github.com/dynamic-labs/DynamicAuth/issues/3150)) ([bcba905](https://github.com/dynamic-labs/DynamicAuth/commit/bcba905f22e796c122bb3bb156ec0e0f2a8fb3c6))
9
+ * **WagmiConnector:** sync wagmi with wallet connector events ([#3161](https://github.com/dynamic-labs/DynamicAuth/issues/3161)) ([7cd1da1](https://github.com/dynamic-labs/DynamicAuth/commit/7cd1da173ff7fca50cf2e92e0fad4dfbe651717c)), closes [#3149](https://github.com/dynamic-labs/DynamicAuth/issues/3149)
10
+
2
11
  ### [0.18.10](https://github.com/dynamic-labs/DynamicAuth/compare/v0.18.9...v0.18.10) (2023-08-25)
3
12
 
4
13
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dynamic-labs/utils",
3
- "version": "0.18.10",
3
+ "version": "0.18.11",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/dynamic-labs/DynamicAuth.git",
@@ -26,7 +26,8 @@
26
26
  "./package.json": "./package.json"
27
27
  },
28
28
  "dependencies": {
29
- "@dynamic-labs/types": "0.18.10"
29
+ "@dynamic-labs/logger": "0.18.11",
30
+ "@dynamic-labs/types": "0.18.11"
30
31
  },
31
32
  "peerDependencies": {}
32
33
  }
@@ -2,32 +2,134 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ var logger = require('../logger/logger.cjs');
6
+
7
+ /* eslint-disable multiline-comment-style */
8
+ const defaultCancel = () => {
9
+ logger.logger.error("Tried to cancel CancellablePromise without ever assigning it's '_cancel' method");
10
+ };
11
+ /** An extension of Promise that allows you to explicitly cancel a promise with an external reference to it.
12
+ * Cancelling CancellablePromise causes it to reject with reason set to the parameter passed to the cancel method.
13
+ * When CancellablePromise rejects, it provides a boolean to indicate whether it was cancelled, alongside the reason.
14
+ *
15
+ * @example
16
+ * // Cancelling this promise
17
+ * const promise = new CancellablePromise(() => {}).onCancel(console.log)
18
+ * promise.cancel('User cancelled')
19
+ *
20
+ * // Setting up listeners for resolve, cancel and reject
21
+ * // (notice calling catch after onCancel prevents catch from being called by cancellation)
22
+ * new CancellablePromise(() => {})
23
+ * .then(() => console.log('Promise resolved'))
24
+ * .onCancel(() => console.log('Promise was cancelled'))
25
+ * .catch(() => console.log('Promise was rejected but NOT cancelled'))
26
+ *
27
+ * // Telling whether catch was cause of cancel or not
28
+ * new CancellablePromise(() => {})
29
+ * .then(() => console.log('Promise resolved'))
30
+ * .catch(({ reason, wasCancelled }) => console.log('Promise was rejected. Cancelled: ' + wasCancelled))
31
+ */
5
32
  class CancellablePromise extends Promise {
33
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
34
+ internalCancel(reason) {
35
+ defaultCancel();
36
+ }
6
37
  constructor(executor) {
38
+ let superReject = defaultCancel;
39
+ let superResolve = () => { };
7
40
  super((resolve, reject) => {
8
- executor((val) => {
9
- if (this.isCancelled)
10
- return;
11
- resolve(val);
12
- }, (err) => {
13
- if (this.isCancelled)
14
- return;
15
- reject(err);
16
- });
41
+ superReject = reject;
42
+ superResolve = resolve;
43
+ });
44
+ this.internalIsCancelled = false;
45
+ this.isSettled = false;
46
+ executor((value) => {
47
+ if (!this.isCancelled && !this.isSettled)
48
+ superResolve(value);
49
+ this.isSettled = true;
50
+ }, (reason) => {
51
+ if (!this.isCancelled && !this.isSettled)
52
+ superReject(reason);
53
+ this.isSettled = true;
17
54
  });
18
- this._controller = new AbortController();
55
+ this.internalCancel = superReject;
19
56
  }
57
+ /** Whether this CancellablePromise was cancelled. */
20
58
  get isCancelled() {
21
- return this._controller.signal.aborted;
59
+ return this.internalIsCancelled;
22
60
  }
23
- cancel() {
24
- this._controller.abort();
61
+ /** Cancels the promise. This causes the promise to reject with { wasCancelled: true, reason: reason }
62
+ * where the reason is the provided argument.
63
+ * @returns The cancelled promise.
64
+ */
65
+ cancel(reason) {
66
+ if (this.isSettled)
67
+ return this;
68
+ this.internalIsCancelled = true;
69
+ this.internalCancel(reason);
70
+ this.isSettled = true;
71
+ return this;
25
72
  }
26
- static fromPromise(promise) {
27
- return new CancellablePromise((resolve, reject) => {
28
- promise.then(resolve).catch(reject);
73
+ internalOnCancel(listener, options) {
74
+ const newPromise = new CancellablePromise((resolve, reject) => {
75
+ this.internalThen(resolve, ({ wasCancelled, reason }) => {
76
+ if (wasCancelled)
77
+ resolve(listener(reason));
78
+ else
79
+ reject(reason);
80
+ }, { ignoreOnCancel: true });
81
+ });
82
+ /** Tie the new promise's cancel to this promise's cancel */
83
+ if (!(options === null || options === void 0 ? void 0 : options.ignoreOnCancel)) {
84
+ newPromise.internalOnCancel((reason) => {
85
+ this.cancel(reason);
86
+ }, { ignoreOnCancel: true });
87
+ }
88
+ return newPromise;
89
+ }
90
+ /** Allows reacting to this CancellablePromise being cancelled */
91
+ onCancel(listener) {
92
+ return this.internalOnCancel(listener);
93
+ }
94
+ internalThen(onFulfilled, onRejected, options) {
95
+ /** Create a catch handler that is undefined if the received callback is undefined */
96
+ const catchHandler = onRejected
97
+ ? (reason) => onRejected({ reason, wasCancelled: this.isCancelled })
98
+ : onRejected;
99
+ // /** Create the CancellablePromise we will returned, associated to the then promise */
100
+ const newPromise = CancellablePromise.fromPromise(super.then(onFulfilled, catchHandler));
101
+ /** Tie the new promise's cancel to this promise's cancel */
102
+ if (!(options === null || options === void 0 ? void 0 : options.ignoreOnCancel)) {
103
+ newPromise.internalOnCancel((reason) => {
104
+ this.cancel(reason);
105
+ }, { ignoreOnCancel: true });
106
+ }
107
+ return newPromise;
108
+ }
109
+ then(onFulfilled, onRejected) {
110
+ return this.internalThen(onFulfilled, onRejected);
111
+ }
112
+ catch(onRejected) {
113
+ return this.then(undefined, onRejected);
114
+ }
115
+ finally(onFinally) {
116
+ return this.then((value) => {
117
+ onFinally === null || onFinally === void 0 ? void 0 : onFinally();
118
+ return value;
119
+ }, (reason) => {
120
+ onFinally === null || onFinally === void 0 ? void 0 : onFinally();
121
+ throw reason;
29
122
  });
30
123
  }
124
+ /** Generates a CancellablePromise from a Promise. If a CancellablePromise is passed, it's returned unscathed.
125
+ * WARNING: unless you attach a method to reject the original Promise with the "onCancel" param.
126
+ * cancelling the resulting CancellablePromise does NOT affect the original Promise. */
127
+ static fromPromise(promise) {
128
+ if ('internalIsCancelled' in promise)
129
+ return promise;
130
+ const newPromise = new CancellablePromise((resolve, reject) => promise.then(resolve, reject));
131
+ return newPromise;
132
+ }
31
133
  }
32
134
 
33
135
  exports.CancellablePromise = CancellablePromise;
@@ -1,9 +1,54 @@
1
1
  type Executor<T> = (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: unknown) => void) => void;
2
+ type CancelListener<TResult = never> = (reason?: unknown) => TResult;
3
+ /** An extension of Promise that allows you to explicitly cancel a promise with an external reference to it.
4
+ * Cancelling CancellablePromise causes it to reject with reason set to the parameter passed to the cancel method.
5
+ * When CancellablePromise rejects, it provides a boolean to indicate whether it was cancelled, alongside the reason.
6
+ *
7
+ * @example
8
+ * // Cancelling this promise
9
+ * const promise = new CancellablePromise(() => {}).onCancel(console.log)
10
+ * promise.cancel('User cancelled')
11
+ *
12
+ * // Setting up listeners for resolve, cancel and reject
13
+ * // (notice calling catch after onCancel prevents catch from being called by cancellation)
14
+ * new CancellablePromise(() => {})
15
+ * .then(() => console.log('Promise resolved'))
16
+ * .onCancel(() => console.log('Promise was cancelled'))
17
+ * .catch(() => console.log('Promise was rejected but NOT cancelled'))
18
+ *
19
+ * // Telling whether catch was cause of cancel or not
20
+ * new CancellablePromise(() => {})
21
+ * .then(() => console.log('Promise resolved'))
22
+ * .catch(({ reason, wasCancelled }) => console.log('Promise was rejected. Cancelled: ' + wasCancelled))
23
+ */
2
24
  export declare class CancellablePromise<T> extends Promise<T> {
3
- _controller: AbortController;
25
+ private internalIsCancelled;
26
+ private isSettled;
27
+ private internalCancel;
4
28
  constructor(executor: Executor<T>);
29
+ /** Whether this CancellablePromise was cancelled. */
5
30
  get isCancelled(): boolean;
6
- cancel(): void;
7
- static fromPromise<T>(promise: Promise<T>): CancellablePromise<T>;
31
+ /** Cancels the promise. This causes the promise to reject with { wasCancelled: true, reason: reason }
32
+ * where the reason is the provided argument.
33
+ * @returns The cancelled promise.
34
+ */
35
+ cancel(reason?: unknown): this;
36
+ private internalOnCancel;
37
+ /** Allows reacting to this CancellablePromise being cancelled */
38
+ onCancel<TResult = never>(listener: CancelListener<TResult>): CancellablePromise<T | TResult>;
39
+ private internalThen;
40
+ then<TResult1 = T, TResult2 = never>(onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null, onRejected?: ((reason: {
41
+ reason: unknown;
42
+ wasCancelled: boolean;
43
+ }) => TResult2 | PromiseLike<TResult2>) | null): CancellablePromise<TResult1 | TResult2>;
44
+ catch<TResult = never>(onRejected?: ((reason: {
45
+ reason: unknown;
46
+ wasCancelled: boolean;
47
+ }) => TResult | PromiseLike<TResult>) | null): CancellablePromise<T | TResult>;
48
+ finally(onFinally?: (() => void) | null): CancellablePromise<T>;
49
+ /** Generates a CancellablePromise from a Promise. If a CancellablePromise is passed, it's returned unscathed.
50
+ * WARNING: unless you attach a method to reject the original Promise with the "onCancel" param.
51
+ * cancelling the resulting CancellablePromise does NOT affect the original Promise. */
52
+ static fromPromise<T>(promise: Promise<T> | CancellablePromise<T>): CancellablePromise<T>;
8
53
  }
9
54
  export {};
@@ -1,29 +1,131 @@
1
+ import { logger } from '../logger/logger.js';
2
+
3
+ /* eslint-disable multiline-comment-style */
4
+ const defaultCancel = () => {
5
+ logger.error("Tried to cancel CancellablePromise without ever assigning it's '_cancel' method");
6
+ };
7
+ /** An extension of Promise that allows you to explicitly cancel a promise with an external reference to it.
8
+ * Cancelling CancellablePromise causes it to reject with reason set to the parameter passed to the cancel method.
9
+ * When CancellablePromise rejects, it provides a boolean to indicate whether it was cancelled, alongside the reason.
10
+ *
11
+ * @example
12
+ * // Cancelling this promise
13
+ * const promise = new CancellablePromise(() => {}).onCancel(console.log)
14
+ * promise.cancel('User cancelled')
15
+ *
16
+ * // Setting up listeners for resolve, cancel and reject
17
+ * // (notice calling catch after onCancel prevents catch from being called by cancellation)
18
+ * new CancellablePromise(() => {})
19
+ * .then(() => console.log('Promise resolved'))
20
+ * .onCancel(() => console.log('Promise was cancelled'))
21
+ * .catch(() => console.log('Promise was rejected but NOT cancelled'))
22
+ *
23
+ * // Telling whether catch was cause of cancel or not
24
+ * new CancellablePromise(() => {})
25
+ * .then(() => console.log('Promise resolved'))
26
+ * .catch(({ reason, wasCancelled }) => console.log('Promise was rejected. Cancelled: ' + wasCancelled))
27
+ */
1
28
  class CancellablePromise extends Promise {
29
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
30
+ internalCancel(reason) {
31
+ defaultCancel();
32
+ }
2
33
  constructor(executor) {
34
+ let superReject = defaultCancel;
35
+ let superResolve = () => { };
3
36
  super((resolve, reject) => {
4
- executor((val) => {
5
- if (this.isCancelled)
6
- return;
7
- resolve(val);
8
- }, (err) => {
9
- if (this.isCancelled)
10
- return;
11
- reject(err);
12
- });
37
+ superReject = reject;
38
+ superResolve = resolve;
39
+ });
40
+ this.internalIsCancelled = false;
41
+ this.isSettled = false;
42
+ executor((value) => {
43
+ if (!this.isCancelled && !this.isSettled)
44
+ superResolve(value);
45
+ this.isSettled = true;
46
+ }, (reason) => {
47
+ if (!this.isCancelled && !this.isSettled)
48
+ superReject(reason);
49
+ this.isSettled = true;
13
50
  });
14
- this._controller = new AbortController();
51
+ this.internalCancel = superReject;
15
52
  }
53
+ /** Whether this CancellablePromise was cancelled. */
16
54
  get isCancelled() {
17
- return this._controller.signal.aborted;
55
+ return this.internalIsCancelled;
18
56
  }
19
- cancel() {
20
- this._controller.abort();
57
+ /** Cancels the promise. This causes the promise to reject with { wasCancelled: true, reason: reason }
58
+ * where the reason is the provided argument.
59
+ * @returns The cancelled promise.
60
+ */
61
+ cancel(reason) {
62
+ if (this.isSettled)
63
+ return this;
64
+ this.internalIsCancelled = true;
65
+ this.internalCancel(reason);
66
+ this.isSettled = true;
67
+ return this;
21
68
  }
22
- static fromPromise(promise) {
23
- return new CancellablePromise((resolve, reject) => {
24
- promise.then(resolve).catch(reject);
69
+ internalOnCancel(listener, options) {
70
+ const newPromise = new CancellablePromise((resolve, reject) => {
71
+ this.internalThen(resolve, ({ wasCancelled, reason }) => {
72
+ if (wasCancelled)
73
+ resolve(listener(reason));
74
+ else
75
+ reject(reason);
76
+ }, { ignoreOnCancel: true });
77
+ });
78
+ /** Tie the new promise's cancel to this promise's cancel */
79
+ if (!(options === null || options === void 0 ? void 0 : options.ignoreOnCancel)) {
80
+ newPromise.internalOnCancel((reason) => {
81
+ this.cancel(reason);
82
+ }, { ignoreOnCancel: true });
83
+ }
84
+ return newPromise;
85
+ }
86
+ /** Allows reacting to this CancellablePromise being cancelled */
87
+ onCancel(listener) {
88
+ return this.internalOnCancel(listener);
89
+ }
90
+ internalThen(onFulfilled, onRejected, options) {
91
+ /** Create a catch handler that is undefined if the received callback is undefined */
92
+ const catchHandler = onRejected
93
+ ? (reason) => onRejected({ reason, wasCancelled: this.isCancelled })
94
+ : onRejected;
95
+ // /** Create the CancellablePromise we will returned, associated to the then promise */
96
+ const newPromise = CancellablePromise.fromPromise(super.then(onFulfilled, catchHandler));
97
+ /** Tie the new promise's cancel to this promise's cancel */
98
+ if (!(options === null || options === void 0 ? void 0 : options.ignoreOnCancel)) {
99
+ newPromise.internalOnCancel((reason) => {
100
+ this.cancel(reason);
101
+ }, { ignoreOnCancel: true });
102
+ }
103
+ return newPromise;
104
+ }
105
+ then(onFulfilled, onRejected) {
106
+ return this.internalThen(onFulfilled, onRejected);
107
+ }
108
+ catch(onRejected) {
109
+ return this.then(undefined, onRejected);
110
+ }
111
+ finally(onFinally) {
112
+ return this.then((value) => {
113
+ onFinally === null || onFinally === void 0 ? void 0 : onFinally();
114
+ return value;
115
+ }, (reason) => {
116
+ onFinally === null || onFinally === void 0 ? void 0 : onFinally();
117
+ throw reason;
25
118
  });
26
119
  }
120
+ /** Generates a CancellablePromise from a Promise. If a CancellablePromise is passed, it's returned unscathed.
121
+ * WARNING: unless you attach a method to reject the original Promise with the "onCancel" param.
122
+ * cancelling the resulting CancellablePromise does NOT affect the original Promise. */
123
+ static fromPromise(promise) {
124
+ if ('internalIsCancelled' in promise)
125
+ return promise;
126
+ const newPromise = new CancellablePromise((resolve, reject) => promise.then(resolve, reject));
127
+ return newPromise;
128
+ }
27
129
  }
28
130
 
29
131
  export { CancellablePromise };
package/src/index.cjs CHANGED
@@ -24,6 +24,7 @@ var isFunction = require('./isFunction/isFunction.cjs');
24
24
  var isMobile = require('./isMobile.cjs');
25
25
  var localStorageAsync = require('./localStorageAsync.cjs');
26
26
  var bufferToBase64 = require('./bufferToBase64.cjs');
27
+ var last = require('./last.cjs');
27
28
 
28
29
 
29
30
 
@@ -57,3 +58,4 @@ exports.getItemAsync = localStorageAsync.getItemAsync;
57
58
  exports.removeItemAsync = localStorageAsync.removeItemAsync;
58
59
  exports.setItemAsync = localStorageAsync.setItemAsync;
59
60
  exports.bufferToBase64 = bufferToBase64.bufferToBase64;
61
+ exports.last = last.last;
package/src/index.d.ts CHANGED
@@ -6,3 +6,4 @@ export * from './isFunction';
6
6
  export * from './isMobile';
7
7
  export * from './localStorageAsync';
8
8
  export * from './bufferToBase64';
9
+ export * from './last';
package/src/index.js CHANGED
@@ -20,3 +20,4 @@ export { isFunction } from './isFunction/isFunction.js';
20
20
  export { isAndroid, isIOS, isIPad, isIPhone, isLegacySafari, isMobile, isSamsungBrowser } from './isMobile.js';
21
21
  export { getItemAsync, removeItemAsync, setItemAsync } from './localStorageAsync.js';
22
22
  export { bufferToBase64 } from './bufferToBase64.js';
23
+ export { last } from './last.js';
package/src/last.cjs ADDED
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ /**
6
+ * Retrieves the last item from an array.
7
+ *
8
+ * @template T - The type of the items in the array.
9
+ * @param {T[]} arr - The array from which to retrieve the last item.
10
+ * @returns {T | undefined} The last item in the array, or undefined if the array is empty.
11
+ *
12
+ * @example
13
+ * const numbers = [1, 2, 3, 4, 5];
14
+ * console.log(last(numbers)); // Outputs: 5
15
+ *
16
+ * const emptyArray: number[] = [];
17
+ * console.log(last(emptyArray)); // Outputs: undefined
18
+ */
19
+ const last = (arr) => arr.length > 0 ? arr[arr.length - 1] : undefined;
20
+
21
+ exports.last = last;
package/src/last.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Retrieves the last item from an array.
3
+ *
4
+ * @template T - The type of the items in the array.
5
+ * @param {T[]} arr - The array from which to retrieve the last item.
6
+ * @returns {T | undefined} The last item in the array, or undefined if the array is empty.
7
+ *
8
+ * @example
9
+ * const numbers = [1, 2, 3, 4, 5];
10
+ * console.log(last(numbers)); // Outputs: 5
11
+ *
12
+ * const emptyArray: number[] = [];
13
+ * console.log(last(emptyArray)); // Outputs: undefined
14
+ */
15
+ export declare const last: <T>(arr: T[]) => T | undefined;
package/src/last.js ADDED
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Retrieves the last item from an array.
3
+ *
4
+ * @template T - The type of the items in the array.
5
+ * @param {T[]} arr - The array from which to retrieve the last item.
6
+ * @returns {T | undefined} The last item in the array, or undefined if the array is empty.
7
+ *
8
+ * @example
9
+ * const numbers = [1, 2, 3, 4, 5];
10
+ * console.log(last(numbers)); // Outputs: 5
11
+ *
12
+ * const emptyArray: number[] = [];
13
+ * console.log(last(emptyArray)); // Outputs: undefined
14
+ */
15
+ const last = (arr) => arr.length > 0 ? arr[arr.length - 1] : undefined;
16
+
17
+ export { last };
@@ -0,0 +1 @@
1
+ export * from './logger';
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var logger$1 = require('@dynamic-labs/logger');
6
+
7
+ const logger = new logger$1.Logger('magic');
8
+
9
+ exports.logger = logger;
@@ -0,0 +1,2 @@
1
+ import { Logger } from '@dynamic-labs/logger';
2
+ export declare const logger: Logger;
@@ -0,0 +1,5 @@
1
+ import { Logger } from '@dynamic-labs/logger';
2
+
3
+ const logger = new Logger('magic');
4
+
5
+ export { logger };