https://github.com/fluture-js/fluture-observe
Consume a Future, observing changes to its state
https://github.com/fluture-js/fluture-observe
fluture
Last synced: 7 months ago
JSON representation
Consume a Future, observing changes to its state
- Host: GitHub
- URL: https://github.com/fluture-js/fluture-observe
- Owner: fluture-js
- License: mit
- Created: 2020-03-08T15:28:02.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-04-06T10:38:43.000Z (over 4 years ago)
- Last Synced: 2024-10-31T17:44:54.978Z (about 1 year ago)
- Topics: fluture
- Language: JavaScript
- Homepage:
- Size: 61.5 KB
- Stars: 9
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Fluture Observe
Consume a [Fluture][] Future, observing changes to its state as the
consumption is happening.
## Usage
### Node
```console
$ npm install --save fluture-observe
```
On Node 12 and up, this module can be loaded directly with `import` or
`require`. On Node versions below 12, `require` or the [esm][]-loader can
be used.
### Deno and Modern Browsers
You can load the EcmaScript module from various content delivery networks:
- [Skypack](https://cdn.skypack.dev/fluture-observe@1.1.1)
- [JSPM](https://jspm.dev/fluture-observe@1.1.1)
- [jsDelivr](https://cdn.jsdelivr.net/npm/fluture-observe@1.1.1/+esm)
### Old Browsers and Code Pens
There's a [UMD][] file included in the NPM package, also available via
jsDelivr: https://cdn.jsdelivr.net/npm/fluture-observe@1.1.1/dist/umd.js
This file adds `flutureObserve` to the global scope, or use CommonJS/AMD
when available.
### Usage Example
```js
import {observe, cata} from 'fluture-observe/index.js';
const consume = observe (cata ({
Idle: () => {
console.log ('Computation is idle.');
},
Pending: cancel => {
console.log ('Computation is pending. Send SIGINT to cancel it');
process.once ('SIGINT', cancel);
},
Canceled: future => {
console.log ('Computation was canceled. Send SIGINT to restart it');
process.once ('SIGINT', () => consume (future));
},
Crashed: error => {
console.error ('I am sorry to inform you:', error);
process.exit (1);
},
Rejected: reason => {
console.log ('The computation rejected with reason', reason);
},
Resolved: value => {
console.log ('The computation resolved with value', value);
}
}));
```
## API
A [Daggy][] tagged union type representing the state of the consumption of
a Future. The `Cancel` and `Future` types below are imported
[types from Fluture][].
```hs
data Computation a b = Idle
| Pending Cancel
| Cancelled (Future a b)
| Crashed Error
| Rejected a
| Resolved b
```
Constructor details are documented below.
#### `Idle :: Computation a b`
Represents a not running computation.
#### `Pending :: Cancel -> Computation a b`
Represents a running computation which can be cancelled.
#### `Canceled :: Future a b -> Computation a b`
Represents a computation that was cancelled and can be restarted.
#### `Crashed :: Error -> Computation a b`
Represents a computation which encountered an exception while running.
#### `Rejected :: a -> Computation a b`
Represents a computation which rejected with a reason.
#### `Resolved :: b -> Computation a b`
Represents a computation which resolved with a value.
[Daggy][]'s catamorphism as a curried function.
#### `observe :: (Computation a b -> Any) -> Future a b -> Undefined`
Consume a Future, observing changes to its state. See [usage](#usage).
[Fluture]: https://github.com/fluture-js/Fluture
[Daggy]: https://github.com/fantasyland/daggy
[types from Fluture]: https://github.com/fluture-js/Fluture#types
[esm]: https://github.com/standard-things/esm
[UMD]: https://github.com/umdjs/umd