

GitHub - staltz/callbag-basics: Basic Callbag factories and operators to get sta...
source link: https://github.com/staltz/callbag-basics
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Callbag basics 👜
Basic callbag factories and operators to get started with. Callbag is just a spec, but callbag-basics
is a real library you can use.
Highlights:
- Supports reactive stream programming
- Supports iterable programming (also!)
- Same operator works for both of the above
- Tiny! Weighs just 7kB
- Fast! Faster than xstream and RxJS
- Extensible: no core library! Everything is a utility function
Imagine a hybrid between an Observable and an (Async)Iterable, that's what callbags are all about. In addition, the internals are tiny because it's all done with a few simple callbacks, following the callbag spec. As a result, it's tiny and fast.
Usage
npm install callbag-basics
Import operators and factories:
const {forEach, fromIter, map, filter, pipe} = require('callbag-basics');
Try it online
Reactive programming examples
Log XY coordinates of click events on <button>
elements:
const {forEach, fromEvent, map, filter, pipe} = require('callbag-basics'); pipe( fromEvent(document, 'click'), filter(ev => ev.target.tagName === 'BUTTON'), map(ev => ({x: ev.clientX, y: ev.clientY})), forEach(coords => console.log(coords)) ); // {x: 110, y: 581} // {x: 295, y: 1128} // ...
Pick the first 5 odd numbers from a clock that ticks every second, then start observing them:
const {forEach, interval, map, filter, take, pipe} = require('callbag-basics'); pipe( interval(1000), map(x => x + 1), filter(x => x % 2), take(5), forEach(x => console.log(x)) ); // 1 // 3 // 5 // 7 // 9
Iterable programming examples
From a range of numbers, pick 5 of them and divide them by 4, then start pulling those one by one:
const {forEach, fromIter, take, map, pipe} = require('callbag-basics'); function* range(from, to) { let i = from; while (i <= to) { yield i; i++; } } pipe( fromIter(range(40, 99)), // 40, 41, 42, 43, 44, 45, 46, ... take(5), // 40, 41, 42, 43, 44 map(x => x / 4), // 10, 10.25, 10.5, 10.75, 11 forEach(x => console.log(x)) ); // 10 // 10.25 // 10.5 // 10.75 // 11
The list below shows what's included.
Source factories
Sink factories
Transformation operators
Filtering operators
Combination operators
Utilities
Terminology
- source: a callbag that delivers data
- sink: a callbag that receives data
- puller sink: a sink that actively requests data from the source
- pullable source: a source that delivers data only on demand (on receiving a request)
- listener sink: a sink that passively receives data from the source
- listenable source: source which sends data to the sink without waiting for requests
- operator: a callbag based on another callbag which applies some operation
Contributing
The Callbag philosophy is: build it yourself. :) You can send pull requests, but even better, don't depend on the repo owner accepting it. Just fork the project, customize it as you wish, and publish your fork on npm. As long as it follows the callbag spec, everything will be interoperable! :)
Recommend
-
119
push 和 pull 模型 如果你了解 RxJs,在响应式编程中,Observable 和 Obsever 是 push 模型,与之对应的,还有一个 pull 模型: Pull(f(): B):返回一个值。 Push(f(x: A): void):响应式
-
86
GitHub is where people build software. More than 28 million people use GitHub to discover, fork, and contribute to over 80 million projects.
-
77
readme.md zii Chain function calls using a prototype function .z() Adds a function z to the Object pr...
-
85
readme.md Profunctor State Hook React Hook for state management with Profunctor Optics A simple and small (2KB!) approach to state mana...
-
65
README.md prevent-global-this The purpose of this library is to prevent TC39 from adding globalThis as a keyword to JavaScript, according...
-
32
The cornerstone of JavaScript is the function. It is a flexible abstraction that works as the basis for other abstractions, such as Promises, Iterables, Observables, and others. I have been teaching these concepts in conf...
-
64
Learning SQL can feel intimidating, even to some experienced DB2 DBAs. For years, you could administer Db2 without using SQL. There are a lot of developers, too who want to learn SQL. The set-thinking of SQL can be confus...
-
8
Ruby Basics - Equality operators in Ruby After Greg Sterndale’s presentation on a boston-rb hackfest earlier this month I noticed...
-
5
André Staltz A plan to rescue the Web from the Internet 18 Dec 2017 My previous article,
-
11
Doing math in JavaScript is a prevalent task. We often want to perform some sort of calculation on one or two numbers. This is where arithmetic operators come in! Let's see which ones we can use in JavaScript: Operator...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK