( hookName: K, plugins: readonly Plugin[], )
| 200 | } |
| 201 | |
| 202 | export function getSortedPluginsByHook<K extends keyof Plugin>( |
| 203 | hookName: K, |
| 204 | plugins: readonly Plugin[], |
| 205 | ): PluginWithRequiredHook<K>[] { |
| 206 | const sortedPlugins: Plugin[] = [] |
| 207 | // Use indexes to track and insert the ordered plugins directly in the |
| 208 | // resulting array to avoid creating 3 extra temporary arrays per hook |
| 209 | let pre = 0, |
| 210 | normal = 0, |
| 211 | post = 0 |
| 212 | for (const plugin of plugins) { |
| 213 | const hook = plugin[hookName] |
| 214 | if (hook) { |
| 215 | if (typeof hook === 'object') { |
| 216 | if (hook.order === 'pre') { |
| 217 | sortedPlugins.splice(pre++, 0, plugin) |
| 218 | continue |
| 219 | } |
| 220 | if (hook.order === 'post') { |
| 221 | sortedPlugins.splice(pre + normal + post++, 0, plugin) |
| 222 | continue |
| 223 | } |
| 224 | } |
| 225 | sortedPlugins.splice(pre + normal++, 0, plugin) |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | return sortedPlugins as PluginWithRequiredHook<K>[] |
| 230 | } |
| 231 | |
| 232 | export function getHookHandler<T extends ObjectHook<Function>>( |
| 233 | hook: T, |
no outgoing calls
no test coverage detected