Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 55 additions & 5 deletions src/components/ng-redux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export class NgRedux<RootState> {
private _defaultMapDispatchToTarget: Function;


/**
* Creates an instance of NgRedux.
*
* @param {Redux.Store<RootState>} store Redux store
*/
constructor(store: Redux.Store<RootState>) {
this._store = store;
this._store$ = this.observableFromStore(store);
Expand All @@ -31,16 +36,23 @@ export class NgRedux<RootState> {
}

/**
* @param store
* Create an observable from a Redux store.
*
* @param {Store<RootState>} store Redux store to create an observable from
* @returns {BehaviorSubject<RootState>}
*/
observableFromStore = (store: Store<RootState>) => {
return new BehaviorSubject(store.getState());
};


/**
* @param selector
* @param comparer
* @returns {Observable<S>}
* Select a slice of state to expose as an observable.
*
* @template S
* @param {(string | number | symbol | ((state: RootState) => S))} selector key or function to select a part of the state
* @param {(x: any, y: any) => boolean} [comparer] optional comparison function called to test if an item is distinct from the previous item in the source.
* @returns {Observable<S>} an Observable that emits items from the source Observable with distinct values.
*/
select<S>(selector: string | number | symbol | ((state: RootState) => S),
comparer?: (x: any, y: any) => boolean): Observable<S> {
Expand All @@ -63,11 +75,23 @@ export class NgRedux<RootState> {
}
wrapActionCreators = (actions) => wrapActionCreators(actions);

/**
* Map the specified actions to the target
* @param {any} actions the actions to bind to the target
* @returns {(target:any)=>void} a function to pass your target into
*/
mapDispatchToTarget = (actions) => (target) => {
return this.updateTarget(target, {}, this.getBoundActions(actions));
};

connect = (mapStateToTarget, mapDispatchToTarget) => {
/**
* Connect your component to your redux state.
*
* @param {*} mapStateToTarget connect will subscribe to Redux store updates. Any time it updates, mapStateToTarget will be called. Its result must be a plain object, and it will be merged into `target`. If you have a component which simply triggers actions without needing any state you can pass null to `mapStateToTarget`.
* @param {*} mapDispatchToTarget Optional. If an object is passed, each function inside it will be assumed to be a Redux action creator. An object with the same function names, but bound to a Redux store, will be merged onto `target`. If a function is passed, it will be given `dispatch`. It’s up to you to return an object that somehow uses `dispatch` to bind action creators in your own way. (Tip: you may use the [`bindActionCreators()`](http://gaearon.github.io/redux/docs/api/bindActionCreators.html) helper from Redux.).
* @returns a function that accepts a target object to map the state and/or dispatch onto, or a function that will recieve the result of mapStateToTarget and mapDispatchToTarget as paramaters
*/
connect = (mapStateToTarget: any, mapDispatchToTarget: any) => {

const finalMapStateToTarget = mapStateToTarget
|| this._defaultMapStateToTarget;
Expand Down Expand Up @@ -108,18 +132,44 @@ export class NgRedux<RootState> {

};



/**
* Get the current state of the application
* @returns {RootState} the application state
*/
getState = (): RootState => {
return this._store.getState();
};


/**
* Subscribe to the Redux store changes
*
* @param {() => void} listener callback to invoke when the state is updated
* @returns a function to unsubscribe
*/
subscribe = (listener: () => void) => {
return this._store.subscribe(listener);
};

/**
* Replaces the reducer currently used by the store to calculate the state.
*
* You might need this if your app implements code splitting and you want to
* load some of the reducers dynamically. You might also need this if you
* implement a hot reloading mechanism for Redux.
*
* @param nextReducer The reducer for the store to use instead.
*/
replaceReducer = (nextReducer: Reducer<RootState>) => {
return this._store.replaceReducer(nextReducer);
};


/**
* Dispatch an action to Redux
*/
dispatch = <A extends Action>(action: A): any => {
return this._store.dispatch(action);
};
Expand Down