This repository has been archived by the owner on Dec 12, 2022. It is now read-only.
forked from paldepind/flyd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementing error handling based on discussion in issue paldepind#35
- Loading branch information
1 parent
3571576
commit f264be8
Showing
5 changed files
with
516 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,54 @@ | ||
# Errors in flyd | ||
`Kefir` handles streams by seeing it as values wrapped in an event description. These descriptions partition events into `value` and `error`, and `Kefir`'s API is written with that in mind. | ||
|
||
The goal of this is to determine how to bring functionality similar to that into `flyd`. | ||
|
||
## Ethos | ||
* It should not be the concern of `flyd` to handle exceptions for the user -- any `throw` should result in a hard failure. | ||
* Silent failures are bad (current way `flyd` handles Promise.reject) | ||
* API must follow [fantasty-land](https://github.com/fantasyland/fantasy-land) | ||
* Unopinionated implementation as possible | ||
|
||
+ It should not be the concern of `flyd` to handle exceptions for the user -- any `throw` should result in a hard failure. | ||
+ Silent failures are bad (current way `flyd` handles Promise.reject) | ||
+ Be as unopinionated in implementation as possible | ||
+ Be functional in design | ||
+ Be as backward compatible as possible with the current api | ||
|
||
## Concepts | ||
+ The stream is of `events` | ||
+ Each stream has a `left` and a `right` side | ||
+ Each stream has a `left` and a `right` side (like an Either) | ||
+ The right side is the domain objects | ||
+ The left side is meta in our case errors | ||
+ Keep the api by default operating on `right` | ||
+ The left side is meta (in most cases errors) | ||
+ By default the api operates on the `right` side | ||
|
||
## The Api | ||
`s` is a stream | ||
|
||
### Setting data s(...) is overloaded | ||
+ `s(value)` alias to `s.right(value)` is the default case takes a value makes it a right and pushes it down the stream | ||
+ `s(value)` is the default case takes a value makes it a right and pushes it down the stream | ||
+ `s(promise)` if the promise resolves pushes a right, otherwise pushes a left | ||
+ `s(either)` pushes a right or left based on either.left either.right (maybe an internal `isEither` function) | ||
+ *new `s.left(value)` sets the stream to a left of `value` | ||
+ `s(either)` pushes down right or left based on either.left either.right | ||
+ `s.left(value)` sets the stream to a left of `value` | ||
|
||
### Getting data | ||
+ `s()` alias to `s.right()` get the last right value or throws an exception if there is a left value | ||
+ *new `s.left()` get the last left value or throws an exception if there is a right value | ||
+ *new `s.isRight()` and `s.isLeft()` return boolean so you know what the stream contains | ||
+ `s()` get the last right value or throws an exception if there is a left value | ||
+ `s.left()` get the last left value or throws an exception if there is a right value | ||
|
||
### Checking stream state | ||
+ `s.isLeft()` return boolean so you know what the stream contains | ||
|
||
### Core functions | ||
+ `.map()` works only on rights and ignores lefts | ||
+ *new `.mapAll()` gets all events as an `Either` or some other lightweight type defining `lefts` and `rights` | ||
+ `.mapAll()` gets all events as an `Either` | ||
+ `.combine()` and `.merge()` stay the same they work on streams | ||
+ `.scan()` needs more thought | ||
+ `ap()` works on `rights` only | ||
+ `.scan()` works on `rights` only | ||
+ `.on()` works on `rights` only | ||
|
||
### Move `.transduce()` to a module | ||
|
||
### Add some helper modules | ||
+ `.onAll()` | ||
+ `.mapLeft()` | ||
+ `.swap()` swaps `rights` and `lefts` - might want this in core for performance reasons | ||
+ others ... | ||
### The Either implementation | ||
There are no additional dependencies and we have provided a minimal implementation for basic use. If you plan on using `.mapAll` we recommend overriding the methods in flyd.Either. You can use [folktale/data.either](https://github.com/folktale/data.either) for example as shown below. | ||
``` | ||
var DE = require('data.either'); | ||
flyd.Either.Right = DE.Right; | ||
flyd.Either.Left = DE.Left; | ||
flyd.Either.isEither = function(obj) { return obj instanceof DE; }; | ||
flyd.Either.isRight = function(e) { return e.isRight; }; | ||
flyd.Either.getRight = function(e) { return e.value; }; | ||
flyd.Either.getLeft = function(e) { return e.value; }; | ||
``` | ||
|
||
### Other functionality | ||
Keeping with the ethos of flyd any further functions like `.swap` or `.onAll` should be implemented as modules. |
Oops, something went wrong.