@jsnw/common-utils 1.5.2 → 1.5.3
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/dist/index.cjs +14 -3
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +14 -3
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -103,6 +103,18 @@ function isObjectEmpty(obj) {
|
|
|
103
103
|
//#endregion
|
|
104
104
|
//#region src/createInFlightDeduper.ts
|
|
105
105
|
/**
|
|
106
|
+
* @template R
|
|
107
|
+
* @param {() => (Promise<R> | R)} fn
|
|
108
|
+
* @returns {Promise<Awaited<R>>}
|
|
109
|
+
*/
|
|
110
|
+
const promiseTry = (fn) => {
|
|
111
|
+
try {
|
|
112
|
+
return Promise.resolve(fn());
|
|
113
|
+
} catch (e) {
|
|
114
|
+
return Promise.reject(e);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
106
118
|
* Creates a deduplicator for in-flight promises or sync functions.
|
|
107
119
|
* Prevents duplicate executions for the same key while a promise is pending.
|
|
108
120
|
* Clears the entry after settlement.
|
|
@@ -113,15 +125,14 @@ function createInFlightDeduper() {
|
|
|
113
125
|
function use(key, fn) {
|
|
114
126
|
let promise = __promises.get(key);
|
|
115
127
|
if (promise) return promise;
|
|
116
|
-
promise =
|
|
128
|
+
promise = promiseTry(fn).finally(() => {
|
|
117
129
|
__promises.delete(key);
|
|
118
130
|
});
|
|
119
131
|
__promises.set(key, promise);
|
|
120
132
|
return promise;
|
|
121
133
|
}
|
|
122
134
|
function maybeIdempotent(p, fn) {
|
|
123
|
-
|
|
124
|
-
return Promise.resolve(fn());
|
|
135
|
+
return p.idempotencyId ? use(p.idempotencyId, fn) : promiseTry(fn);
|
|
125
136
|
}
|
|
126
137
|
return {
|
|
127
138
|
use,
|
package/dist/index.d.cts
CHANGED
|
@@ -71,8 +71,8 @@ type WithIdempotencyId = {
|
|
|
71
71
|
idempotencyId?: string;
|
|
72
72
|
};
|
|
73
73
|
type InFlightDeduper = {
|
|
74
|
-
use: <R
|
|
75
|
-
maybeIdempotent: <R
|
|
74
|
+
use: <R>(key: string, fn: () => R | Promise<R>) => Promise<Awaited<R>>;
|
|
75
|
+
maybeIdempotent: <R>(p: WithIdempotencyId, fn: () => R | Promise<R>) => Promise<Awaited<R>>;
|
|
76
76
|
};
|
|
77
77
|
/**
|
|
78
78
|
* Creates a deduplicator for in-flight promises or sync functions.
|
package/dist/index.d.mts
CHANGED
|
@@ -71,8 +71,8 @@ type WithIdempotencyId = {
|
|
|
71
71
|
idempotencyId?: string;
|
|
72
72
|
};
|
|
73
73
|
type InFlightDeduper = {
|
|
74
|
-
use: <R
|
|
75
|
-
maybeIdempotent: <R
|
|
74
|
+
use: <R>(key: string, fn: () => R | Promise<R>) => Promise<Awaited<R>>;
|
|
75
|
+
maybeIdempotent: <R>(p: WithIdempotencyId, fn: () => R | Promise<R>) => Promise<Awaited<R>>;
|
|
76
76
|
};
|
|
77
77
|
/**
|
|
78
78
|
* Creates a deduplicator for in-flight promises or sync functions.
|
package/dist/index.mjs
CHANGED
|
@@ -102,6 +102,18 @@ function isObjectEmpty(obj) {
|
|
|
102
102
|
//#endregion
|
|
103
103
|
//#region src/createInFlightDeduper.ts
|
|
104
104
|
/**
|
|
105
|
+
* @template R
|
|
106
|
+
* @param {() => (Promise<R> | R)} fn
|
|
107
|
+
* @returns {Promise<Awaited<R>>}
|
|
108
|
+
*/
|
|
109
|
+
const promiseTry = (fn) => {
|
|
110
|
+
try {
|
|
111
|
+
return Promise.resolve(fn());
|
|
112
|
+
} catch (e) {
|
|
113
|
+
return Promise.reject(e);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
105
117
|
* Creates a deduplicator for in-flight promises or sync functions.
|
|
106
118
|
* Prevents duplicate executions for the same key while a promise is pending.
|
|
107
119
|
* Clears the entry after settlement.
|
|
@@ -112,15 +124,14 @@ function createInFlightDeduper() {
|
|
|
112
124
|
function use(key, fn) {
|
|
113
125
|
let promise = __promises.get(key);
|
|
114
126
|
if (promise) return promise;
|
|
115
|
-
promise =
|
|
127
|
+
promise = promiseTry(fn).finally(() => {
|
|
116
128
|
__promises.delete(key);
|
|
117
129
|
});
|
|
118
130
|
__promises.set(key, promise);
|
|
119
131
|
return promise;
|
|
120
132
|
}
|
|
121
133
|
function maybeIdempotent(p, fn) {
|
|
122
|
-
|
|
123
|
-
return Promise.resolve(fn());
|
|
134
|
+
return p.idempotencyId ? use(p.idempotencyId, fn) : promiseTry(fn);
|
|
124
135
|
}
|
|
125
136
|
return {
|
|
126
137
|
use,
|