(configs = [])
| 1 | function hook(configs = []) { |
| 2 | const unsubFn = []; |
| 3 | for (const { fn, arr } of configs) { |
| 4 | if (typeof fn !== "function" || !Array.isArray(arr)) continue; |
| 5 | const id = randId(); |
| 6 | arr.push({ fn, id }); |
| 7 | unsubFn.push(() => { |
| 8 | const index = arr.findIndex((e) => e.id === id); |
| 9 | if (index !== -1) arr.splice(index, 1); |
| 10 | }); |
| 11 | } |
| 12 | |
| 13 | return () => { |
| 14 | unsubFn.forEach((fn) => fn?.()); |
| 15 | }; |
| 16 | } |
| 17 | |
| 18 | function randId() { |
| 19 | return Math.random().toString(36).slice(2); |