-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
136 lines (109 loc) · 2.82 KB
/
index.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const onload = require('fast-on-load')
const unloaders = Symbol('unloaders')
const updating = Symbol('updating')
const first = Symbol('first')
const element = Symbol('element')
module.exports = class Component {
constructor (opts) {
if (!opts) opts = {}
this[element] = opts.element || null
this[unloaders] = []
this[updating] = false
this[first] = true
this.loaded = false
if (opts.onload) this.onload = opts.onload
if (opts.onunload) this.onunload = opts.onunload
if (opts.render) this.render = opts.render
if (opts.createElement) this.createElement = opts.createElement
}
get element () {
if (!this[element]) this[element] = this.createElement()
if (this[element] && this[first]) {
this[first] = false
onload(this[element],
this._onload.bind(this),
this._onunload.bind(this),
this.constructor
)
}
return this[element]
}
set element (el) {
this[element] = el
}
on (emitter, name, fn) {
this[unloaders].push([emitter, name, fn])
on(emitter, name, fn)
}
once (emitter, name, fn) {
this[unloaders].push([emitter, name, fn])
once(emitter, name, fn)
}
off (emitter, name, fn) {
off(emitter, name, fn)
// swap and pop
for (let i = 0; i < this[unloaders].length; i++) {
const [e, n, f] = this[unloaders][i]
if (e === emitter && n === name && f === fn) {
this[unloaders][i] = this[unloaders][this[unloaders].length - 1]
this[unloaders].pop()
}
}
}
update () {
if (this[updating]) return
this[updating] = true
window.requestAnimationFrame(this._reallyUpdate.bind(this))
}
_reallyUpdate () {
if (!this[updating]) return
this[updating] = false
this.render()
}
createElement () {
// overwrite me
return null
}
render () {
// overwrite me
}
onload () {
// overwrite me
}
onunload () {
// overwrite me
}
_onload () {
this.loaded = true
this.onload()
// if any listeners were attached in onload we trigger a rerender as state
// may have changed between createElement or unload/load
if (this[unloaders].length) this.render()
}
_onunload () {
this.loaded = false
this[updating] = false
const list = this[unloaders]
while (list.length) {
const [emitter, name, fn] = list.pop()
off(emitter, name, fn)
}
this.onunload()
}
}
function on (e, name, fn) {
if (e.on) e.on(name, fn)
else if (e.addEventListener) e.addEventListener(name, fn)
}
function off (e, name, fn) {
if (e.off) e.off(name, fn)
else if (e.removeEventListener) e.removeEventListener(name, fn)
else e.removeListener(name, fn)
}
function once (e, name, fn) {
if (e.once) {
e.once(name, fn)
} else if (e.addEventListener) {
e.addEventListener(name, fn, { once: true })
}
}