-
Notifications
You must be signed in to change notification settings - Fork 395
/
Copy pathrunloop.js
149 lines (118 loc) · 3.26 KB
/
runloop.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
137
138
139
140
141
142
143
144
145
146
147
148
149
import circular from 'circular';
import fireEvent from 'Ractive/prototype/shared/fireEvent';
import removeFromArray from 'utils/removeFromArray';
import Promise from 'utils/Promise';
import resolveRef from 'shared/resolveRef';
import TransitionManager from 'global/TransitionManager';
var batch, runloop, unresolved = [];
runloop = {
start: function ( instance, returnPromise ) {
var promise, fulfilPromise;
if ( returnPromise ) {
promise = new Promise( f => ( fulfilPromise = f ) );
}
batch = {
previousBatch: batch,
transitionManager: new TransitionManager( fulfilPromise, batch && batch.transitionManager ),
views: [],
tasks: [],
viewmodels: []
};
if ( instance ) {
batch.viewmodels.push( instance.viewmodel );
}
return promise;
},
end: function () {
flushChanges();
batch.transitionManager.init();
batch = batch.previousBatch;
},
addViewmodel: function ( viewmodel ) {
if ( batch ) {
if ( batch.viewmodels.indexOf( viewmodel ) === -1 ) {
batch.viewmodels.push( viewmodel );
}
} else {
viewmodel.applyChanges();
}
},
registerTransition: function ( transition ) {
transition._manager = batch.transitionManager;
batch.transitionManager.add( transition );
},
addView: function ( view ) {
batch.views.push( view );
},
addUnresolved: function ( thing ) {
unresolved.push( thing );
},
removeUnresolved: function ( thing ) {
removeFromArray( unresolved, thing );
},
// synchronise node detachments with transition ends
detachWhenReady: function ( thing ) {
batch.transitionManager.detachQueue.push( thing );
},
scheduleTask: function ( task ) {
if ( !batch ) {
task();
} else {
batch.tasks.push( task );
}
}
};
circular.runloop = runloop;
export default runloop;
function flushChanges () {
var i, thing, changeHash;
for ( i = 0; i < batch.viewmodels.length; i += 1 ) {
thing = batch.viewmodels[i];
changeHash = thing.applyChanges();
if ( changeHash ) {
fireEvent( thing.ractive, 'change', { args: [ changeHash ] });
}
}
batch.viewmodels.length = 0;
attemptKeypathResolution();
// Now that changes have been fully propagated, we can update the DOM
// and complete other tasks
for ( i = 0; i < batch.views.length; i += 1 ) {
batch.views[i].update();
}
batch.views.length = 0;
for ( i = 0; i < batch.tasks.length; i += 1 ) {
batch.tasks[i]();
}
batch.tasks.length = 0;
// If updating the view caused some model blowback - e.g. a triple
// containing <option> elements caused the binding on the <select>
// to update - then we start over
if ( batch.viewmodels.length ) return flushChanges();
}
function attemptKeypathResolution () {
var i, item, keypath, resolved;
i = unresolved.length;
// see if we can resolve any unresolved references
while ( i-- ) {
item = unresolved[i];
if ( item.keypath ) {
// it resolved some other way. TODO how? two-way binding? Seems
// weird that we'd still end up here
unresolved.splice( i, 1 );
}
if ( keypath = resolveRef( item.root, item.ref, item.parentFragment ) ) {
( resolved || ( resolved = [] ) ).push({
item: item,
keypath: keypath
});
unresolved.splice( i, 1 );
}
}
if ( resolved ) {
resolved.forEach( resolve );
}
}
function resolve ( resolved ) {
resolved.item.resolve( resolved.keypath );
}