Skip to content

Commit

Permalink
feat: make .run method available everytime
Browse files Browse the repository at this point in the history
.run method is available in both unapplied io and handler

Closes #120
  • Loading branch information
guillaumearm committed Mar 22, 2018
1 parent e56900e commit 3e31b51
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 28 deletions.
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Highly inspired by [funkia/io](https://github.com/funkia/io) and [redux-saga](ht
testHandler(logTwice('hello world'))
.matchIo(log('hello world'))
.matchIo(log('hello world'))
.run()
.run();
```

This piece of code is an assertion, an error will be thrown if something goes wrong:
Expand Down Expand Up @@ -63,12 +63,20 @@ const log = io(console.log);

##### Run IO functions

Calling .run() after applies the io function to its arguments:
Running log with arguments:

```js
log('Hello', 'World').run(); // print Hello World
```

Running log without arguments:

```js
log().run();
// or
log.run();
```

**Keep in mind**: pieces of code using `.run()` cannot be tested properly.

The idea of this library is to apply an **IO** function inside a structure called **handler**.
Expand Down Expand Up @@ -117,14 +125,16 @@ testHandler(addValues())
.matchIo(getEnv('VALUE1'), 32),
.matchIo(getEnv('VALUE2'), 10),
.shouldReturn(42)
.run()
.run();
```

#### Running handlers
Same as for **IO** functions, there is a **.run()** method:

```js
addValues().run() // => 42
addValues().run(); // => 42
// or
addValue.run();
```

Likewise, don't use handlers' **.run()** everywhere in your codebase.
Expand Down Expand Up @@ -153,7 +163,7 @@ const sleepSecond = handler(function*(s) {
testHander(sleepSecond(42))
.matchIo(sleep(42000))
.shouldReturn(42)
.run()
.run();
```

Please note that `sleep(n)` and `sleepSecond(n)` will expose .run() methods that return a promise.
Expand All @@ -163,7 +173,7 @@ Please note that `sleep(n)` and `sleepSecond(n)` will expose .run() methods that
```js
sleepSecond(1).run().then((n) => {
console.log(`${n} second(s) waited`);
})
});
```

### Dealing with errors
Expand Down Expand Up @@ -224,7 +234,7 @@ const ioError = io(() => { throw new Error() });
const myHandler = handler(function*() {
const [res, err] = yield catchError(ioError());
if (err) {
yield log(err)
yield log(err);
}
return res;
})
Expand Down
8 changes: 4 additions & 4 deletions __tests__/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ describe('handle-io/handler', () => {
test('is a function', () => {
expect(typeof defaultHandler).toBe('function');
});
test('has no "run" method', () => {
expect(defaultHandler.run).toBe(undefined);
test('has "run" method', () => {
expect(typeof defaultHandler.run).toBe('function');
})
test('calls run returns undefined', () => {
expect(defaultHandler().run()).toBe(undefined);
Expand All @@ -37,8 +37,8 @@ describe('handle-io/handler', () => {
test('is a function', () => {
expect(typeof listHandler).toBe('function');
});
test('has no "run" method', () => {
expect(listHandler.run).toBe(undefined);
test('has "run" method', () => {
expect(typeof listHandler.run).toBe('function');
})
test('calls run returns [1, 2, 3]', () => {
expect(listHandler(1, 2, 3).run()).toEqual([1, 2, 3]);
Expand Down
10 changes: 5 additions & 5 deletions __tests__/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ describe('handle-io/io', () => {
test('identityIo is a function', () => {
expect(typeof identityIo).toBe('function');
});
test('identityIo has "run" method', () => {
expect(typeof identityIo.run).toBe('function');
})
test('identityIo has "f" equals to identity', () => {
expect(identityIo.f).toBe(identity);
})
test('identityIo has "args" equals to null', () => {
expect(identityIo.args).toBe(null);
})
test('identityIo has no "run" method', () => {
expect(identityIo.run).toBe(undefined);
test('identityIo has "args" equals to empty array', () => {
expect(identityIo.args).toEqual([]);
})
});

Expand Down
10 changes: 4 additions & 6 deletions src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ const runHandler = (runner, gen, genResult) => {
}
}

const createHandler = (ioGen = noopGen, args) => {
const createHandler = (ioGen = noopGen, args = []) => {
const handlerObject = (...args) => createHandler(ioGen, args)
if (args) {
handlerObject.run = (runner = ioRunner) => {
const gen = ioGen(...args)
return runHandler(runner, gen, gen.next());
}
handlerObject.run = (runner = ioRunner) => {
const gen = ioGen(...args)
return runHandler(runner, gen, gen.next());
}
return handlerObject
}
Expand Down
10 changes: 4 additions & 6 deletions src/io.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import ioRunner from './internal/ioRunner';

const createIo = (f, args) => {
const createIo = (f, args = []) => {
const ioObject = (...args) => createIo(f, args)
ioObject.f = f;
ioObject.args = args;
if (args) {
ioObject.run = (runner = ioRunner) => {
return runner(ioObject)
}
ioObject.run = (runner = ioRunner) => {
return runner(ioObject)
}
return ioObject;
}

export default (f) => createIo(f, null);
export default (f) => createIo(f);

0 comments on commit 3e31b51

Please sign in to comment.