@storeon/angular
Version:
Storeon for Angular 🅰⚡
171 lines (164 loc) • 5 kB
JavaScript
import { InjectionToken, Injectable, Inject, ɵɵdefineInjectable, ɵɵinject, ɵɵdirectiveInject } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { pluck, map, distinctUntilChanged } from 'rxjs/operators';
/**
* @fileoverview added by tsickle
* Generated from: lib/storeon.token.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @type {?} */
const STOREON = new InjectionToken('Token for store object');
/**
* @fileoverview added by tsickle
* Generated from: lib/storeon.service.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @template State, Events
*/
class StoreonService {
/**
* @param {?} store
*/
constructor(store) {
this.store = store;
this.state$ = new BehaviorSubject(this.store.get());
this.unbind = this.store.on('@changed', (/**
* @param {?} state
* @return {?}
*/
(state) => {
this.state$.next(Object.assign({}, (/** @type {?} */ (state))));
return null;
}));
}
/**
* @param {?} pathOrMapFn
* @return {?}
*/
useStoreon(pathOrMapFn) {
/** @type {?} */
let mapped$;
if (typeof pathOrMapFn === 'string') {
mapped$ = this.state$.pipe(pluck(pathOrMapFn));
}
else if (typeof pathOrMapFn === 'function') {
mapped$ = this.state$.pipe(map((/**
* @param {?} source
* @return {?}
*/
source => pathOrMapFn(source))));
}
else {
throw new TypeError(`Unexpected type '${typeof pathOrMapFn}' in select operator,` +
` expected 'string' or 'function'`);
}
return mapped$.pipe(distinctUntilChanged());
}
/**
* @template K
* @param {?} event
* @param {?=} data
* @return {?}
*/
dispatch(event, data) {
// @ts-ignore
this.store.dispatch(event, data);
}
/**
* @return {?}
*/
ngOnDestroy() {
if (this.unbind) {
this.unbind();
}
}
}
StoreonService.decorators = [
{ type: Injectable, args: [{
providedIn: 'root'
},] }
];
/** @nocollapse */
StoreonService.ctorParameters = () => [
{ type: undefined, decorators: [{ type: Inject, args: [STOREON,] }] }
];
/** @nocollapse */ StoreonService.ɵprov = ɵɵdefineInjectable({ factory: function StoreonService_Factory() { return new StoreonService(ɵɵinject(STOREON)); }, token: StoreonService, providedIn: "root" });
if (false) {
/**
* @type {?}
* @private
*/
StoreonService.prototype.state$;
/**
* @type {?}
* @private
*/
StoreonService.prototype.unbind;
/**
* @type {?}
* @private
*/
StoreonService.prototype.store;
}
/**
* @fileoverview added by tsickle
* Generated from: lib/storeon.decorator.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
*
*
* Experimental feature, works only with Ivy renderer
* Patches the component with keys from storeon store
*
* @template State, Events
* @param {?} config
* @return {?}
*/
function UseStoreon(config) {
return (/**
* @param {?} cmpType
* @return {?}
*/
(cmpType) => {
/** @type {?} */
const isNg11 = !!cmpType.ɵfac;
/** @type {?} */
const originalFactory = isNg11 ? cmpType.ɵfac : cmpType.ngComponentDef.factory;
/** @type {?} */
const newFactory = (/**
* @return {?}
*/
() => {
/** @type {?} */
const ngCompType = isNg11 ? cmpType.ɵcmp.type : cmpType.ngComponentDef.type;
/** @type {?} */
const cmp = originalFactory(ngCompType);
/** @type {?} */
const storeon = ɵɵdirectiveInject(StoreonService);
config.keys.forEach((/**
* @param {?} key
* @return {?}
*/
key => cmp[key] = storeon.useStoreon(key)));
if (config.dispatcher) {
cmp[config.dispatcher] = storeon.dispatch.bind(storeon);
}
return cmp;
});
cmpType.ɵfac = newFactory;
});
}
/**
* @fileoverview added by tsickle
* Generated from: public_api.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* Generated from: storeon-angular.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { STOREON, StoreonService, UseStoreon };
//# sourceMappingURL=storeon-angular.js.map