Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jviide/flowponent

A small Preact/React library for defining workflow-like evolving views via generators
https://github.com/jviide/flowponent

generators higher-order-component preact react

Last synced: about 1 month ago
JSON representation

A small Preact/React library for defining workflow-like evolving views via generators

Awesome Lists containing this project

README

        

# flowponent [![npm](https://img.shields.io/npm/v/flowponent.svg)](https://www.npmjs.com/package/flowponent)

A small library for [Preact 10.x](https://preactjs.com/) (and [React](https://reactjs.org/) - see [the note](#a-note-on-react-support)) for defining workflow-like evolving views via generators.

Here's the canonical counter example (also available [at Codesandbox](https://codesandbox.io/s/flowponent-in-action-ebfq2)):

```js
import { render } from "preact";
import flowponent from "flowponent";

const App = flowponent(function*() {
let count = 0;

for (;;) {
count += yield resolve => (


current value: {count}

resolve(1)}>+1
resolve(-1)}>-1

);
}
});

render(, document.getElementById("root"));
```

For a more involved one see [here's a Codesandbox](https://codesandbox.io/s/flowponent-in-action-88vb9) demonstrating composition, additional props & cleanups upon unmount:

[![A Codesandbox sandbox demonstrating flowponent](https://user-images.githubusercontent.com/19776768/70826521-d10e8380-1def-11ea-82fd-0004f1caa6fc.png)](https://codesandbox.io/s/flowponent-in-action-88vb9)

## Installation

```sh
$ npm install --save flowponent
```

### A Note on React Support

The use flowponent with React, import from `"flowponent/react"` instead of `"flowponent"`:

```js
import React from "react";
import { render } from "react-dom";
import flowponent from "flowponent/react";
```

Otherwise it all works the same way as before. Here's a React example featuring a random selection of very good dogs: [Codesandbox](https://codesandbox.io/s/flowponentreact-in-action-8q16y).

## Error Propagation

If an error is throw inside the yielded functions it can be caught in the flowponent.

The function can also fail after it's result has been rendered by calling its second parameter (`reject`):

```js
const App = flowponent(function*() {
try {
const data = yield (resolve, reject) => (

);
yield () =>

Download succeeded: {data}


} catch (err) {
yield () =>

Download failed: {err.message}

;
}
});
```

## Async Mode

Async mode is activated by using async generators instead of regular ones. In fact, regular flowponents are actually just a special case of async ones! Here's the first example implemented with async flowponents:

```js
const App = flowponent(async function*() {
let count = 0;

for (;;) {
const promise = yield resolve => (


current value: {count}

resolve(1)}>+1
resolve(-1)}>-1

);
count += await promise;
}
});
```

This allows doing things between the time that the view has been rendered and `resolve` has been called.

Check out a bit more involved downloader widget example: [Codesandbox](https://codesandbox.io/s/flowponent-async-mode-in-action-97wk1)

## See Also

- The [tweet with the initial idea](https://twitter.com/jviide/status/1204492830594473985), but still using async generators.
- For a more sophisticated approach check out [`concur-js`](https://github.com/ajnsit/concur-js) from which this library ~~stole~~borrowed further ideas 🙂

## License

This library is licensed under the MIT license. See [LICENSE](./LICENSE).