-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Enhanced DX by including thenable response and curry function (#13
- Loading branch information
1 parent
39ccb88
commit e69672b
Showing
16 changed files
with
319 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
"settle-map": major | ||
--- | ||
|
||
# Update: Enhanced Response Handling | ||
|
||
- Removed the `all` getter in this release. With `settleMap` now returning a `Thenable` object, you can use the more intuitive `await` syntax without `all`. | ||
- Added a new `omitResult` option to prevent result storage. This is useful for saving memory when using the event-driven `on` function or when you would like to ignore it. | ||
- Implemented function overriding to manage the map function response based on the `omitResult` value. | ||
- Renamed the `stop` function to `abort` for clarity. | ||
- Introduced a new curry function, `createSettleMap`, to simplify code and adhere to the `DRY` (Don't Repeat Yourself) principle. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
src | ||
test | ||
|
||
.changeset | ||
.github | ||
.nvmrc | ||
|
||
pnpm-lock.yaml | ||
tsconfig.json | ||
vit.config.ts | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import settleMap from "."; | ||
import type { SettleOptions } from "./types"; | ||
import { mergeOptions } from "./utils"; | ||
|
||
const createSettleMap = | ||
(defaultOption: SettleOptions) => | ||
<T, R>( | ||
items: T[], | ||
fn: (item: T, index: number) => Promise<R>, | ||
newOption?: number | Partial<SettleOptions> | ||
) => | ||
settleMap( | ||
items, | ||
fn, | ||
mergeOptions(newOption ?? defaultOption, defaultOption) | ||
); | ||
|
||
export default createSettleMap; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,35 @@ | ||
import type { Emit, Listener } from "./types"; | ||
import type { Listener } from "./types"; | ||
|
||
class EventEmitter<T, R> { | ||
public readonly listeners: Listener<T, R> = {}; | ||
public listeners: Listener<T, R> | null = null; | ||
|
||
public readonly emit: Emit<T, R> = (event, args) => { | ||
constructor() { | ||
this.listeners = {}; | ||
} | ||
|
||
public readonly emit = <E extends keyof Listener<T, R>>( | ||
event: E, | ||
args: Parameters<NonNullable<Listener<T, R>[E]>>[0] | ||
) => { | ||
try { | ||
return this.listeners[event]?.(args as never); | ||
return this.listeners?.[event]?.(args as never); | ||
} catch { | ||
return null; | ||
} | ||
}; | ||
|
||
public on<E extends keyof Listener<T, R>>( | ||
event: E, | ||
listener: (payload: Parameters<NonNullable<Listener<T, R>[E]>>[0]) => void | ||
) { | ||
if (!this.listeners) return; | ||
|
||
this.listeners[event] = listener; | ||
} | ||
|
||
public destroy() { | ||
this.listeners = null; | ||
} | ||
} | ||
|
||
export default EventEmitter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import settleMap from "./settle-map"; | ||
import createSettleMap from "./create-settle-map"; | ||
|
||
export { settleMap }; | ||
export { settleMap, createSettleMap }; | ||
|
||
export default settleMap; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,56 @@ | ||
import type { SettleOptions } from "./types"; | ||
import type { ReturnObjectType, Result, SettleOptions } from "./types"; | ||
import { mergeOptions } from "./utils"; | ||
import Settler from "./settler"; | ||
|
||
const settleMap = <T, R>( | ||
// return void incase omitResult is true | ||
function settleMap<T, R>( | ||
items: T[], | ||
fn: (item: T, index: number) => Promise<R>, | ||
options: SettleOptions & { omitResult: true } | ||
): ReturnObjectType<T, R> & { | ||
then: (onfulfilled?: (value: undefined) => unknown) => Promise<unknown>; | ||
abort: () => undefined; | ||
}; | ||
|
||
// return result object incase omitResult is false or undefined | ||
function settleMap<T, R>( | ||
items: T[], | ||
fn: (item: T, index: number) => Promise<R>, | ||
options?: number | SettleOptions | ||
): ReturnObjectType<T, R> & { | ||
then: (onfulfilled?: (value: Result<T, R>) => unknown) => Promise<unknown>; | ||
abort: () => Result<T, R>; | ||
}; | ||
|
||
function settleMap<T, R>( | ||
items: T[], | ||
fn: (item: T, index: number) => Promise<R>, | ||
options: SettleOptions | number = 1 | ||
) => { | ||
) { | ||
const settler = new Settler<T, R>(mergeOptions(options)); | ||
const promise = settler.settle(items, fn); | ||
|
||
return { | ||
get all() { | ||
return promise; | ||
waitUntilFinished: async () => { | ||
await settler.promise; | ||
}, | ||
waitUntilFinished: () => settler.waitUntilFinished(), | ||
status() { | ||
return settler.status; | ||
return { | ||
activeCount: settler.limit.activeCount, | ||
pendingCount: settler.limit.pendingCount, | ||
}; | ||
}, | ||
on: settler.events.on.bind(settler.events), | ||
abort: () => { | ||
settler.limit.clearQueue(); | ||
settler.events.destroy(); | ||
|
||
return settler.result; | ||
}, | ||
then(onfulfilled?: (value: Result<T, R> | undefined) => unknown) { | ||
return promise.then(onfulfilled); | ||
}, | ||
on: settler.on.bind(settler), | ||
stop: () => settler.stop(), | ||
}; | ||
}; | ||
} | ||
|
||
export default settleMap; |
Oops, something went wrong.