https://github.com/yamiteru/stoic
Tiny, fast and simple TypeScript functional reactive library.
https://github.com/yamiteru/stoic
event-driven functional-reactive-programming reactive reactive-programming reactive-streams typescript
Last synced: 5 months ago
JSON representation
Tiny, fast and simple TypeScript functional reactive library.
- Host: GitHub
- URL: https://github.com/yamiteru/stoic
- Owner: yamiteru
- License: mit
- Created: 2021-08-04T13:49:12.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2023-10-21T04:56:45.000Z (over 1 year ago)
- Last Synced: 2023-10-21T05:27:00.932Z (over 1 year ago)
- Topics: event-driven, functional-reactive-programming, reactive, reactive-programming, reactive-streams, typescript
- Language: TypeScript
- Homepage:
- Size: 263 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Stoic
Tiny, fast and simple TypeScript functional reactive library.## Concepts
### Observable
Something that emits values over time to which you can listen to. It starts its life once it's created and ends either by `error` or `end`.There are 3 types of events to which you can listen to:
1. `onNext` listens to new values
2. `onError` listens to errors
3. `onEnd` listens to the end of the observableThere are 2 core types of observables:
1. `stream` doesn't remember the latest value
2. `value` does remember the latest value### Value mapping
Every observable has some sort of input and output value. Their shapes can be completely different and for that there is a map.It accepts input value and outputs either output value or undefined. Undefined is used to stop the propagation of the output value to subscribers.
### Derivation
Each observable type has its derived version. Derived observables derive their input value from an array of output values of other observables.Derived observables are updated internally by an array of observables so there is no `next` method for manually passing input values into them.
___
## API
### Observables
#### Stream
``` typescript
const simpleLogger$ = stream<[type: string, message: any]>(null, {
next: ([type, message]) => console.log(type.toUpperCase(), message)
});
```#### derivedStream
``` typescript
const squidStats$ = derivedStream([players$, reward$], ([players, reward]) => [
`${players.length} 염`,
`₩ ${reward.toWon()}`
]);
```#### value
``` typescript
const price$ = value(0, isPositive);
const count$ = value(0, isPositive);
```#### derivedValue
``` typescript
const sum$ = derivedValue([price$, count$], ([price, count]) => price * count);
```___
## Future
- Tests
- Benchmarks
- `pipe` (once it's possible to define types that would support an **unlimited** number of functions)
- React hooks___
Have a beautiful day 🍀