-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
spy.ts
32 lines (26 loc) · 942 Bytes
/
spy.ts
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
import { globalState } from "./globalstate"
import { once, Lambda } from "../utils/utils"
export function isSpyEnabled() {
return !!globalState.spyListeners.length
}
export function spyReport(event) {
if (!globalState.spyListeners.length) return
const listeners = globalState.spyListeners
for (let i = 0, l = listeners.length; i < l; i++) listeners[i](event)
}
export function spyReportStart(event) {
const change = { ...event, spyReportStart: true }
spyReport(change)
}
const END_EVENT = { spyReportEnd: true }
export function spyReportEnd(change?) {
if (change) spyReport({ ...change, spyReportEnd: true })
else spyReport(END_EVENT)
}
export function spy(listener: (change: any) => void): Lambda {
globalState.spyListeners.push(listener)
return once(() => {
const idx = globalState.spyListeners.indexOf(listener)
if (idx !== -1) globalState.spyListeners.splice(idx, 1)
})
}