-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
disposables.ts
97 lines (84 loc) · 2.75 KB
/
disposables.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
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
93
94
95
96
97
import { microTask } from './micro-task'
export type Disposables = ReturnType<typeof disposables>
/**
* Disposables are a way to manage event handlers and functions like
* `setTimeout` and `requestAnimationFrame` that need to be cleaned up when they
* are no longer needed.
*
*
* When you register a disposable function, it is added to a collection of
* disposables. Each disposable in the collection provides a `dispose` clean up
* function that can be called when it's no longer needed. There is also a
* `dispose` function on the collection itself that can be used to clean up all
* pending disposables in that collection.
*/
export function disposables() {
let _disposables: Function[] = []
let api = {
addEventListener<TEventName extends keyof WindowEventMap>(
element: HTMLElement | Window | Document,
name: TEventName,
listener: (event: WindowEventMap[TEventName]) => any,
options?: boolean | AddEventListenerOptions
) {
element.addEventListener(name, listener as any, options)
return api.add(() => element.removeEventListener(name, listener as any, options))
},
requestAnimationFrame(...args: Parameters<typeof requestAnimationFrame>) {
let raf = requestAnimationFrame(...args)
return api.add(() => cancelAnimationFrame(raf))
},
nextFrame(...args: Parameters<typeof requestAnimationFrame>) {
return api.requestAnimationFrame(() => {
return api.requestAnimationFrame(...args)
})
},
setTimeout(...args: Parameters<typeof setTimeout>) {
let timer = setTimeout(...args)
return api.add(() => clearTimeout(timer))
},
microTask(...args: Parameters<typeof microTask>) {
let task = { current: true }
microTask(() => {
if (task.current) {
args[0]()
}
})
return api.add(() => {
task.current = false
})
},
style(node: HTMLElement, property: string, value: string) {
let previous = node.style.getPropertyValue(property)
Object.assign(node.style, { [property]: value })
return this.add(() => {
Object.assign(node.style, { [property]: previous })
})
},
group(cb: (d: typeof this) => void) {
let d = disposables()
cb(d)
return this.add(() => d.dispose())
},
add(cb: () => void) {
// Ensure we don't add the same callback twice
if (!_disposables.includes(cb)) {
_disposables.push(cb)
}
return () => {
let idx = _disposables.indexOf(cb)
if (idx >= 0) {
for (let dispose of _disposables.splice(idx, 1)) {
dispose()
}
}
}
},
dispose() {
for (let dispose of _disposables.splice(0)) {
dispose()
}
},
}
return api
}