UNPKG

6.99 kBJavaScriptView Raw
1import { autorun, reaction, makeAutoObservable, action as action$1, computed as computed$1, observable as observable$1 } from 'mobx';
2import * as i0 from '@angular/core';
3import { Directive, Input, Injectable, NgModule } from '@angular/core';
4import * as i1 from '@angular/router';
5import { NavigationEnd } from '@angular/router';
6import { filter } from 'rxjs/operators';
7
8// import { mobxAngularDebug } from '../utils/mobx-angular-debug';
9class MobxAutorunDirective {
10 constructor(templateRef, viewContainer) {
11 this.templateRef = templateRef;
12 this.viewContainer = viewContainer;
13 this.templateBindings = {};
14 this.allAutorunOptions = [
15 'delay',
16 'scheduler',
17 'requiresObservable',
18 'name',
19 'onError'
20 ];
21 }
22 ngOnInit() {
23 this.view = this.viewContainer.createEmbeddedView(this.templateRef);
24 if (this.dispose) {
25 this.dispose();
26 }
27 if (this.shouldDetach()) {
28 this.view.detach();
29 }
30 this.autoDetect(this.view);
31 // mobxAngularDebug(this.view, this.dispose);
32 }
33 shouldDetach() {
34 return this.mobxAutorun && this.mobxAutorun.detach;
35 }
36 autoDetect(view) {
37 const opts = this.getAutorunOptions();
38 this.dispose = autorun(() => view.detectChanges(), opts);
39 }
40 getAutorunOptions() {
41 return Object.keys(this.mobxAutorun || {}).reduce((opts, current) => {
42 if (this.allAutorunOptions.includes(current)) {
43 opts[current] = this.mobxAutorun[current];
44 }
45 return opts;
46 }, {});
47 }
48 ngOnDestroy() {
49 if (this.dispose) {
50 this.dispose();
51 }
52 }
53}
54MobxAutorunDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: MobxAutorunDirective, deps: [{ token: i0.TemplateRef }, { token: i0.ViewContainerRef }], target: i0.ɵɵFactoryTarget.Directive });
55MobxAutorunDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.1.5", type: MobxAutorunDirective, selector: "[mobxAutorun]", inputs: { mobxAutorun: "mobxAutorun" }, ngImport: i0 });
56i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: MobxAutorunDirective, decorators: [{
57 type: Directive,
58 args: [{ selector: '[mobxAutorun]' }]
59 }], ctorParameters: function () { return [{ type: i0.TemplateRef }, { type: i0.ViewContainerRef }]; }, propDecorators: { mobxAutorun: [{
60 type: Input
61 }] } });
62
63class MobxReactionDirective extends MobxAutorunDirective {
64 constructor(templateRef, viewContainer) {
65 super(templateRef, viewContainer);
66 this.templateRef = templateRef;
67 this.viewContainer = viewContainer;
68 }
69 autoDetect(view) {
70 const opts = Object.assign({ fireImmediately: true }, this.mobxReactionOptions);
71 this.dispose = reaction(this.mobxReaction, () => {
72 view.detectChanges();
73 }, opts);
74 }
75}
76MobxReactionDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: MobxReactionDirective, deps: [{ token: i0.TemplateRef }, { token: i0.ViewContainerRef }], target: i0.ɵɵFactoryTarget.Directive });
77MobxReactionDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.1.5", type: MobxReactionDirective, selector: "[mobxReaction]", inputs: { mobxReaction: "mobxReaction", mobxReactionOptions: "mobxReactionOptions" }, usesInheritance: true, ngImport: i0 });
78i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: MobxReactionDirective, decorators: [{
79 type: Directive,
80 args: [{ selector: '[mobxReaction]' }]
81 }], ctorParameters: function () { return [{ type: i0.TemplateRef }, { type: i0.ViewContainerRef }]; }, propDecorators: { mobxReaction: [{
82 type: Input
83 }], mobxReactionOptions: [{
84 type: Input
85 }] } });
86
87class RouterStore {
88 constructor(router, activatedRoute) {
89 this.router = router;
90 this.activatedRoute = activatedRoute;
91 this.url = '';
92 this.routeSnapshot = null;
93 makeAutoObservable(this);
94 router.events
95 .pipe(filter((event) => event instanceof NavigationEnd))
96 .subscribe((e) => this.routeListener(e));
97 }
98 routeListener(event) {
99 this.routeSnapshot = this.activatedRoute.snapshot;
100 this.url = event.urlAfterRedirects;
101 }
102}
103RouterStore.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: RouterStore, deps: [{ token: i1.Router }, { token: i1.ActivatedRoute }], target: i0.ɵɵFactoryTarget.Injectable });
104RouterStore.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: RouterStore, providedIn: 'root' });
105i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: RouterStore, decorators: [{
106 type: Injectable,
107 args: [{ providedIn: 'root' }]
108 }], ctorParameters: function () { return [{ type: i1.Router }, { type: i1.ActivatedRoute }]; } });
109
110const DIRECTIVES = [MobxAutorunDirective, MobxReactionDirective];
111class MobxAngularModule {
112}
113MobxAngularModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: MobxAngularModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
114MobxAngularModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.1.5", ngImport: i0, type: MobxAngularModule, declarations: [MobxAutorunDirective, MobxReactionDirective], exports: [MobxAutorunDirective, MobxReactionDirective] });
115MobxAngularModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: MobxAngularModule });
116i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: MobxAngularModule, decorators: [{
117 type: NgModule,
118 args: [{
119 declarations: DIRECTIVES,
120 exports: DIRECTIVES
121 }]
122 }] });
123
124/*
125 * Public API Surface of mobx-angular
126 */
127// Re-export mobx operators to be able to use inside components with AOT:
128function actionInternal(...args) {
129 return action$1(...args);
130}
131const action = Object.assign(actionInternal, action$1);
132function computedInternal(...args) {
133 return computed$1(...args);
134}
135const computed = Object.assign(computedInternal, computed$1);
136function observableInternal(...args) {
137 return observable$1(...args);
138}
139const observable = Object.assign(observableInternal, observable$1);
140
141/**
142 * Generated bundle index. Do not edit.
143 */
144
145export { MobxAngularModule, MobxAutorunDirective, MobxReactionDirective, RouterStore, action, actionInternal, computed, observable };
146//# sourceMappingURL=mobx-angular.mjs.map