Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add undocumented action duration method #133

Merged
merged 1 commit into from
Dec 17, 2015
Merged
Show file tree
Hide file tree
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
21 changes: 21 additions & 0 deletions src/Actions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import stampit from 'stampit';
import warning from 'warning';
import debugFactory from 'debug';
import {
Observable,
Expand All @@ -10,6 +11,7 @@ import {

import waitFor from './waitFor';

const __DEV__ = process.env.NODE_ENV !== 'production';
const { checkDisposed } = Disposable;
const assign = Object.assign;
const debug = debugFactory('thundercats:actions');
Expand Down Expand Up @@ -50,6 +52,7 @@ export function create(shouldBind, { name, map }) {
const observers = [];
const actionDisposable = new CompositeDisposable();
const actionStart = new Subject();
const actionEnd = new Subject();
const maybeBound = shouldBind ?
map.bind(this) :
map;
Expand Down Expand Up @@ -81,6 +84,9 @@ export function create(shouldBind, { name, map }) {
.doOnNext(
value => observers.forEach(observer => observer.onNext(value))
)
.doOnCompleted(
() => actionEnd.onNext()
)
.subscribe(
() => debug('%s onNext', name),
err => {
Expand Down Expand Up @@ -123,10 +129,25 @@ export function create(shouldBind, { name, map }) {
};

action.waitFor = function() {
/* istanbul ignore else */
if (__DEV__) {
warning(
false,
'action.waitFor is deprecated and will be removed in ' +
'the next version of thundercats'
);
}
return actionStart
.flatMap(payload => waitFor(...arguments).map(() => payload));
};

// NOTE: not public API. May change or be removed at any time
action.__duration = function __duration() {
return actionStart
.flatMap(actionEnd)
.first();
};

action._subscribe = function subscribeToAction(observer) {
// in next major version this should check if action
// has been stopped or disposed and act accordingly
Expand Down
35 changes: 35 additions & 0 deletions test/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,41 @@ describe('Actions', function() {
});
});

describe('internal lifecycle hooks', function() {
it('should have a duration observable', () => {
const catActions = Actions({ purr: null })();
assert(
!!catActions.purr.__duration,
'actions should have a __durations property'
);
});

it('should on call __duration once per action', () => {
const spy = sinon.spy();
const spy2 = sinon.spy();
const catActions = Actions({ purr: null })();
catActions.purr.__duration().subscribe(spy);
catActions.purr(Observable.of(1, 2, 3));
assert(
spy.calledOnce,
'duration Observer was called more than once'
);

catActions.purr.__duration().subscribe(spy2);
catActions.purr(Observable.of(1, 2, 3));

assert(
spy.calledOnce,
'duration Observable spy was called more than once'
);

assert(
spy2.calledOnce,
'duration Observable spy was called more than once'
);
});
});

describe('disposal', function() {
let catActions;

Expand Down