-
-
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: adds return promise to success and error events and better ts s…
…upport
- Loading branch information
Showing
3 changed files
with
61 additions
and
37 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 |
---|---|---|
@@ -1,60 +1,70 @@ | ||
import type { VisitOptions, GlobalEventsMap } from '@inertiajs/core'; | ||
import type { GlobalEventsMap } from '@inertiajs/core'; | ||
|
||
export type Events<A extends [...args: any]> = { | ||
export type EventCallback<A extends [...args: any]> = { | ||
[K in keyof Omit<GlobalEventsMap, 'navigate' | 'invalid' | 'exception'>] | ||
: (...args: [...GlobalEventsMap[K]['parameters'], ...A]) => GlobalEventsMap[K]['result']; | ||
: (...args: [...GlobalEventsMap[K]['parameters'], ...A]) | ||
=> K extends 'success' | 'error' | ||
? Promise<GlobalEventsMap[K]['result']> | GlobalEventsMap[K]['result'] | ||
: GlobalEventsMap[K]['result']; | ||
} & { | ||
cancelToken: (...args: [{ cancel: () => void }, ...A]) => void; | ||
}; | ||
|
||
export type EventsList<A extends [...args: any]> = { | ||
[K in keyof Events<A>]: Events<A>[K][]; | ||
}; | ||
export type OnFunction<A extends [...args: any]> = ReturnType<typeof createEventCallbackManager<A>>['on']; | ||
export type CombineFunction<A extends [...args: any]> = ReturnType<typeof createEventCallbackManager<A>>['combine']; | ||
export type ExecuteFunction<A extends [...args: any]> = ReturnType<typeof createEventCallbackManager<A>>['execute']; | ||
|
||
export const useEventsSystem = <E extends [...args: any]>() => { | ||
const eventList: Partial<EventsList<E>> = {}; | ||
export const createEventCallbackManager = <E extends [...args: any]>() => { | ||
const events: Partial<{ | ||
[K in keyof EventCallback<E>]: EventCallback<E>[K][]; | ||
}> = {}; | ||
|
||
const on = <T extends keyof Events<E>>(eventName: T, callback: Events<E>[T]) => { | ||
if (typeof eventList[eventName] === 'undefined') eventList[eventName] = []; | ||
const on = <T extends keyof EventCallback<E>>(eventName: T, callback: EventCallback<E>[T]) => { | ||
if (typeof events[eventName] === 'undefined') events[eventName] = []; | ||
|
||
eventList[eventName]?.push(callback); | ||
events[eventName]?.push(callback); | ||
}; | ||
|
||
const combine = (combineCb: (cb: typeof on) => void | ((cb: typeof on) => void)[]) => { | ||
if (Array.isArray(combineCb)) combineCb.forEach((cb) => cb(on)); | ||
combineCb(on); | ||
}; | ||
|
||
const execute = <T extends keyof Events<E>>(eventName: T, ...params: Parameters<Events<E>[T]>): ReturnType<Events<E>[T]> | undefined => { | ||
const events = eventList[eventName]; | ||
if (!events) return; | ||
const execute = <T extends keyof EventCallback<E>>(eventName: T, ...params: Parameters<EventCallback<E>[T]>): ReturnType<EventCallback<E>[T]> | undefined => { | ||
const eventList = events[eventName]; | ||
if (!eventList) return; | ||
|
||
if (eventName === 'before') { | ||
for (const event of events) { | ||
for (const event of eventList) { | ||
const res = event(...params); | ||
|
||
if (typeof res === 'boolean') return res as ReturnType<EventCallback<E>[T]>; | ||
} | ||
} else if (['success', 'error'].includes(eventName)) { | ||
let promiseResolver = Promise.resolve(); | ||
|
||
for (const event of eventList) { | ||
const res = event(...params); | ||
|
||
if (typeof res === 'boolean') return res as ReturnType<Events<E>[T]>; | ||
if (res instanceof Promise) { | ||
promiseResolver = res; | ||
} else { | ||
promiseResolver = Promise.resolve(res as void); | ||
} | ||
} | ||
|
||
return promiseResolver as ReturnType<EventCallback<E>[T]>; | ||
} else { | ||
for (const event of events) { | ||
for (const event of eventList) { | ||
event(...params); | ||
} | ||
} | ||
}; | ||
|
||
const toVisitOptions = (...params: E): VisitOptions => ((Object.keys(eventList) as (keyof typeof eventList)[]).map((name) => ({ | ||
[`on${(name.charAt(0).toUpperCase() + name.slice(1)) as Capitalize<keyof typeof eventList>}`]: (arg: any) => { | ||
return execute(name, ...[arg, ...params]); | ||
} | ||
})).reduce((p, c) => ({ | ||
...p, | ||
...c | ||
}), {})); | ||
|
||
return { | ||
events, | ||
on, | ||
combine, | ||
execute, | ||
toVisitOptions | ||
execute | ||
}; | ||
}; |
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,4 +1,4 @@ | ||
export type { Events, EventsList } from './event'; | ||
export { useEventsSystem } from './event'; | ||
export type { EventCallback, OnFunction, CombineFunction, ExecuteFunction } from './event'; | ||
export { createEventCallbackManager } from './event'; | ||
|
||
export { useForm } from './inertia'; |
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