Skip to content

Commit

Permalink
Merge pull request #46 from edge/master
Browse files Browse the repository at this point in the history
implement rerun
  • Loading branch information
Widdershin committed Mar 13, 2016
2 parents f82a548 + 8daf848 commit 0e5198d
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 82 deletions.
5 changes: 5 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"indent": [2, 2]
}
}
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {run} from '@cycle/core';
import {makeDOMDriver} from '@cycle/dom';
import {makeHTTPDriver} from '@cycle/http';

import {restart, restartable} from 'cycle-restart';
import {rerunner, restartable} from 'cycle-restart';

let app = require('./src/app').default;

Expand All @@ -36,13 +36,13 @@ const drivers = {
HTTP: restartable(makeHTTPDriver())
};

const {sinks, sources} = run(app, drivers);
let rerun = rerunner(run);
rerun(app, drivers);

if (module.hot) {
module.hot.accept('./src/app', () => {
app = require('./src/app').default;

restart(app, drivers, {sinks, sources});
rerun(app, drivers);
});
}
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"devDependencies": {
"@cycle/dom": "^9.0.1",
"@cycle/history": "^1.1.0",
"@cycle/http": "^7.0.0",
"@cycle/http": "^7.1.0",
"@cycle/isolate": "^1.2.0",
"@cycle/jsonp": "^5.0.0",
"babel-cli": "^6.3.15",
Expand Down
18 changes: 17 additions & 1 deletion src/restart.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,23 @@ function restart (main, drivers, {sources, sinks}, isolate = {}, timeToTravelTo
return newSourcesAndSinks;
}

function rerunner (run, isolate) {
let sourcesAndSinks = {};
let first = true;
return function(main, drivers, timeToTravelTo = null) {
if (first) {
sourcesAndSinks = run(main, drivers);
first = false;
}
else {
sourcesAndSinks = restart(main, drivers, sourcesAndSinks, isolate, timeToTravelTo);
}
return sourcesAndSinks;
}
}

module.exports = {
restart,
restartable
restartable,
rerunner
}
2 changes: 1 addition & 1 deletion src/restartable.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export default function restartable (driver, opts = {}) {
let filteredSink$ = sink$;

if (sink$ && pauseSinksWhileReplaying) {
filteredSink$ = sink$.filter(_ => !replaying);
filteredSink$ = sink$.filter(() => !replaying);
}

const source = driver(filteredSink$);
Expand Down
42 changes: 21 additions & 21 deletions test/browser/dom-http-test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@

/* globals describe, it, before, after */
/* globals describe, it*/
import assert from 'assert';
import {run} from '@cycle/core';
import {button, div, input, a, makeDOMDriver} from '@cycle/dom';

const request = require('../../node_modules/@cycle/http/node_modules/superagent');
import {button, div, a, makeDOMDriver} from '@cycle/dom';

let requestCount = 0;
let wikiRequest = 0;

const request = require('../../node_modules/@cycle/http/node_modules/superagent');

const config = [
{
pattern: '/(boo)',

fixtures: function (match, params, headers) {
fixtures: function (match) {
requestCount += 1;

return 'Hello, world! - ' + match[1];
Expand All @@ -26,7 +25,7 @@ const config = [
{
pattern: '/wikipedia/(.*)',

fixtures: function (match, params, headers) {
fixtures: function (match) {
wikiRequest += 1;

return {items: [{full_name: match[1], stargazers_count: 10}]};
Expand All @@ -42,7 +41,7 @@ const superagentMock = require('superagent-mock')(request, config);

import {makeHTTPDriver} from '@cycle/http';

import {restart, restartable} from '../../src/restart';
import {rerunner, restartable} from '../../src/restart';

import {Observable} from 'rx';

Expand All @@ -68,13 +67,13 @@ describe('restarting a cycle app that makes http requests trigged by dom events'

return {
DOM: Observable.just(button('.click', 'Click me!')),
HTTP: click$.map(_ => '/boo'),
HTTP: click$.map(() => '/boo'),
responses$
};
}

it('replays responses', (done) => {
const {container, selector} = makeTestContainer();
const {selector} = makeTestContainer();

const drivers = {
HTTP: restartable(makeHTTPDriver({eager: true})),
Expand All @@ -83,7 +82,8 @@ describe('restarting a cycle app that makes http requests trigged by dom events'

assert.equal(requestCount, 0);

const {sources, sinks} = run(main, drivers);
let rerun = rerunner(run);
const {sinks} = rerun(main, drivers);

setTimeout(() => {
$('.click').click();
Expand All @@ -99,7 +99,7 @@ describe('restarting a cycle app that makes http requests trigged by dom events'
`Expected requestCount to be 1 prior to restart, was ${requestCount}.`
);

const restartedSinks = restart(main, drivers, {sources, sinks}).sinks;
const restartedSinks = rerun(main, drivers).sinks;

restartedSinks.responses$.take(1).subscribe(text => {
assert.equal(text, responseText);
Expand All @@ -111,7 +111,7 @@ describe('restarting a cycle app that makes http requests trigged by dom events'
done();
});
});
}, 50);
}, 100);
});

it('handles more complex apps', (done) => {
Expand All @@ -125,11 +125,11 @@ describe('restarting a cycle app that makes http requests trigged by dom events'
requestCount = 0;
assert.equal(requestCount, 0);

const {sources, sinks} = run(WikipediaSearchBox, drivers);
let rerun = rerunner(run);
const {sinks} = rerun(WikipediaSearchBox, drivers);

setTimeout(() => {
container.find('.search').click()
let responseText;

sinks.results$.skip(1).take(1).subscribe(data => {
assert.equal(data.items[0].full_name, 'woah');
Expand All @@ -139,7 +139,7 @@ describe('restarting a cycle app that makes http requests trigged by dom events'
`Expected requestCount to be 1 prior to restart, was ${wikiRequest}.`
);

const restartedSinks = restart(WikipediaSearchBox, drivers, {sources, sinks}).sinks;
const restartedSinks = rerun(WikipediaSearchBox, drivers).sinks;

restartedSinks.results$.skip(1).take(1).subscribe(newData => {
assert.equal(newData.items[0].full_name, 'woah');
Expand Down Expand Up @@ -167,11 +167,11 @@ describe('restarting a cycle app that makes http requests trigged by dom events'
requestCount = 0;
assert.equal(requestCount, 0);

const {sources, sinks} = run(WikipediaSearchBox, drivers);
let rerun = rerunner(run);
const {sinks} = rerun(WikipediaSearchBox, drivers);

setTimeout(() => {
container.find('.search').click()
let responseText;

sinks.results$.skip(1).take(1).subscribe(data => {
assert.equal(data.items[0].full_name, 'woah');
Expand All @@ -182,7 +182,7 @@ describe('restarting a cycle app that makes http requests trigged by dom events'
);


const restartedSinks = restart(WikipediaSearchBox, drivers, {sources, sinks}).sinks;
const restartedSinks = rerun(WikipediaSearchBox, drivers).sinks;

restartedSinks.results$.skip(1).take(1).subscribe(newData => {
assert.equal(newData.items[0].full_name, 'woah');
Expand All @@ -195,7 +195,7 @@ describe('restarting a cycle app that makes http requests trigged by dom events'
setTimeout(() => {
container.find('.search').click();

restartedSinks.results$.skip(2).take(1).subscribe(newData => {
restartedSinks.results$.skip(2).take(1).subscribe(() => {

assert.equal(
wikiRequest, 2,
Expand Down Expand Up @@ -230,7 +230,7 @@ function WikipediaSearchBox ({DOM, HTTP}) {
.select('.search')
.events('click')
.debounce(100)
.map(ev => 'woah')
.map(() => 'woah')

return {
DOM: results$.map(results =>
Expand Down
31 changes: 16 additions & 15 deletions test/browser/dom-test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/* globals describe, it */
/* globals describe, it*/
import assert from 'assert';
import {run} from '@cycle/core';
import {makeDOMDriver, div, button} from '@cycle/dom';

import {restart, restartable} from '../../src/restart';

import {Observable} from 'rx';
import {rerunner, restartable} from '../../src/restart';

import $ from 'jquery';

Expand All @@ -27,7 +25,7 @@ describe('restarting a cycle app', () => {
const count$ = DOM
.select('.add')
.events('click')
.map(_ => 1)
.map(() => 1)
.scan((total, change) => total + change)
.startWith(0);

Expand All @@ -45,7 +43,7 @@ describe('restarting a cycle app', () => {
const count$ = DOM
.select('.add')
.events('click')
.map(_ => 2)
.map(() => 2)
.scan((total, change) => total + change)
.startWith(0);

Expand All @@ -66,7 +64,8 @@ describe('restarting a cycle app', () => {
DOM: restartable(makeDOMDriver(selector), {pauseSinksWhileReplaying: false})
};

const {sources, sinks} = run(main, drivers);
let rerun = rerunner(run);
rerun(main, drivers);

setTimeout(() => {
container.find('.add').click();
Expand All @@ -75,7 +74,7 @@ describe('restarting a cycle app', () => {

assert.equal(container.find('.count').text(), 3);

restart(newMain, drivers, {sources, sinks});
rerun(newMain, drivers);

setTimeout(() => {
assert.equal(container.find('.count').text(), 6);
Expand All @@ -93,7 +92,8 @@ describe('restarting a cycle app', () => {
DOM: restartable(makeDOMDriver(selector), {pauseSinksWhileReplaying: false})
};

let sourcesAndSinks = run(main, drivers);
let rerun = rerunner(run);
rerun(main, drivers);

assert.equal(container.find('.count').text(), 0);

Expand All @@ -104,7 +104,7 @@ describe('restarting a cycle app', () => {

assert.equal(container.find('.count').text(), 3);

sourcesAndSinks = restart(main, drivers, sourcesAndSinks);
rerun(main, drivers);

setTimeout(() => {
assert.equal(container.find('.count').text(), 3);
Expand All @@ -115,7 +115,7 @@ describe('restarting a cycle app', () => {

assert.equal(container.find('.count').text(), 6);

sourcesAndSinks = restart(main, drivers, sourcesAndSinks);
rerun(main, drivers);

setTimeout(() => {
assert.equal(container.find('.count').text(), 6);
Expand All @@ -136,12 +136,12 @@ describe('restarting a cycle app with multiple streams', () => {
const add$ = DOM
.select('.add')
.events('click')
.map(_ => 1);
.map(() => 1);

const subtract$ = DOM
.select('.subtract')
.events('click')
.map(_ => -1);
.map(() => -1);

const count$ = add$.merge(subtract$)
.scan((total, change) => total + change)
Expand All @@ -162,7 +162,8 @@ describe('restarting a cycle app with multiple streams', () => {
DOM: restartable(makeDOMDriver(selector), {pauseSinksWhileReplaying: false})
};

const {sources} = run(main, drivers);
let rerun = rerunner(run);
rerun(main, drivers);

setTimeout(() => {
container.find('.add').click();
Expand All @@ -177,7 +178,7 @@ describe('restarting a cycle app with multiple streams', () => {

assert.equal(container.find('.count').text(), 0);

restart(main, drivers, {sources});
rerun(main, drivers);

setTimeout(() => {
assert.equal(container.find('.count').text(), 0);
Expand Down
15 changes: 8 additions & 7 deletions test/browser/history-test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/* globals describe, it, before, after */
/* globals describe, it*/
import assert from 'assert';
import {run} from '@cycle/core';
import {makeHistoryDriver} from '@cycle/history';

import {restart, restartable} from '../../src/restart';
import {rerunner, restartable} from '../../src/restart';

import {Observable, Subject} from 'rx';
import {Subject} from 'rx';

const testSubject = new Subject();

describe('restarting a cycle app using a simple driver', () => {
function main ({History}) {
const count$ = testSubject.map(_ => 1).scan((total, change) => total + change);
const count$ = testSubject.map(() => 1).scan((total, change) => total + change);

return {
History: count$.startWith(0).map(count => '/' + count),
Expand All @@ -24,14 +24,15 @@ describe('restarting a cycle app using a simple driver', () => {
History: restartable(makeHistoryDriver(), {pauseSinksWhileReplaying: true})
};

const {sources, sinks} = run(main, drivers);
let rerun = rerunner(run);
const {sinks} = rerun(main, drivers);

sinks.location.skip(1).take(1).subscribe(({pathname}) => {
assert.deepEqual(pathname, '/0');

const newSourcesAndSinks = restart(main, drivers, {sources, sinks});
const newSourcesAndSinks = rerun(main, drivers);

newSourcesAndSinks.sinks.location.skip(1).take(1).subscribe(count => {
newSourcesAndSinks.sinks.location.skip(1).take(1).subscribe(() => {
assert.deepEqual(pathname, '/0');

done();
Expand Down
Loading

0 comments on commit 0e5198d

Please sign in to comment.