UNPKG

10.6 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 Object.defineProperty(this, "templateRef", {
12 enumerable: true,
13 configurable: true,
14 writable: true,
15 value: templateRef
16 });
17 Object.defineProperty(this, "viewContainer", {
18 enumerable: true,
19 configurable: true,
20 writable: true,
21 value: viewContainer
22 });
23 Object.defineProperty(this, "templateBindings", {
24 enumerable: true,
25 configurable: true,
26 writable: true,
27 value: {}
28 });
29 Object.defineProperty(this, "dispose", {
30 enumerable: true,
31 configurable: true,
32 writable: true,
33 value: void 0
34 });
35 Object.defineProperty(this, "view", {
36 enumerable: true,
37 configurable: true,
38 writable: true,
39 value: void 0
40 });
41 Object.defineProperty(this, "allAutorunOptions", {
42 enumerable: true,
43 configurable: true,
44 writable: true,
45 value: [
46 'delay',
47 'scheduler',
48 'requiresObservable',
49 'name',
50 'onError'
51 ]
52 });
53 Object.defineProperty(this, "mobxAutorun", {
54 enumerable: true,
55 configurable: true,
56 writable: true,
57 value: void 0
58 });
59 }
60 ngOnInit() {
61 this.view = this.viewContainer.createEmbeddedView(this.templateRef);
62 if (this.dispose) {
63 this.dispose();
64 }
65 if (this.shouldDetach()) {
66 this.view.detach();
67 }
68 this.autoDetect(this.view);
69 // mobxAngularDebug(this.view, this.dispose);
70 }
71 shouldDetach() {
72 return this.mobxAutorun && this.mobxAutorun.detach;
73 }
74 autoDetect(view) {
75 const opts = this.getAutorunOptions();
76 this.dispose = autorun(() => view.detectChanges(), opts);
77 }
78 getAutorunOptions() {
79 return Object.keys(this.mobxAutorun || {}).reduce((opts, current) => {
80 if (this.allAutorunOptions.includes(current)) {
81 opts[current] = this.mobxAutorun[current];
82 }
83 return opts;
84 }, {});
85 }
86 ngOnDestroy() {
87 if (this.dispose) {
88 this.dispose();
89 }
90 }
91}
92Object.defineProperty(MobxAutorunDirective, "\u0275fac", {
93 enumerable: true,
94 configurable: true,
95 writable: true,
96 value: i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.0.0", ngImport: i0, type: MobxAutorunDirective, deps: [{ token: i0.TemplateRef }, { token: i0.ViewContainerRef }], target: i0.ɵɵFactoryTarget.Directive })
97});
98Object.defineProperty(MobxAutorunDirective, "\u0275dir", {
99 enumerable: true,
100 configurable: true,
101 writable: true,
102 value: i0.ɵɵngDeclareDirective({ minVersion: "12.0.0", version: "13.0.0", type: MobxAutorunDirective, selector: "[mobxAutorun]", inputs: { mobxAutorun: "mobxAutorun" }, ngImport: i0 })
103});
104i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.0.0", ngImport: i0, type: MobxAutorunDirective, decorators: [{
105 type: Directive,
106 args: [{ selector: '[mobxAutorun]' }]
107 }], ctorParameters: function () { return [{ type: i0.TemplateRef }, { type: i0.ViewContainerRef }]; }, propDecorators: { mobxAutorun: [{
108 type: Input
109 }] } });
110
111class MobxReactionDirective extends MobxAutorunDirective {
112 constructor(templateRef, viewContainer) {
113 super(templateRef, viewContainer);
114 Object.defineProperty(this, "templateRef", {
115 enumerable: true,
116 configurable: true,
117 writable: true,
118 value: templateRef
119 });
120 Object.defineProperty(this, "viewContainer", {
121 enumerable: true,
122 configurable: true,
123 writable: true,
124 value: viewContainer
125 });
126 Object.defineProperty(this, "mobxReaction", {
127 enumerable: true,
128 configurable: true,
129 writable: true,
130 value: void 0
131 });
132 Object.defineProperty(this, "mobxReactionOptions", {
133 enumerable: true,
134 configurable: true,
135 writable: true,
136 value: void 0
137 });
138 }
139 autoDetect(view) {
140 const opts = Object.assign({ fireImmediately: true }, this.mobxReactionOptions);
141 this.dispose = reaction(this.mobxReaction, () => {
142 view.detectChanges();
143 }, opts);
144 }
145}
146Object.defineProperty(MobxReactionDirective, "\u0275fac", {
147 enumerable: true,
148 configurable: true,
149 writable: true,
150 value: i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.0.0", ngImport: i0, type: MobxReactionDirective, deps: [{ token: i0.TemplateRef }, { token: i0.ViewContainerRef }], target: i0.ɵɵFactoryTarget.Directive })
151});
152Object.defineProperty(MobxReactionDirective, "\u0275dir", {
153 enumerable: true,
154 configurable: true,
155 writable: true,
156 value: i0.ɵɵngDeclareDirective({ minVersion: "12.0.0", version: "13.0.0", type: MobxReactionDirective, selector: "[mobxReaction]", inputs: { mobxReaction: "mobxReaction", mobxReactionOptions: "mobxReactionOptions" }, usesInheritance: true, ngImport: i0 })
157});
158i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.0.0", ngImport: i0, type: MobxReactionDirective, decorators: [{
159 type: Directive,
160 args: [{ selector: '[mobxReaction]' }]
161 }], ctorParameters: function () { return [{ type: i0.TemplateRef }, { type: i0.ViewContainerRef }]; }, propDecorators: { mobxReaction: [{
162 type: Input
163 }], mobxReactionOptions: [{
164 type: Input
165 }] } });
166
167class RouterStore {
168 constructor(router, activatedRoute) {
169 Object.defineProperty(this, "router", {
170 enumerable: true,
171 configurable: true,
172 writable: true,
173 value: router
174 });
175 Object.defineProperty(this, "activatedRoute", {
176 enumerable: true,
177 configurable: true,
178 writable: true,
179 value: activatedRoute
180 });
181 Object.defineProperty(this, "url", {
182 enumerable: true,
183 configurable: true,
184 writable: true,
185 value: ''
186 });
187 Object.defineProperty(this, "routeSnapshot", {
188 enumerable: true,
189 configurable: true,
190 writable: true,
191 value: null
192 });
193 makeAutoObservable(this);
194 router.events
195 .pipe(filter((event) => event instanceof NavigationEnd))
196 .subscribe((e) => this.routeListener(e));
197 }
198 routeListener(event) {
199 this.routeSnapshot = this.activatedRoute.snapshot;
200 this.url = event.urlAfterRedirects;
201 }
202}
203Object.defineProperty(RouterStore, "\u0275fac", {
204 enumerable: true,
205 configurable: true,
206 writable: true,
207 value: i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.0.0", ngImport: i0, type: RouterStore, deps: [{ token: i1.Router }, { token: i1.ActivatedRoute }], target: i0.ɵɵFactoryTarget.Injectable })
208});
209Object.defineProperty(RouterStore, "\u0275prov", {
210 enumerable: true,
211 configurable: true,
212 writable: true,
213 value: i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.0.0", ngImport: i0, type: RouterStore, providedIn: 'root' })
214});
215i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.0.0", ngImport: i0, type: RouterStore, decorators: [{
216 type: Injectable,
217 args: [{ providedIn: 'root' }]
218 }], ctorParameters: function () { return [{ type: i1.Router }, { type: i1.ActivatedRoute }]; } });
219
220const DIRECTIVES = [MobxAutorunDirective, MobxReactionDirective];
221class MobxAngularModule {
222}
223Object.defineProperty(MobxAngularModule, "\u0275fac", {
224 enumerable: true,
225 configurable: true,
226 writable: true,
227 value: i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.0.0", ngImport: i0, type: MobxAngularModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule })
228});
229Object.defineProperty(MobxAngularModule, "\u0275mod", {
230 enumerable: true,
231 configurable: true,
232 writable: true,
233 value: i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "13.0.0", ngImport: i0, type: MobxAngularModule, declarations: [MobxAutorunDirective, MobxReactionDirective], exports: [MobxAutorunDirective, MobxReactionDirective] })
234});
235Object.defineProperty(MobxAngularModule, "\u0275inj", {
236 enumerable: true,
237 configurable: true,
238 writable: true,
239 value: i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.0.0", ngImport: i0, type: MobxAngularModule })
240});
241i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.0.0", ngImport: i0, type: MobxAngularModule, decorators: [{
242 type: NgModule,
243 args: [{
244 declarations: DIRECTIVES,
245 exports: DIRECTIVES
246 }]
247 }] });
248
249/*
250 * Public API Surface of mobx-angular
251 */
252// Re-export mobx operators to be able to use inside components with AOT:
253function actionInternal(...args) {
254 return action$1(...args);
255}
256const action = Object.assign(actionInternal, action$1);
257function computedInternal(...args) {
258 return computed$1(...args);
259}
260const computed = Object.assign(computedInternal, computed$1);
261function observableInternal(...args) {
262 return observable$1(...args);
263}
264const observable = Object.assign(observableInternal, observable$1);
265
266/**
267 * Generated bundle index. Do not edit.
268 */
269
270export { MobxAngularModule, MobxAutorunDirective, MobxReactionDirective, RouterStore, action, actionInternal, computed, observable };
271//# sourceMappingURL=mobx-angular.mjs.map