Skip to content

Instantly share code, notes, and snippets.

@okunokentaro
Last active January 30, 2024 08:38
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okunokentaro/0a2857cdb91722d7eacb6de5dd39228c to your computer and use it in GitHub Desktop.
Save okunokentaro/0a2857cdb91722d7eacb6de5dd39228c to your computer and use it in GitHub Desktop.
Rxチャーハン
import {Observable, Subject} from 'rxjs'
// # 材料
// ## ご飯
class ご飯 {
freeze() {
this.温度 = -10
return this
}
cool() {
this.温度 = 4
return this
}
heat() {
this.温度 = 45
return this
}
}
const 冷やご飯 = new ご飯().cool()
// ## 具
class 刻みネギ {}
class 刻みベーコン {}
const 長ネギ = [new 刻みネギ(), new 刻みネギ(), new 刻みネギ(), new 刻みネギ()]
const ベーコン = [new 刻みベーコン(), new 刻みベーコン(), new 刻みベーコン()]
// ### 卵を実装する
const 黄身 = new Observable((observer) => {
observer.next(18)
observer.complete()
})
const 白身 = new Observable((observer) => {
observer.next(36)
observer.complete()
})
const 卵 = Observable.merge(黄身, 白身)
// ## 調味料
class 調味料 {
constructor(種類) {
this.調味料$ = new Subject()
this.種類 = 種類
}
sprinkle() {
this.調味料$.next(this.種類)
}
prepare() {
return this.調味料$
}
}
const 塩 = new 調味料('塩')
const コショウ = new 調味料('コショウ')
const 醤油 = new 調味料('醤油')
const 鶏ガラスープ = new 調味料('鶏ガラスープ')
// ## 具材を加工する
const 刻まれたネギ = Observable.from(長ネギ)
const 刻まれたベーコン = Observable.from(ベーコン)
const 溶き卵 = 卵.scan((acc, val) => acc + val).last()
const チンご飯 = new Observable((observer) => {
observer.next(冷やご飯)
observer.complete()
}).delay(30 * 1000)
.map((ご飯30秒後) => {
return ご飯30秒後.heat()
})
// ## フライパンを火にかける
const フライパン = Observable
.interval(1000)
.do((v) => {
console.log(`
∧,,∧
(;\`・ω・)  。・゚・⌒) チャーハン作るよ!!
/   o━ヽニニフ))
しー-J
`)
})
.publish()
フライパン.connect()
// ## 炒める
const ごま油 = new 調味料('ごま油')
const チャーハン = フライパン
.merge(ごま油.prepare())
.merge(刻まれたベーコン)
.merge(刻まれたネギ)
.merge(塩.prepare())
.merge(コショウ.prepare())
.merge(溶き卵)
.merge(チンご飯)
.merge(醤油.prepare())
.merge(鶏ガラスープ.prepare())
// たぶん小さじ1はこれくらい
ごま油.sprinkle()
ごま油.sprinkle()
ごま油.sprinkle()
ごま油.sprinkle()
塩.sprinkle()
コショウ.sprinkle()
塩.sprinkle()
コショウ.sprinkle()
醤油.sprinkle()
醤油.sprinkle()
鶏ガラスープ.sprinkle()
塩.sprinkle()
コショウ.sprinkle()
class ぼく {
eat(食べ物) {
食べ物.subscribe(() => {
console.log('おいしい!')
})
}
}
new ぼく().eat(チャーハン)
@okunokentaro
Copy link
Author

npm i rxjs webpack@2.1.0-beta.27 && $(npm bin)/webpack recipe.js chahan.js && node chahan.js

@kt3k
Copy link

kt3k commented Dec 22, 2016

npm i rxjs webpack@2.1.0-beta.27 && wget https://gist.githubusercontent.com/armorik83/0a2857cdb91722d7eacb6de5dd39228c/raw/7b9cc99a838058dc1b99f7e38497ad5156625017/recipe.js && $(npm bin)/webpack recipe.js chahan.js && node chahan.js

@okunokentaro
Copy link
Author

👍

@terukazu-inoue
Copy link

sprinkle() が独立してるので、subscribeの前に終了してしまいますね

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