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