-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathcompose.ts
322 lines (294 loc) · 8.25 KB
/
compose.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import { Container } from 'aurelia-dependency-injection';
import { DOM } from 'aurelia-pal';
import { TaskQueue } from 'aurelia-task-queue';
import { bindable, CompositionContext, CompositionEngine, customElement, noView, View, ViewResources, ViewSlot } from 'aurelia-templating';
/**
* Available activation strategies for the view and view-model bound to `<compose/>` element
*
* @export
* @enum {string}
*/
export enum ActivationStrategy {
/**
* Default activation strategy; the 'activate' lifecycle hook will be invoked when the model changes.
*/
InvokeLifecycle = 'invoke-lifecycle',
/**
* The view/view-model will be recreated, when the "model" changes.
*/
Replace = 'replace'
}
/**
* Used to compose a new view / view-model template or bind to an existing instance.
*/
@noView
@customElement('compose')
export class Compose {
/**@internal */
static inject() {
return [DOM.Element, Container, CompositionEngine, ViewSlot, ViewResources, TaskQueue];
}
/**
* Model to bind the custom element to.
*
* @property model
* @type {CustomElement}
*/
@bindable model: any;
/**
* View to bind the custom element to.
*
* @property view
* @type {HtmlElement}
*/
@bindable view: any;
/**
* View-model to bind the custom element's template to.
*
* @property viewModel
* @type {Class}
*/
@bindable viewModel: any;
/**
* Strategy to activate the view-model. Default is "invoke-lifecycle".
* Bind "replace" to recreate the view/view-model when the model changes.
*
* @property activationStrategy
* @type {ActivationStrategy}
*/
@bindable activationStrategy: ActivationStrategy = ActivationStrategy.InvokeLifecycle;
/**
* SwapOrder to control the swapping order of the custom element's view.
*
* @property view
* @type {String}
*/
@bindable swapOrder: any;
/**
*@internal
*/
element: any;
/**
*@internal
*/
container: any;
/**
*@internal
*/
compositionEngine: any;
/**
*@internal
*/
viewSlot: any;
/**
*@internal
*/
viewResources: any;
/**
*@internal
*/
taskQueue: any;
/**
*@internal
*/
currentController: any;
/**
*@internal
*/
currentViewModel: any;
/**
*@internal
*/
changes: any;
/**
*@internal
*/
owningView: View;
/**
*@internal
*/
bindingContext: any;
/**
*@internal
*/
overrideContext: any;
/**
*@internal
*/
pendingTask: any;
/**
*@internal
*/
updateRequested: any;
/**
* Creates an instance of Compose.
* @param element The Compose element.
* @param container The dependency injection container instance.
* @param compositionEngine CompositionEngine instance to compose the element.
* @param viewSlot The slot the view is injected in to.
* @param viewResources Collection of resources used to compile the the view.
* @param taskQueue The TaskQueue instance.
*/
constructor(element, container, compositionEngine, viewSlot, viewResources, taskQueue) {
this.element = element;
this.container = container;
this.compositionEngine = compositionEngine;
this.viewSlot = viewSlot;
this.viewResources = viewResources;
this.taskQueue = taskQueue;
this.currentController = null;
this.currentViewModel = null;
this.changes = Object.create(null);
}
/**
* Invoked when the component has been created.
*
* @param owningView The view that this component was created inside of.
*/
created(owningView: View) {
this.owningView = owningView;
}
/**
* Used to set the bindingContext.
*
* @param bindingContext The context in which the view model is executed in.
* @param overrideContext The context in which the view model is executed in.
*/
bind(bindingContext, overrideContext) {
this.bindingContext = bindingContext;
this.overrideContext = overrideContext;
let changes = this.changes;
changes.view = this.view;
changes.viewModel = this.viewModel;
changes.model = this.model;
if (!this.pendingTask) {
processChanges(this);
}
}
/**
* Unbinds the Compose.
*/
unbind() {
this.changes = Object.create(null);
this.bindingContext = null;
this.overrideContext = null;
let returnToCache = true;
let skipAnimation = true;
this.viewSlot.removeAll(returnToCache, skipAnimation);
}
/**
* Invoked everytime the bound model changes.
* @param newValue The new value.
* @param oldValue The old value.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
modelChanged(newValue, oldValue) {
this.changes.model = newValue;
requestUpdate(this);
}
/**
* Invoked everytime the bound view changes.
* @param newValue The new value.
* @param oldValue The old value.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
viewChanged(newValue, oldValue) {
this.changes.view = newValue;
requestUpdate(this);
}
/**
* Invoked everytime the bound view model changes.
* @param newValue The new value.
* @param oldValue The old value.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
viewModelChanged(newValue, oldValue) {
this.changes.viewModel = newValue;
requestUpdate(this);
}
}
function isEmpty(obj) {
for (const _ in obj) {
return false;
}
return true;
}
function tryActivateViewModel(vm, model) {
if (vm && typeof vm.activate === 'function') {
return Promise.resolve(vm.activate(model));
}
}
function createInstruction(composer: Compose, instruction: CompositionContext): CompositionContext {
return Object.assign(instruction, {
bindingContext: composer.bindingContext,
overrideContext: composer.overrideContext,
owningView: composer.owningView,
container: composer.container,
viewSlot: composer.viewSlot,
viewResources: composer.viewResources,
currentController: composer.currentController,
host: composer.element,
swapOrder: composer.swapOrder
});
}
function processChanges(composer: Compose) {
const changes = composer.changes;
composer.changes = Object.create(null);
const activationStrategy = determineActivationStrategy(composer);
if (needsReInitialization(activationStrategy, changes)) {
//when using `replace` activation strategy - we cant force the creation of a new VM
const currentViewModel = activationStrategy === ActivationStrategy.Replace ? null : composer.currentViewModel;
// init context
let instruction = {
view: composer.view,
viewModel: currentViewModel || composer.viewModel,
model: composer.model
} as CompositionContext;
// apply changes
instruction = Object.assign(instruction, changes);
// create context
instruction = createInstruction(composer, instruction);
composer.pendingTask = composer.compositionEngine.compose(instruction).then(controller => {
composer.currentController = controller;
composer.currentViewModel = controller ? controller.viewModel : null;
});
} else {
// just try to activate the current view model
composer.pendingTask = tryActivateViewModel(composer.currentViewModel, changes.model);
if (!composer.pendingTask) { return; }
}
composer.pendingTask = composer.pendingTask
.then(() => {
completeCompositionTask(composer);
}, reason => {
completeCompositionTask(composer);
throw reason;
});
}
function completeCompositionTask(composer) {
composer.pendingTask = null;
if (!isEmpty(composer.changes)) {
processChanges(composer);
}
}
function requestUpdate(composer: Compose) {
if (composer.pendingTask || composer.updateRequested) { return; }
composer.updateRequested = true;
composer.taskQueue.queueMicroTask(() => {
composer.updateRequested = false;
processChanges(composer);
});
}
function determineActivationStrategy(composer: Compose): ActivationStrategy {
let activationStrategy = composer.activationStrategy;
const vm = composer.currentViewModel;
if (vm && typeof vm.determineActivationStrategy === 'function') {
activationStrategy = vm.determineActivationStrategy();
}
return activationStrategy;
}
function needsReInitialization(activationStrategy: ActivationStrategy, changes: any): boolean {
return 'view' in changes
|| 'viewModel' in changes
|| activationStrategy === ActivationStrategy.Replace;
}