@reelout/core 0.11.0 → 0.13.0

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.
@@ -1,166 +0,0 @@
1
- // SPDX-License-Identifier: MIT
2
- // Copyright (c) 2026 Fai
3
-
4
- export type NextFunction<Context> = (context: Context) => Promise<void>
5
-
6
- export type HookHandler<Context> = {
7
- bivariant(context: Context, next: NextFunction<Context>): void | Promise<void>
8
- }['bivariant']
9
-
10
- export const HookPriority = {
11
- High: 50,
12
- Normal: 0,
13
- Low: -50,
14
- } as const
15
-
16
- interface HookRegistration<HookName, Context> {
17
- name: HookName
18
- priority: number
19
- handler: HookHandler<Context>
20
- }
21
-
22
- /**
23
- * Intercept a registered handler during emission.
24
- *
25
- * Runs in place of every registered handler, receiving the shared `context`,
26
- * the `registration` being dispatched, and the `next` function that continues
27
- * with the following handler.
28
- */
29
- export type HandlerMiddleware<HookName, Context> = (
30
- context: Context,
31
- registration: HookRegistration<HookName, Context>,
32
- next: NextFunction<Context>
33
- ) => void | Promise<void>
34
-
35
- type InferContext<Handler extends HookHandler<unknown>> =
36
- Handler extends HookHandler<infer Context> ? Context : never
37
-
38
- /**
39
- * Manage hook registrations and asynchronous execution chains.
40
- *
41
- * Stores handlers in a prioritized registry and executes them sequentially in
42
- * priority order.
43
- */
44
- export class HookManager<
45
- HookMap extends Record<keyof HookMap, HookHandler<unknown>>,
46
- > {
47
- readonly #registry = new Map<
48
- keyof HookMap,
49
- HookRegistration<keyof HookMap, InferContext<HookMap[keyof HookMap]>>[]
50
- >()
51
-
52
- readonly #middleware:
53
- | HandlerMiddleware<keyof HookMap, InferContext<HookMap[keyof HookMap]>>
54
- | undefined
55
-
56
- /**
57
- * Create a hook manager.
58
- *
59
- * @param middleware Wrapper invoked in place of every registered handler
60
- * during emission
61
- */
62
- constructor(
63
- middleware?: HandlerMiddleware<
64
- keyof HookMap,
65
- InferContext<HookMap[keyof HookMap]>
66
- >
67
- ) {
68
- this.#middleware = middleware
69
- }
70
-
71
- /**
72
- * Register a handler function for a specific hook.
73
- *
74
- * Adds the `handler` to the internal registry for `name` and sorts the list
75
- * so higher-priority handlers run first.
76
- *
77
- * @param name Name of the hook to register
78
- * @param handler Callback function executed when the hook triggers
79
- * @param priority Execution order rank where higher values run earlier
80
- */
81
- on<HookName extends keyof HookMap>(
82
- name: HookName,
83
- handler: HookHandler<InferContext<HookMap[HookName]>>,
84
- priority: number = HookPriority.Normal
85
- ): void {
86
- const registrations = this.#registry.get(name) ?? []
87
-
88
- registrations.push({ name, handler, priority })
89
-
90
- registrations.sort((a, b) => b.priority - a.priority)
91
-
92
- this.#registry.set(name, registrations)
93
- }
94
-
95
- /**
96
- * Unregister a handler function from a specific hook.
97
- *
98
- * Removes matching `handler` references from the internal registry for
99
- * `name`. Takes no action if `name` has no registered handlers.
100
- *
101
- * @param name Name of the hook to unregister from
102
- * @param handler Reference of the handler function to remove
103
- */
104
- off<HookName extends keyof HookMap>(
105
- name: HookName,
106
- handler: HookHandler<InferContext<HookMap[HookName]>>
107
- ): void {
108
- const registrations = this.#registry.get(name)
109
-
110
- if (!registrations) return
111
-
112
- this.#registry.set(
113
- name,
114
- registrations.filter((registration) => registration.handler !== handler)
115
- )
116
- }
117
-
118
- /**
119
- * Trigger execution of all handlers registered to a hook.
120
- *
121
- * Passes `context` through the handler pipeline in descending priority order
122
- * and dispatches each registration through the constructor middleware when
123
- * one is provided. Enables handlers to control execution flow using `next`
124
- * and executes `done` after the last handler completes.
125
- *
126
- * @param name Name of the hook to trigger
127
- * @param context Shared payload passed into each handler
128
- * @param done Optional final callback run after the pipeline completes
129
- * @returns Promise that resolves when execution completes
130
- */
131
- async emit<HookName extends keyof HookMap>(
132
- name: HookName,
133
- context: InferContext<HookMap[HookName]>,
134
- done?: NextFunction<InferContext<HookMap[HookName]>>
135
- ): Promise<void> {
136
- type Context = InferContext<HookMap[HookName]>
137
-
138
- const registrations = this.#registry.get(name) ?? []
139
-
140
- const middleware = this.#middleware
141
-
142
- const dispatch = async (index: number, context: Context): Promise<void> => {
143
- const registration = registrations[index]
144
-
145
- if (!registration) {
146
- return done ? await done(context) : undefined
147
- }
148
-
149
- let isNextCalled = false
150
-
151
- const next: NextFunction<Context> = async (context) => {
152
- if (isNextCalled) return
153
- isNextCalled = true
154
- return await dispatch(index + 1, context)
155
- }
156
-
157
- const handler: HookHandler<Context> = middleware
158
- ? (context, next) => middleware(context, registration, next)
159
- : registration.handler
160
-
161
- return await handler(context, next)
162
- }
163
-
164
- return await dispatch(0, context)
165
- }
166
- }