1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | var __extends = (this && this.__extends) || function (d, b) {
|
9 | for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
|
10 | function __() { this.constructor = d; }
|
11 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
12 | };
|
13 | import { ErrorHandler } from '../src/error_handler';
|
14 | import { ListWrapper } from '../src/facade/collection';
|
15 | import { stringify } from '../src/facade/lang';
|
16 | import { isPromise } from '../src/util/lang';
|
17 | import { ApplicationInitStatus } from './application_init';
|
18 | import { APP_BOOTSTRAP_LISTENER, PLATFORM_INITIALIZER } from './application_tokens';
|
19 | import { Console } from './console';
|
20 | import { Injectable, Injector, OpaqueToken, Optional, ReflectiveInjector } from './di';
|
21 | import { CompilerFactory } from './linker/compiler';
|
22 | import { ComponentFactory } from './linker/component_factory';
|
23 | import { ComponentFactoryResolver } from './linker/component_factory_resolver';
|
24 | import { wtfCreateScope, wtfLeave } from './profile/profile';
|
25 | import { Testability, TestabilityRegistry } from './testability/testability';
|
26 | import { NgZone } from './zone/ng_zone';
|
27 | var _devMode = true;
|
28 | var _runModeLocked = false;
|
29 | var _platform;
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | export function enableProdMode() {
|
42 | if (_runModeLocked) {
|
43 | throw new Error('Cannot enable prod mode after platform setup.');
|
44 | }
|
45 | _devMode = false;
|
46 | }
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | export function isDevMode() {
|
57 | _runModeLocked = true;
|
58 | return _devMode;
|
59 | }
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | export var NgProbeToken = (function () {
|
66 | |
67 |
|
68 |
|
69 |
|
70 | function NgProbeToken(name, token) {
|
71 | this.name = name;
|
72 | this.token = token;
|
73 | }
|
74 | return NgProbeToken;
|
75 | }());
|
76 | function NgProbeToken_tsickle_Closure_declarations() {
|
77 |
|
78 | NgProbeToken.prototype.name;
|
79 |
|
80 | NgProbeToken.prototype.token;
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | export function createPlatform(injector) {
|
91 | if (_platform && !_platform.destroyed) {
|
92 | throw new Error('There can be only one platform. Destroy the previous one to create a new one.');
|
93 | }
|
94 | _platform = injector.get(PlatformRef);
|
95 | var inits = (injector.get(PLATFORM_INITIALIZER, null));
|
96 | if (inits)
|
97 | inits.forEach(function (init) { return init(); });
|
98 | return _platform;
|
99 | }
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | export function createPlatformFactory(parentPlatformFactory, name, providers) {
|
110 | if (providers === void 0) { providers = []; }
|
111 | var marker = new OpaqueToken("Platform: " + name);
|
112 | return function (extraProviders) {
|
113 | if (extraProviders === void 0) { extraProviders = []; }
|
114 | if (!getPlatform()) {
|
115 | if (parentPlatformFactory) {
|
116 | parentPlatformFactory(providers.concat(extraProviders).concat({ provide: marker, useValue: true }));
|
117 | }
|
118 | else {
|
119 | createPlatform(ReflectiveInjector.resolveAndCreate(providers.concat(extraProviders).concat({ provide: marker, useValue: true })));
|
120 | }
|
121 | }
|
122 | return assertPlatform(marker);
|
123 | };
|
124 | }
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | export function assertPlatform(requiredToken) {
|
134 | var platform = getPlatform();
|
135 | if (!platform) {
|
136 | throw new Error('No platform exists!');
|
137 | }
|
138 | if (!platform.injector.get(requiredToken, null)) {
|
139 | throw new Error('A platform with a different configuration has been created. Please destroy it first.');
|
140 | }
|
141 | return platform;
|
142 | }
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | export function destroyPlatform() {
|
150 | if (_platform && !_platform.destroyed) {
|
151 | _platform.destroy();
|
152 | }
|
153 | }
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | export function getPlatform() {
|
161 | return _platform && !_platform.destroyed ? _platform : null;
|
162 | }
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 | export var PlatformRef = (function () {
|
175 | function PlatformRef() {
|
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 | PlatformRef.prototype.bootstrapModuleFactory = function (moduleFactory) { };
|
204 | |
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 | PlatformRef.prototype.bootstrapModule = function (moduleType, compilerOptions) { };
|
224 | |
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | PlatformRef.prototype.onDestroy = function (callback) { };
|
231 | |
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 | PlatformRef.prototype.injector = function () { };
|
238 | |
239 |
|
240 |
|
241 |
|
242 |
|
243 | PlatformRef.prototype.destroy = function () { };
|
244 | |
245 |
|
246 |
|
247 |
|
248 | PlatformRef.prototype.destroyed = function () { };
|
249 | return PlatformRef;
|
250 | }());
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 | function _callAndReportToErrorHandler(errorHandler, callback) {
|
257 | try {
|
258 | var result = callback();
|
259 | if (isPromise(result)) {
|
260 | return result.catch(function (e) {
|
261 | errorHandler.handleError(e);
|
262 |
|
263 | throw e;
|
264 | });
|
265 | }
|
266 | return result;
|
267 | }
|
268 | catch (e) {
|
269 | errorHandler.handleError(e);
|
270 |
|
271 | throw e;
|
272 | }
|
273 | }
|
274 | export var PlatformRef_ = (function (_super) {
|
275 | __extends(PlatformRef_, _super);
|
276 | |
277 |
|
278 |
|
279 | function PlatformRef_(_injector) {
|
280 | _super.call(this);
|
281 | this._injector = _injector;
|
282 | this._modules = [];
|
283 | this._destroyListeners = [];
|
284 | this._destroyed = false;
|
285 | }
|
286 | |
287 |
|
288 |
|
289 |
|
290 | PlatformRef_.prototype.onDestroy = function (callback) { this._destroyListeners.push(callback); };
|
291 | Object.defineProperty(PlatformRef_.prototype, "injector", {
|
292 | |
293 |
|
294 |
|
295 | get: function () { return this._injector; },
|
296 | enumerable: true,
|
297 | configurable: true
|
298 | });
|
299 | Object.defineProperty(PlatformRef_.prototype, "destroyed", {
|
300 | |
301 |
|
302 |
|
303 | get: function () { return this._destroyed; },
|
304 | enumerable: true,
|
305 | configurable: true
|
306 | });
|
307 | |
308 |
|
309 |
|
310 | PlatformRef_.prototype.destroy = function () {
|
311 | if (this._destroyed) {
|
312 | throw new Error('The platform has already been destroyed!');
|
313 | }
|
314 | this._modules.slice().forEach(function (module) { return module.destroy(); });
|
315 | this._destroyListeners.forEach(function (listener) { return listener(); });
|
316 | this._destroyed = true;
|
317 | };
|
318 | |
319 |
|
320 |
|
321 |
|
322 | PlatformRef_.prototype.bootstrapModuleFactory = function (moduleFactory) {
|
323 | return this._bootstrapModuleFactoryWithZone(moduleFactory, null);
|
324 | };
|
325 | |
326 |
|
327 |
|
328 |
|
329 |
|
330 | PlatformRef_.prototype._bootstrapModuleFactoryWithZone = function (moduleFactory, ngZone) {
|
331 | var _this = this;
|
332 |
|
333 |
|
334 |
|
335 |
|
336 | if (!ngZone)
|
337 | ngZone = new NgZone({ enableLongStackTrace: isDevMode() });
|
338 |
|
339 |
|
340 | return ngZone.run(function () {
|
341 | var ngZoneInjector = ReflectiveInjector.resolveAndCreate([{ provide: NgZone, useValue: ngZone }], _this.injector);
|
342 | var moduleRef = (moduleFactory.create(ngZoneInjector));
|
343 | var exceptionHandler = moduleRef.injector.get(ErrorHandler, null);
|
344 | if (!exceptionHandler) {
|
345 | throw new Error('No ErrorHandler. Is platform module (BrowserModule) included?');
|
346 | }
|
347 | moduleRef.onDestroy(function () { return ListWrapper.remove(_this._modules, moduleRef); });
|
348 | ngZone.onError.subscribe({ next: function (error) { exceptionHandler.handleError(error); } });
|
349 | return _callAndReportToErrorHandler(exceptionHandler, function () {
|
350 | var initStatus = moduleRef.injector.get(ApplicationInitStatus);
|
351 | return initStatus.donePromise.then(function () {
|
352 | _this._moduleDoBootstrap(moduleRef);
|
353 | return moduleRef;
|
354 | });
|
355 | });
|
356 | });
|
357 | };
|
358 | |
359 |
|
360 |
|
361 |
|
362 |
|
363 | PlatformRef_.prototype.bootstrapModule = function (moduleType, compilerOptions) {
|
364 | if (compilerOptions === void 0) { compilerOptions = []; }
|
365 | return this._bootstrapModuleWithZone(moduleType, compilerOptions, null);
|
366 | };
|
367 | |
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 | PlatformRef_.prototype._bootstrapModuleWithZone = function (moduleType, compilerOptions, ngZone, componentFactoryCallback) {
|
375 | var _this = this;
|
376 | if (compilerOptions === void 0) { compilerOptions = []; }
|
377 | var compilerFactory = this.injector.get(CompilerFactory);
|
378 | var compiler = compilerFactory.createCompiler(Array.isArray(compilerOptions) ? compilerOptions : [compilerOptions]);
|
379 |
|
380 |
|
381 |
|
382 | if (componentFactoryCallback) {
|
383 | return compiler.compileModuleAndAllComponentsAsync(moduleType)
|
384 | .then(function (_a) {
|
385 | var ngModuleFactory = _a.ngModuleFactory, componentFactories = _a.componentFactories;
|
386 | componentFactoryCallback(componentFactories);
|
387 | return _this._bootstrapModuleFactoryWithZone(ngModuleFactory, ngZone);
|
388 | });
|
389 | }
|
390 | return compiler.compileModuleAsync(moduleType)
|
391 | .then(function (moduleFactory) { return _this._bootstrapModuleFactoryWithZone(moduleFactory, ngZone); });
|
392 | };
|
393 | |
394 |
|
395 |
|
396 |
|
397 | PlatformRef_.prototype._moduleDoBootstrap = function (moduleRef) {
|
398 | var appRef = moduleRef.injector.get(ApplicationRef);
|
399 | if (moduleRef.bootstrapFactories.length > 0) {
|
400 | moduleRef.bootstrapFactories.forEach(function (compFactory) { return appRef.bootstrap(compFactory); });
|
401 | }
|
402 | else if (moduleRef.instance.ngDoBootstrap) {
|
403 | moduleRef.instance.ngDoBootstrap(appRef);
|
404 | }
|
405 | else {
|
406 | throw new Error(("The module " + stringify(moduleRef.instance.constructor) + " was bootstrapped, but it does not declare \"@NgModule.bootstrap\" components nor a \"ngDoBootstrap\" method. ") +
|
407 | "Please define one of these.");
|
408 | }
|
409 | this._modules.push(moduleRef);
|
410 | };
|
411 | PlatformRef_.decorators = [
|
412 | { type: Injectable },
|
413 | ];
|
414 |
|
415 | PlatformRef_.ctorParameters = function () { return [
|
416 | { type: Injector, },
|
417 | ]; };
|
418 | return PlatformRef_;
|
419 | }(PlatformRef));
|
420 | function PlatformRef__tsickle_Closure_declarations() {
|
421 |
|
422 | PlatformRef_.decorators;
|
423 | |
424 |
|
425 |
|
426 |
|
427 | PlatformRef_.ctorParameters;
|
428 |
|
429 | PlatformRef_.prototype._modules;
|
430 |
|
431 | PlatformRef_.prototype._destroyListeners;
|
432 |
|
433 | PlatformRef_.prototype._destroyed;
|
434 |
|
435 | PlatformRef_.prototype._injector;
|
436 | }
|
437 |
|
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 | export var ApplicationRef = (function () {
|
446 | function ApplicationRef() {
|
447 | }
|
448 | |
449 |
|
450 |
|
451 |
|
452 |
|
453 |
|
454 |
|
455 |
|
456 |
|
457 |
|
458 |
|
459 |
|
460 |
|
461 |
|
462 |
|
463 | ApplicationRef.prototype.bootstrap = function (componentFactory) { };
|
464 | |
465 |
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 | ApplicationRef.prototype.tick = function () { };
|
477 | |
478 |
|
479 |
|
480 |
|
481 |
|
482 |
|
483 | ApplicationRef.prototype.componentTypes = function () { };
|
484 | |
485 |
|
486 |
|
487 |
|
488 |
|
489 | ApplicationRef.prototype.components = function () { };
|
490 | |
491 |
|
492 |
|
493 |
|
494 |
|
495 |
|
496 |
|
497 |
|
498 | ApplicationRef.prototype.attachView = function (view) { };
|
499 | |
500 |
|
501 |
|
502 |
|
503 |
|
504 |
|
505 | ApplicationRef.prototype.detachView = function (view) { };
|
506 | |
507 |
|
508 |
|
509 |
|
510 |
|
511 | ApplicationRef.prototype.viewCount = function () { };
|
512 | return ApplicationRef;
|
513 | }());
|
514 | export var ApplicationRef_ = (function (_super) {
|
515 | __extends(ApplicationRef_, _super);
|
516 | |
517 |
|
518 |
|
519 |
|
520 |
|
521 |
|
522 |
|
523 |
|
524 |
|
525 |
|
526 | function ApplicationRef_(_zone, _console, _injector, _exceptionHandler, _componentFactoryResolver, _initStatus, _testabilityRegistry, _testability) {
|
527 | var _this = this;
|
528 | _super.call(this);
|
529 | this._zone = _zone;
|
530 | this._console = _console;
|
531 | this._injector = _injector;
|
532 | this._exceptionHandler = _exceptionHandler;
|
533 | this._componentFactoryResolver = _componentFactoryResolver;
|
534 | this._initStatus = _initStatus;
|
535 | this._testabilityRegistry = _testabilityRegistry;
|
536 | this._testability = _testability;
|
537 | this._bootstrapListeners = [];
|
538 | this._rootComponents = [];
|
539 | this._rootComponentTypes = [];
|
540 | this._views = [];
|
541 | this._runningTick = false;
|
542 | this._enforceNoNewChanges = false;
|
543 | this._enforceNoNewChanges = isDevMode();
|
544 | this._zone.onMicrotaskEmpty.subscribe({ next: function () { _this._zone.run(function () { _this.tick(); }); } });
|
545 | }
|
546 | |
547 |
|
548 |
|
549 |
|
550 | ApplicationRef_.prototype.attachView = function (viewRef) {
|
551 | var view = ((viewRef)).internalView;
|
552 | this._views.push(view);
|
553 | view.attachToAppRef(this);
|
554 | };
|
555 | |
556 |
|
557 |
|
558 |
|
559 | ApplicationRef_.prototype.detachView = function (viewRef) {
|
560 | var view = ((viewRef)).internalView;
|
561 | ListWrapper.remove(this._views, view);
|
562 | view.detach();
|
563 | };
|
564 | |
565 |
|
566 |
|
567 |
|
568 | ApplicationRef_.prototype.bootstrap = function (componentOrFactory) {
|
569 | var _this = this;
|
570 | if (!this._initStatus.done) {
|
571 | throw new Error('Cannot bootstrap as there are still asynchronous initializers running. Bootstrap components in the `ngDoBootstrap` method of the root module.');
|
572 | }
|
573 | var componentFactory;
|
574 | if (componentOrFactory instanceof ComponentFactory) {
|
575 | componentFactory = componentOrFactory;
|
576 | }
|
577 | else {
|
578 | componentFactory = this._componentFactoryResolver.resolveComponentFactory(componentOrFactory);
|
579 | }
|
580 | this._rootComponentTypes.push(componentFactory.componentType);
|
581 | var compRef = componentFactory.create(this._injector, [], componentFactory.selector);
|
582 | compRef.onDestroy(function () { _this._unloadComponent(compRef); });
|
583 | var testability = compRef.injector.get(Testability, null);
|
584 | if (testability) {
|
585 | compRef.injector.get(TestabilityRegistry)
|
586 | .registerApplication(compRef.location.nativeElement, testability);
|
587 | }
|
588 | this._loadComponent(compRef);
|
589 | if (isDevMode()) {
|
590 | this._console.log("Angular is running in the development mode. Call enableProdMode() to enable the production mode.");
|
591 | }
|
592 | return compRef;
|
593 | };
|
594 | |
595 |
|
596 |
|
597 |
|
598 | ApplicationRef_.prototype._loadComponent = function (componentRef) {
|
599 | this.attachView(componentRef.hostView);
|
600 | this.tick();
|
601 | this._rootComponents.push(componentRef);
|
602 |
|
603 | var listeners = (this._injector.get(APP_BOOTSTRAP_LISTENER, [])
|
604 | .concat(this._bootstrapListeners));
|
605 | listeners.forEach(function (listener) { return listener(componentRef); });
|
606 | };
|
607 | |
608 |
|
609 |
|
610 |
|
611 | ApplicationRef_.prototype._unloadComponent = function (componentRef) {
|
612 | this.detachView(componentRef.hostView);
|
613 | ListWrapper.remove(this._rootComponents, componentRef);
|
614 | };
|
615 | |
616 |
|
617 |
|
618 | ApplicationRef_.prototype.tick = function () {
|
619 | if (this._runningTick) {
|
620 | throw new Error('ApplicationRef.tick is called recursively');
|
621 | }
|
622 | var scope = ApplicationRef_._tickScope();
|
623 | try {
|
624 | this._runningTick = true;
|
625 | this._views.forEach(function (view) { return view.ref.detectChanges(); });
|
626 | if (this._enforceNoNewChanges) {
|
627 | this._views.forEach(function (view) { return view.ref.checkNoChanges(); });
|
628 | }
|
629 | }
|
630 | finally {
|
631 | this._runningTick = false;
|
632 | wtfLeave(scope);
|
633 | }
|
634 | };
|
635 | |
636 |
|
637 |
|
638 | ApplicationRef_.prototype.ngOnDestroy = function () {
|
639 |
|
640 | this._views.slice().forEach(function (view) { return view.destroy(); });
|
641 | };
|
642 | Object.defineProperty(ApplicationRef_.prototype, "viewCount", {
|
643 | |
644 |
|
645 |
|
646 | get: function () { return this._views.length; },
|
647 | enumerable: true,
|
648 | configurable: true
|
649 | });
|
650 | Object.defineProperty(ApplicationRef_.prototype, "componentTypes", {
|
651 | |
652 |
|
653 |
|
654 | get: function () { return this._rootComponentTypes; },
|
655 | enumerable: true,
|
656 | configurable: true
|
657 | });
|
658 | Object.defineProperty(ApplicationRef_.prototype, "components", {
|
659 | |
660 |
|
661 |
|
662 | get: function () { return this._rootComponents; },
|
663 | enumerable: true,
|
664 | configurable: true
|
665 | });
|
666 |
|
667 | ApplicationRef_._tickScope = wtfCreateScope('ApplicationRef#tick()');
|
668 | ApplicationRef_.decorators = [
|
669 | { type: Injectable },
|
670 | ];
|
671 |
|
672 | ApplicationRef_.ctorParameters = function () { return [
|
673 | { type: NgZone, },
|
674 | { type: Console, },
|
675 | { type: Injector, },
|
676 | { type: ErrorHandler, },
|
677 | { type: ComponentFactoryResolver, },
|
678 | { type: ApplicationInitStatus, },
|
679 | { type: TestabilityRegistry, decorators: [{ type: Optional },] },
|
680 | { type: Testability, decorators: [{ type: Optional },] },
|
681 | ]; };
|
682 | return ApplicationRef_;
|
683 | }(ApplicationRef));
|
684 | function ApplicationRef__tsickle_Closure_declarations() {
|
685 | |
686 |
|
687 |
|
688 |
|
689 | ApplicationRef_._tickScope;
|
690 |
|
691 | ApplicationRef_.decorators;
|
692 | |
693 |
|
694 |
|
695 |
|
696 | ApplicationRef_.ctorParameters;
|
697 |
|
698 | ApplicationRef_.prototype._bootstrapListeners;
|
699 |
|
700 | ApplicationRef_.prototype._rootComponents;
|
701 |
|
702 | ApplicationRef_.prototype._rootComponentTypes;
|
703 |
|
704 | ApplicationRef_.prototype._views;
|
705 |
|
706 | ApplicationRef_.prototype._runningTick;
|
707 |
|
708 | ApplicationRef_.prototype._enforceNoNewChanges;
|
709 |
|
710 | ApplicationRef_.prototype._zone;
|
711 |
|
712 | ApplicationRef_.prototype._console;
|
713 |
|
714 | ApplicationRef_.prototype._injector;
|
715 |
|
716 | ApplicationRef_.prototype._exceptionHandler;
|
717 |
|
718 | ApplicationRef_.prototype._componentFactoryResolver;
|
719 |
|
720 | ApplicationRef_.prototype._initStatus;
|
721 |
|
722 | ApplicationRef_.prototype._testabilityRegistry;
|
723 |
|
724 | ApplicationRef_.prototype._testability;
|
725 | }
|
726 |
|
\ | No newline at end of file |