Skip to content

Instantly share code, notes, and snippets.

@ENvironmentSet
Last active October 29, 2018 09:22
Show Gist options
  • Save ENvironmentSet/8d9a2bb7fd14ea388716778b74994ed2 to your computer and use it in GitHub Desktop.
Save ENvironmentSet/8d9a2bb7fd14ea388716778b74994ed2 to your computer and use it in GitHub Desktop.
callbag-basic korean readme

Origin

Callbag basics ๐Ÿ‘œ

๊ธฐ๋ณธ์ ์ธ callbag ํŒฉํ† ๋ฆฌ์™€ ์˜คํผ๋ ˆ์ดํ„ฐ๋“ค์„ ์ œ๊ณตํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค.

callbag-basics ์€ callbag ์ด๋ผ๋Š” ๋ช…์„ธ๋ฅผ ๋”ฐ๋ฅด๋Š” ํ•จ์ˆ˜๋“ค์„ ๊ตฌํ˜„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์š”.

Callbag ์€ ๋ง์ด์ฃ ..:

  • ์ŠคํŠธ๋ฆผ์„ ํ†ตํ•œ ๋ฆฌ์•กํ‹ฐ๋ธŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์ง€์›ํ•ด์š”.
  • Iterable ์„ ์ด์šฉํ•œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์ง€์›ํ•ด์š”. (์–˜๋Š” ๋ค์ด์—์š”!)
  • ๋ชจ๋“  ์˜คํผ๋ ˆ์ดํ„ฐ๊ฐ€ ์œ„ ๋‘๊ฐ€์ง€ ์‚ฌํ•ญ์„ ์ง€์›ํ•ด์š”.
  • ์ž‘์•„์š”! ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ํฌ๊ธฐ๋Š” ๊ณ ์ž‘ 7kB ์—์š”.
  • ๋นจ๋ผ์š”! xstream ๊ณผ RxJS ๋ณด๋‹ค ๋นจ๋ผ์š”.
  • ํ™•์žฅ ๊ฐ€๋Šฅ์„ฑ: ์ฝ”์–ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์—†์–ด์š”! ๋ชจ๋“  ๊ฒŒ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜์—์š”!

Observable ๊ณผ (๋น„๋™๊ธฐ)Iterable์ด ํ•ฉ์ณ์ง€๋Š” ๊ฑธ ์ƒ์ƒํ•ด๋ณด์„ธ์š”. ๊ทธ๊ฒŒ ๋ฐ”๋กœ callbag ์ด์—์š”. ์•„, ๊ทธ๋ฆฌ๊ณ  ํ•˜๋‚˜ ๋”!, callbag ์˜ ๋‚ด๋ถ€๋Š” ์ •๋ง ์ž‘์„ ์ˆ˜ ๋ฐ–์— ์—†์–ด์š”. ์™œ๋ƒํ•˜๋ฉด ๋ชจ๋“  ๊ฑธ ๋ช‡ ๊ฐœ์˜ ๊ฐ„๋‹จํ•œ ์ฝœ๋ฐฑ์œผ๋กœ ํ•ด๋‚ด๊ธฐ ๋•Œ๋ฌธ์ด์ฃ ! ๊ฒฐ๊ณผ์ ์œผ๋กœ callbag ๋ช…์„ธ๋ฅผ ๋”ฐ๋ผ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•˜๋Š” ๊ฒƒ์€ ์ž‘๊ณ  ๋น ๋ฅธ ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“œ๋Š” ๋ฐ ๋„์›€์ด ๋˜์š”.

์‚ฌ์šฉ๋ฒ•

npm install callbag-basics

ํŒฉํ† ๋ฆฌ์™€ ์˜คํผ๋ ˆ์ดํ„ฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ถˆ๋Ÿฌ์™€์š”:

const {forEach, fromIter, map, filter, pipe} = require('callbag-basics');

์˜จ๋ผ์ธ์—์„œ ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

๋ฆฌ์•กํ‹ฐ๋ธŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์˜ˆ์ œ

<button> ์—˜๋ฆฌ๋จผํŠธ์˜ ํด๋ฆญ ์ด๋ฒคํŠธ์—์„œ XY ์ขŒํ‘œ๋ฅผ ๋กœ๊น…ํ•˜๊ธฐ:

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}
// ...

๋งค ์ดˆ๋งˆ๋‹ค ํ•˜๋‚˜์˜ ์ž์—ฐ์ˆ˜๋ฅผ ์—ฐ์‚ฐํ•˜์—ฌ, ์ด 5๊ฐœ์˜ ๊ฐ€์žฅ ์ž‘์€ ํ™€์ˆ˜๋ฅผ ์–ป๊ธฐ:

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 ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์˜ˆ์ œ

์ˆซ์ž๋“ค์˜ ์—ด์—์„œ, 5๊ฐœ์˜ ์ˆซ์ž๋ฅผ ๋ฝ‘๊ณ , ๋ฝ‘์€ ์ˆซ์ž๋“ค์„ 4๋กœ ๋‚˜๋ˆˆ ํ›„ ํ•˜๋‚˜์”ฉ ์ถœ๋ ฅํ•˜๊ธฐ:

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

API

์•„๋ž˜์˜ ๋ฆฌ์ŠคํŠธ์— ์žˆ๋Š” ํ•จ์ˆ˜๋“ค์„ callbag-basics๊ฐ€ ์ œ๊ณตํ•˜๊ณ  ์žˆ์–ด์š”.

Source ํŒฉํ† ๋ฆฌ

Sink ํŒฉํ† ๋ฆฌ

๋ณ€ํ™˜ ์˜คํผ๋ ˆ์ดํ„ฐ

ํ•„ํ„ฐ ์˜คํผ๋ ˆ์ดํ„ฐ

๋ณ‘ํ•ฉ ์˜คํผ๋ ˆ์ดํ„ฐ

์œ ํ‹ธ๋ฆฌํ‹ฐ

๊ทธ๋ฆฌ๊ณ  ๋”!

์šฉ์–ด์‚ฌ์ „

  • source: ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐœ์†กํ•˜๋Š” callbag
  • sink: ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์‹ ํ•˜๋Š” callbag
  • puller sink: source ์—๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐœ์†กํ•˜๊ธฐ๋ฅผ ์š”๊ตฌํ•˜๋Š” sink
  • pullable source: ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐœ์†กํ•˜๊ธฐ๋ฅผ ์š”๊ตฌ๋ฐ›์•„์•ผ์ง€๋งŒ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐœ์†กํ•˜๋Š” source
  • listener sink: ์ˆ˜๋™์ ์œผ๋กœ source ๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์‹ ํ•˜๋Š” sink
  • listenable source: ์ž๋ฐœ์ ์œผ๋กœ sink ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐœ์†กํ•˜๋Š” source
  • operator: ๋‹ค๋ฅธ callbag ์— ํŠน์ • ์—ฐ์‚ฐ์„ ์ ์šฉํ•˜๋Š” callbag

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! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment