Skip to content

Commit

Permalink
fix(core): CHECKOUT-2450 Create new observable from observable-like a…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
davidchin committed Feb 28, 2018
1 parent cbe8631 commit 07aaed5
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ npm install --save @bigcommerce/data-store

## Requirements

This library depends on [rxjs](https://github.com/ReactiveX/rxjs). Please refer to [package.json](package.json) for the version requirement.
This library requires [Promise polyfill](https://github.com/stefanpenner/es6-promise) if you need to support older browsers, such as IE11.

It also requires [Promise polyfill](https://github.com/stefanpenner/es6-promise) if you need to support older browsers, such as IE11.
You may need to create Observables when using this library (please refer to the usage section). We recommend you to use [rxjs](https://github.com/ReactiveX/rxjs) until the time comes when you can create them natively.


## Usage
Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,15 @@
"validate-commits": "validate-commits",
"validate-dependencies": "yarn install --check-files --frozen-lockfile"
},
"peerDependencies": {
"rxjs": "^5.5.5"
},
"dependencies": {
"@types/lodash": "^4.14.92",
"lodash": "^4.17.4",
"rxjs": "^5.5.5",
"tslib": "^1.8.0"
},
"devDependencies": {
"@types/jest": "^21.1.10",
"jest": "^21.2.1",
"rxjs": "^5.5.5",
"standard-version": "^4.2.0",
"ts-jest": "^21.2.3",
"tslint": "^5.9.1",
Expand Down
12 changes: 7 additions & 5 deletions src/data-store.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { isEqual, merge } from 'lodash';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Observable } from 'rxjs/Observable';
import { Observable, SubscribableOrPromise } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
import Action from './action';
import deepFreeze from './deep-freeze';
import DispatchableDataStore, { DispatchOptions } from './dispatchable-data-store';
import DispatchableDataStore, { DispatchableAction, DispatchOptions } from './dispatchable-data-store';
import isObservableActionLike from './is-observable-action-like';
import noopActionTransformer from './noop-action-transformer';
import noopStateTransformer from './noop-state-transformer';
import ReadableDataStore, { Filter, Subscriber, Unsubscriber } from './readable-data-store';
import Reducer from './reducer';
import ThunkAction from './thunk-action';
import 'rxjs/add/observable/from';
import 'rxjs/add/observable/merge';
import 'rxjs/add/observable/of';
import 'rxjs/add/observable/throw';
Expand Down Expand Up @@ -65,11 +67,11 @@ export default class DataStore<TState, TAction extends Action = Action, TTransfo
}

dispatch<TDispatchAction extends TAction>(
action: TDispatchAction | Observable<TDispatchAction> | ThunkAction<TDispatchAction, TTransformedState>,
action: DispatchableAction<TDispatchAction, TTransformedState>,
options?: DispatchOptions
): Promise<TTransformedState> {
if (action instanceof Observable) {
return this._dispatchObservableAction(action, options);
if (isObservableActionLike(action)) {
return this._dispatchObservableAction(Observable.from(action), options);
}

if (typeof action === 'function') {
Expand Down
7 changes: 5 additions & 2 deletions src/dispatchable-data-store.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { Observable } from 'rxjs/Observable';
import { SubscribableOrPromise } from 'rxjs/Observable';
import Action from './action';
import ThunkAction from './thunk-action';
import ReadableDataStore from './readable-data-store';

export default interface DispatchableDataStore<TTransformedState, TAction extends Action> extends ReadableDataStore<TTransformedState> {
dispatch: <TDispatchAction extends TAction>(
action: TDispatchAction | Observable<TDispatchAction>,
action: DispatchableAction<TDispatchAction, TTransformedState>,
options?: DispatchOptions
) => Promise<TTransformedState>;
}

export interface DispatchOptions {
queueId?: string;
}

export type DispatchableAction<TAction = Action, TState = any> = TAction | SubscribableOrPromise<TAction> | ThunkAction<TAction, TState>;
20 changes: 20 additions & 0 deletions src/is-observable-action-like.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Observable } from 'rxjs';
import createAction from './create-action';
import isObservableActionLike from './is-observable-action-like';

describe('isObservableActionLike()', () => {
it('returns true if observable action', () => {
expect(isObservableActionLike(Observable.of(createAction('FOOBAR'))))
.toEqual(true);
});

it('returns true if promise-like action', () => {
expect(isObservableActionLike(Promise.resolve(createAction('FOOBAR'))))
.toEqual(true);
});

it('returns false if not observable action or promise-like action', () => {
expect(isObservableActionLike(createAction('FOOBAR')))
.toEqual(false);
});
});
12 changes: 12 additions & 0 deletions src/is-observable-action-like.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Subscribable, SubscribableOrPromise } from 'rxjs/Observable';
import { DispatchableAction } from './dispatchable-data-store';
import Action from './action';

export default function isObservableActionLike<TAction extends Action = Action>(
action: DispatchableAction<TAction>
): action is SubscribableOrPromise<TAction> {
return (
typeof (action as Subscribable<TAction>).subscribe === 'function' ||
typeof (action as PromiseLike<TAction>).then === 'function'
);
}

0 comments on commit 07aaed5

Please sign in to comment.