mobx-angular
Version:
Angular connector to MobX (2 and above)
147 lines (139 loc) • 6.99 kB
JavaScript
import { autorun, reaction, makeAutoObservable, action as action$1, computed as computed$1, observable as observable$1 } from 'mobx';
import * as i0 from '@angular/core';
import { Directive, Input, Injectable, NgModule } from '@angular/core';
import * as i1 from '@angular/router';
import { NavigationEnd } from '@angular/router';
import { filter } from 'rxjs/operators';
// import { mobxAngularDebug } from '../utils/mobx-angular-debug';
class MobxAutorunDirective {
constructor(templateRef, viewContainer) {
this.templateRef = templateRef;
this.viewContainer = viewContainer;
this.templateBindings = {};
this.allAutorunOptions = [
'delay',
'scheduler',
'requiresObservable',
'name',
'onError'
];
}
ngOnInit() {
this.view = this.viewContainer.createEmbeddedView(this.templateRef);
if (this.dispose) {
this.dispose();
}
if (this.shouldDetach()) {
this.view.detach();
}
this.autoDetect(this.view);
// mobxAngularDebug(this.view, this.dispose);
}
shouldDetach() {
return this.mobxAutorun && this.mobxAutorun.detach;
}
autoDetect(view) {
const opts = this.getAutorunOptions();
this.dispose = autorun(() => view.detectChanges(), opts);
}
getAutorunOptions() {
return Object.keys(this.mobxAutorun || {}).reduce((opts, current) => {
if (this.allAutorunOptions.includes(current)) {
opts[current] = this.mobxAutorun[current];
}
return opts;
}, {});
}
ngOnDestroy() {
if (this.dispose) {
this.dispose();
}
}
}
MobxAutorunDirective.ɵ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 });
MobxAutorunDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.1.5", type: MobxAutorunDirective, selector: "[mobxAutorun]", inputs: { mobxAutorun: "mobxAutorun" }, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: MobxAutorunDirective, decorators: [{
type: Directive,
args: [{ selector: '[mobxAutorun]' }]
}], ctorParameters: function () { return [{ type: i0.TemplateRef }, { type: i0.ViewContainerRef }]; }, propDecorators: { mobxAutorun: [{
type: Input
}] } });
class MobxReactionDirective extends MobxAutorunDirective {
constructor(templateRef, viewContainer) {
super(templateRef, viewContainer);
this.templateRef = templateRef;
this.viewContainer = viewContainer;
}
autoDetect(view) {
const opts = Object.assign({ fireImmediately: true }, this.mobxReactionOptions);
this.dispose = reaction(this.mobxReaction, () => {
view.detectChanges();
}, opts);
}
}
MobxReactionDirective.ɵ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 });
MobxReactionDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.1.5", type: MobxReactionDirective, selector: "[mobxReaction]", inputs: { mobxReaction: "mobxReaction", mobxReactionOptions: "mobxReactionOptions" }, usesInheritance: true, ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: MobxReactionDirective, decorators: [{
type: Directive,
args: [{ selector: '[mobxReaction]' }]
}], ctorParameters: function () { return [{ type: i0.TemplateRef }, { type: i0.ViewContainerRef }]; }, propDecorators: { mobxReaction: [{
type: Input
}], mobxReactionOptions: [{
type: Input
}] } });
class RouterStore {
constructor(router, activatedRoute) {
this.router = router;
this.activatedRoute = activatedRoute;
this.url = '';
this.routeSnapshot = null;
makeAutoObservable(this);
router.events
.pipe(filter((event) => event instanceof NavigationEnd))
.subscribe((e) => this.routeListener(e));
}
routeListener(event) {
this.routeSnapshot = this.activatedRoute.snapshot;
this.url = event.urlAfterRedirects;
}
}
RouterStore.ɵ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 });
RouterStore.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: RouterStore, providedIn: 'root' });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: RouterStore, decorators: [{
type: Injectable,
args: [{ providedIn: 'root' }]
}], ctorParameters: function () { return [{ type: i1.Router }, { type: i1.ActivatedRoute }]; } });
const DIRECTIVES = [MobxAutorunDirective, MobxReactionDirective];
class MobxAngularModule {
}
MobxAngularModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: MobxAngularModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
MobxAngularModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.1.5", ngImport: i0, type: MobxAngularModule, declarations: [MobxAutorunDirective, MobxReactionDirective], exports: [MobxAutorunDirective, MobxReactionDirective] });
MobxAngularModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: MobxAngularModule });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: MobxAngularModule, decorators: [{
type: NgModule,
args: [{
declarations: DIRECTIVES,
exports: DIRECTIVES
}]
}] });
/*
* Public API Surface of mobx-angular
*/
// Re-export mobx operators to be able to use inside components with AOT:
function actionInternal(...args) {
return action$1(...args);
}
const action = Object.assign(actionInternal, action$1);
function computedInternal(...args) {
return computed$1(...args);
}
const computed = Object.assign(computedInternal, computed$1);
function observableInternal(...args) {
return observable$1(...args);
}
const observable = Object.assign(observableInternal, observable$1);
/**
* Generated bundle index. Do not edit.
*/
export { MobxAngularModule, MobxAutorunDirective, MobxReactionDirective, RouterStore, action, actionInternal, computed, observable };
//# sourceMappingURL=mobx-angular.mjs.map