forked from livewire/livewire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.js
92 lines (74 loc) · 2.26 KB
/
hooks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { isFunction } from '@/utils'
/**
* Our internal event listener bus...
*/
let listeners = []
/**
* Register a callback to run when an event is triggered...
*/
export function on(name, callback) {
if (! listeners[name]) listeners[name] = []
listeners[name].push(callback)
// Return an "off" callback to remove the listener...
return () => {
listeners[name] = listeners[name].filter(i => i !== callback)
}
}
/**
* In addition to triggering an event, this method allows you to
* defer running callbacks returned from listeners and pass a
* value through each one so they can act like middleware.
*
* An example of using this combination to the fullest:
*
* // First let's look at the triggering phase:
* let finish = trigger([event name], ...[event params])
*
* return finish([pass-through value])
*
* // Now, let's look at the "listening" phase:
* on([event name], (...[event params]) => {
* // The contents of this callback will be run immediately on trigger.
*
* return ([pass-through value]) => {
* // This callback will be run when "finish()" is called.
*
* // The [pass-through value] can be mutated and must
* // be returned for the next callback to process.
* return [pass-through value]
* }
* })
*/
export function trigger(name, ...params) {
let callbacks = listeners[name] || []
let finishers = []
for (let i = 0; i < callbacks.length; i++) {
let finisher = callbacks[i](...params)
if (isFunction(finisher)) finishers.push(finisher)
}
return (result) => {
return runFinishers(finishers, result)
}
}
export async function triggerAsync(name, ...params) {
let callbacks = listeners[name] || []
let finishers = []
for (let i = 0; i < callbacks.length; i++) {
let finisher = await callbacks[i](...params)
if (isFunction(finisher)) finishers.push(finisher)
}
return (result) => {
return runFinishers(finishers, result)
}
}
export function runFinishers(finishers, result)
{
let latest = result
for (let i = 0; i < finishers.length; i++) {
let iResult = finishers[i](latest)
if (iResult !== undefined) {
latest = iResult
}
}
return latest
}