Skip to content

Commit

Permalink
Add undocumented action duration method
Browse files Browse the repository at this point in the history
  • Loading branch information
Berkeley Martinez authored and Berkeley Martinez committed Dec 17, 2015
1 parent 55dd0fc commit c2454f0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
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

0 comments on commit c2454f0

Please sign in to comment.