UNPKG

@harmowatch/ngx-redux-core

Version:

[![Join the chat at https://gitter.im/harmowatch/ngx-redux-core](https://badges.gitter.im/harmowatch/ngx-redux-core.svg)](https://gitter.im/harmowatch/ngx-redux-core?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

2 lines 9.68 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/core"),require("@harmowatch/redux-decorators"),require("rxjs"),require("rxjs/operators"),require("redux"),require("@angular/common")):"function"==typeof define&&define.amd?define("@harmowatch/ngx-redux-core",["exports","@angular/core","@harmowatch/redux-decorators","rxjs","rxjs/operators","redux","@angular/common"],t):t((e.harmowatch=e.harmowatch||{},e.harmowatch["ngx-redux-core"]={}),e.ng.core,null,e.rxjs,e.rxjs.operators,null,e.ng.common)}(this,function(e,u,a,i,c,o,t){"use strict";var n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r])};function s(e,t){function r(){this.constructor=e}n(e,t),e.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)}var d=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e};function p(e,t){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var n,o,i=r.call(e),u=[];try{for(;(void 0===t||0<t--)&&!(n=i.next()).done;)u.push(n.value)}catch(a){o={error:a}}finally{try{n&&!n.done&&(r=i["return"])&&r.call(i)}finally{if(o)throw o.error}}return u}var r=function(e){function t(){return e.call(this,"ReduxStateDefinitionToken")||this}return s(t,e),t}(u.InjectionToken),f=function(e){function t(){return e.call(this,"ReduxStore")||this}return s(t,e),t}(u.InjectionToken),l=function(){function o(e){void 0===e&&(e=null),o.reset(),o.registerStore(e)}return o.reset=function(){o._store=new i.AsyncSubject},o.registerStore=function(r){o.reset(),o._store.next(r),o._store.complete(),a.ReduxActionDispatcher.dispatchedActions.subscribe(function(e){var t={type:e.type,payload:e.payload};r.dispatch(t),e.onDispatchSuccess&&e.onDispatchSuccess()})},o.registerState=function(n){o.getStore().then(function(t){var r=a.ReduxStateDecorator.get(n.constructor),e=n.getInitialState();Promise.resolve(e instanceof i.Observable?e.toPromise():e).then(function(e){t.dispatch({payload:{initialValue:e,name:r.name},type:o.ACTION_REGISTER_STATE})})})},o.getStore=function(){return new Promise(o._store.subscribe.bind(o._store))},o.ACTION_REGISTER_STATE="@harmowatch/ngx-redux-core/registerState",o._store=new i.AsyncSubject,o.decorators=[{type:u.Injectable}],o.ctorParameters=function(){return[{type:undefined,decorators:[{type:u.Inject,args:[f]}]}]},o}(),h=function(e){function u(r,n,o){void 0===n&&(n="/");var i=this;if(!n.startsWith(u.DELIMITER)&&!o)throw new Error("You need to provide a state provider, if you use relative selectors");return i=e.call(this,1)||this,l.getStore().then(function(e){var t=function(){r.run(function(){i.next(u.getValueByState(e.getState(),n,o))})};e.subscribe(function(){return t()}),t()}),i}return s(u,e),u.normalize=function(e,t){if(!e.startsWith(u.DELIMITER)){var r=a.ReduxStateDecorator.get(t).name;return""+u.DELIMITER+r+u.DELIMITER+e}return e},u.getValueByState=function(e,t,r){return u.normalize(t,r).split(u.DELIMITER).filter(function(e){return""!==e}).reduce(function(e,t){return e&&e.hasOwnProperty(t)?e[t]:null},e)},u.DELIMITER="/",u}(i.ReplaySubject),y=function(){function i(e,t){void 0===e&&(e=[]);var r=this;this.zone=t,this.selectorCache={};var n=(a.ReduxStateDecorator.get(this.constructor)||{}).name,o=void 0===n?null:n;if(!o)throw new Error('Unable to resolve state name! Make sure you\'ve decorated the provider by "@ReduxState"!');if(this.name=a.ReduxStateDecorator.get(this.constructor).name,this.stateDef=e.find(function(e){return a.ReduxStateDecorator.get(e.provider).name===o}),!this.stateDef)throw new Error("Unable to resolve state definition! Make sure you've registered the provider to ReduxModule!");this.reducerMethodsByType=(this.stateDef.reducers||[]).map(function(e){return r.getReducerMethods(new e)}).reduce(function(e,t){return[].concat(t,e)},[]).reduce(function(e,t){var r,n=a.ReduxActionDispatcher.getType(t.type);return d({},e,((r={})[n]=[t.method].concat(e[n]||[]),r))},{}),i.instancesByName[this.name]=this}return i.prototype.getInitialState=function(){throw new Error('Method "getInitialState" not implemented.')},i.prototype.select=function(e){void 0===e&&(e="");var t=this.constructor;return e=h.normalize(e,t),this.selectorCache[e]||(this.selectorCache[e]=new h(this.zone,e,t).pipe(c.distinctUntilChanged())),this.selectorCache[e]},i.prototype.getState=function(){var t=this;return l.getStore().then(function(e){return h.getValueByState(e.getState(),"/"+t.name)})},i.prototype.reduce=function(e,r){return(this.reducerMethodsByType[r.type]||[]).reduce(function(e,t){return t(e,r)},e)},i.prototype.getReducerMethods=function(t){return Object.values(Object.getPrototypeOf(t)).map(function(e){return{method:e.bind(t),type:a.ReduxReducerDecorator.get(e)}}).filter(function(e){return e&&e.type}).reduce(function(e,t){return e.concat([].concat(t.type).map(function(e){return d({},t,{type:e})}))},[])},i.instancesByName={},i.ctorParameters=function(){return[{type:Array,decorators:[{type:u.Inject,args:[r]}]},{type:u.NgZone}]},i}();var v=function(){function e(){this.stateProviders={}}return Object.defineProperty(e.prototype,"rootReducer",{get:function(){return this.reduce.bind(this)},enumerable:!0,configurable:!0}),e.prototype.addStateProvider=function(e){if(this.stateProviders[e.name])throw new Error('State "'+e.name+'" is registered twice! Make sure your state name is unique!');l.registerState(e),this.stateProviders[e.name]=e},e.prototype.reduce=function(e,n){var t;if(n.type===l.ACTION_REGISTER_STATE){var r=n;return d({},e,((t={})[r.payload.name]=r.payload.initialValue,t))}return Object.values(this.stateProviders).reduce(function(e,t){var r;return Object.assign({},e,((r={})[t.name]=t.reduce(e[t.name],n),r))},e)},e.decorators=[{type:u.Injectable}],e}(),g=function(){function e(e,t){void 0===e&&(e=[]),this.provider=t.get(e[0].provider)}return e.prototype.transform=function(e){return this.provider.select(e)},e.decorators=[{type:u.Pipe,args:[{name:"reduxSelect"}]}],e.ctorParameters=function(){return[{type:Array,decorators:[{type:u.Inject,args:[r]}]},{type:u.Injector}]},e}(),m=function(){function e(){this.state=new i.BehaviorSubject(null)}return e.factory=function(){return new e},e.prototype.setState=function(e,t){var r,n=this,o=a.ReduxStateDecorator.get(e).name,i=Object.assign({},this.state.getValue(),((r={})[o]=t,r));return this.state.next(i),this.state.pipe(c.takeWhile(function(e){return e!==i})).toPromise().then(function(){return n.state.getValue()})},e.prototype.getState=function(){return this.state.getValue()},e.prototype.subscribe=function(e){return this.state.subscribe.call(this.state,e)},e.prototype.replaceReducer=function(){},e.prototype.dispatch=function(e){return e},e.decorators=[{type:u.Injectable}],e}(),S=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}var r;return s(t,e),(r=t).prototype.getInitialState=function(){return r.INITIAL_STATE},t.NAME="testing-7c66b613-20bd-4d35-8611-5181ca4a0b72",t.INITIAL_STATE={todo:{isFetching:!1,items:["Item 1","Item 2"]}},t=r=function(e,t,r,n){var o,i=arguments.length,u=i<3?t:null===n?n=Object.getOwnPropertyDescriptor(t,r):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)u=Reflect.decorate(e,t,r,n);else for(var a=e.length-1;0<=a;a--)(o=e[a])&&(u=(i<3?o(u):3<i?o(t,r,u):o(t,r))||u);return 3<i&&u&&Object.defineProperty(t,r,u),u}([a.ReduxStateDecoratorForClass({name:r.NAME})],t)}(y),R=function(e){function t(){return e.call(this,"ReduxMiddlewares")||this}return s(t,e),t}(u.InjectionToken),x=function(){function n(t,r,e){void 0===e&&(e=[]),t.get(l),Array.isArray(e)&&e.filter(function(e){return e&&e.provider}).map(function(e){return t.get(e.provider)}).forEach(function(e){return r.addStateProvider(e)})}return n.forChild=function(e){return void 0===e&&(e={}),{ngModule:n,providers:[{provide:r,useValue:e.state||null,multi:!0}]}},n.forRoot=function(e){return void 0===e&&(e={}),{ngModule:n,providers:[v,l,{provide:f,useFactory:e.storeFactory||n.defaultStoreFactory,deps:[v,R]},{provide:r,useValue:e.state||null,multi:!0},{provide:R,useValue:e.middlewareFunctions||[],multi:!1}]}},n.defaultStoreFactory=function(e,t,r){return void 0===r&&(r=u.isDevMode()),o.createStore(e.rootReducer,{},n.defaultEnhancerFactory(t,r))},n.defaultEnhancerFactory=function(e,t){var r=o.compose;return t&&window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__&&(r=window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__),r(o.applyMiddleware.apply(void 0,function(){for(var e=[],t=0;t<arguments.length;t++)e=e.concat(p(arguments[t]));return e}(e)))},n.decorators=[{type:u.NgModule,args:[{declarations:[g],exports:[g],imports:[t.CommonModule]}]}],n.ctorParameters=function(){return[{type:u.Injector},{type:v},{type:Array,decorators:[{type:u.Optional},{type:u.Inject,args:[r]}]}]},n}(),b=a.ReduxActionDispatcher.getType,I=a.ReduxActionDispatcher.dispatch;e.ReduxActionContext=a.ReduxActionContextDecoratorForClass,e.ReduxAction=a.ReduxActionDecoratorForMethod,e.ReduxReducer=a.ReduxReducerDecoratorForMethod,e.ReduxState=a.ReduxStateDecoratorForClass,e.ReduxModule=x,e.getActionType=b,e.dispatch=I,e.ReduxSelect=function(n,o){return function(e,t){var r=a.ReduxStateDecorator.get(o).name;Object.defineProperty(e,t,{get:function(){return y.instancesByName[r].select(n)}})}},e.ReduxReducerProvider=v,e.ReduxSelectPipe=g,e.ReduxSelector=h,e.ReduxStateDefinitionToken=r,e.ReduxStateProvider=y,e.ReduxStore=f,e.ReduxTestingStore=m,e.TestingStateProvider=S,e.ReduxRegistry=l,e.ɵa=R,Object.defineProperty(e,"__esModule",{value:!0})}); //# sourceMappingURL=harmowatch-ngx-redux-core.umd.min.js.map