@jsnw/common-utils 1.5.1 → 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 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,13 +125,19 @@ function createInFlightDeduper() {
113
125
  function use(key, fn) {
114
126
  let promise = __promises.get(key);
115
127
  if (promise) return promise;
116
- promise = Promise.resolve(fn()).finally(() => {
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
- return { use };
134
+ function maybeIdempotent(p, fn) {
135
+ return p.idempotencyId ? use(p.idempotencyId, fn) : promiseTry(fn);
136
+ }
137
+ return {
138
+ use,
139
+ maybeIdempotent
140
+ };
123
141
  }
124
142
  //#endregion
125
143
  //#region src/resultify.ts
package/dist/index.d.cts CHANGED
@@ -67,8 +67,12 @@ declare const wait: (wait_ms: number, signal?: AbortSignal) => Promise<void>;
67
67
  declare function isObjectEmpty(obj: object): boolean;
68
68
  //#endregion
69
69
  //#region src/createInFlightDeduper.d.ts
70
+ type WithIdempotencyId = {
71
+ idempotencyId?: string;
72
+ };
70
73
  type InFlightDeduper = {
71
- use: <R extends any>(key: string, fn: () => R | Promise<R>) => Promise<Awaited<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>>;
72
76
  };
73
77
  /**
74
78
  * Creates a deduplicator for in-flight promises or sync functions.
@@ -190,4 +194,4 @@ declare const promisify: <F extends (...args: any[]) => any>(fn: F, ...args: Par
190
194
  */
191
195
  declare const clamp: (num: number, min: number, max: number) => number;
192
196
  //#endregion
193
- export { DeferredPromise, ErrorResult, InFlightDeduper, Monitor, MonitorMark, MonitorResult, PerfmonError, PerfmonResult, WithPerfmonReturn, chunk, chunkStr, clamp, createInFlightDeduper, defer, ignorePromise, isAbortError, isObjectEmpty, noop, promisify, resultify, trySettlePromises, wait, withPerfmon };
197
+ export { DeferredPromise, ErrorResult, InFlightDeduper, Monitor, MonitorMark, MonitorResult, PerfmonError, PerfmonResult, WithIdempotencyId, WithPerfmonReturn, chunk, chunkStr, clamp, createInFlightDeduper, defer, ignorePromise, isAbortError, isObjectEmpty, noop, promisify, resultify, trySettlePromises, wait, withPerfmon };
package/dist/index.d.mts CHANGED
@@ -67,8 +67,12 @@ declare const wait: (wait_ms: number, signal?: AbortSignal) => Promise<void>;
67
67
  declare function isObjectEmpty(obj: object): boolean;
68
68
  //#endregion
69
69
  //#region src/createInFlightDeduper.d.ts
70
+ type WithIdempotencyId = {
71
+ idempotencyId?: string;
72
+ };
70
73
  type InFlightDeduper = {
71
- use: <R extends any>(key: string, fn: () => R | Promise<R>) => Promise<Awaited<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>>;
72
76
  };
73
77
  /**
74
78
  * Creates a deduplicator for in-flight promises or sync functions.
@@ -190,4 +194,4 @@ declare const promisify: <F extends (...args: any[]) => any>(fn: F, ...args: Par
190
194
  */
191
195
  declare const clamp: (num: number, min: number, max: number) => number;
192
196
  //#endregion
193
- export { DeferredPromise, ErrorResult, InFlightDeduper, Monitor, MonitorMark, MonitorResult, PerfmonError, PerfmonResult, WithPerfmonReturn, chunk, chunkStr, clamp, createInFlightDeduper, defer, ignorePromise, isAbortError, isObjectEmpty, noop, promisify, resultify, trySettlePromises, wait, withPerfmon };
197
+ export { DeferredPromise, ErrorResult, InFlightDeduper, Monitor, MonitorMark, MonitorResult, PerfmonError, PerfmonResult, WithIdempotencyId, WithPerfmonReturn, chunk, chunkStr, clamp, createInFlightDeduper, defer, ignorePromise, isAbortError, isObjectEmpty, noop, promisify, resultify, trySettlePromises, wait, withPerfmon };
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,13 +124,19 @@ function createInFlightDeduper() {
112
124
  function use(key, fn) {
113
125
  let promise = __promises.get(key);
114
126
  if (promise) return promise;
115
- promise = Promise.resolve(fn()).finally(() => {
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
- return { use };
133
+ function maybeIdempotent(p, fn) {
134
+ return p.idempotencyId ? use(p.idempotencyId, fn) : promiseTry(fn);
135
+ }
136
+ return {
137
+ use,
138
+ maybeIdempotent
139
+ };
122
140
  }
123
141
  //#endregion
124
142
  //#region src/resultify.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsnw/common-utils",
3
- "version": "1.5.1",
3
+ "version": "1.5.3",
4
4
  "description": "Lightweight JavaScript/TypeScript utilities with async helpers, array and string operations",
5
5
  "publishConfig": {
6
6
  "access": "public"