-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathready.js
111 lines (83 loc) · 1.75 KB
/
ready.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
Ext('Ext.util.DelayedTask', 'Ext.util.Observable', 'Ext.ComponentMgr')
var ready = [], readyCnt = 0, create = Ext.create
function flushrun() {
if (ready.length) {
var ev = ready.shift()
ev[0].call(ev[1], readyCnt)
}
ready.length && flushrun()
}
function readyHook() {
readyCnt++
return function() {
readyCnt--
readyPump()
}
}
function readyPump() {
Ext._ready.delay(Ext._readyTime)
}
Ext._ready = new Ext.util.DelayedTask(function() {
if (readyCnt) {
readyPump()
return
}
if (!Ext.isReady) {
Ext.isReady = true
ready.length && flushrun()
}
})
Ext._readyTime = 700
Ext.onReady = function onReady(fn, scope) {
ready.push([fn, scope || global])
if (Ext.isReady) {
flushrun()
} else {
readyPump()
}
}
Ext.setupReady = function setupReady(obs) {
readyPump()
if ('function' == obs.once) {
obs.once('ready', readyHook())
} else if (obs instanceof Ext.util.Observable) {
obs.on('ready', readyHook(), obs, {single: true})
} else {
throw new Error('Expected Observable')
}
}
Ext.create = function createReady(...args) {
let r = create.apply(Ext, args)
if (r instanceof Ext.util.Observable && r.events.ready) {
r.on('ready', readyHook(), r, {single: true})
}
return r
}
Ext.readyP = function() {
let P = Ext.Promise || Promise
if (Ext.isReady) {
return P.resolve()
}
return new P((p, f) => {
Ext.onReady(p)
})
}
class ReadyOnLoad {
constructor(cfg) {
this.ev = (cfg && cfg.ev) || 'load'
}
init(cmp) {
this.parent = cmp
if ('function' == typeof cmp.once) {
cmp.once(this.ev, this.x)
} else if (cmp instanceof Ext.util.Observable) {
cmp.on(this.ev, this.x, cmp, {single: true})
cmp.addEvents('ready')
}
Ext.setupReady(cmp)
}
x() {
this.fireEvent('ready')
}
}
Ext.preg('ready', ReadyOnLoad)