1 |
|
2 | import { chan, go, put, close, take, sleep, repeat, repeatTake, CLOSED } from '../lib/index'
|
3 |
|
4 | let player = async (name, table) => {
|
5 | repeatTake(table, async (ball) => {
|
6 | if (ball === CLOSED) {
|
7 | console.log(`${name} is closed!`)
|
8 | return false
|
9 | }
|
10 | ball.hits++
|
11 | console.log(name, ball.hits)
|
12 | await sleep(100)
|
13 | put(table, ball)
|
14 | })
|
15 | }
|
16 |
|
17 | go(async () => {
|
18 | let table = chan()
|
19 | player('ping', table)
|
20 | player('pong', table)
|
21 | put(table, { hits: 0 })
|
22 | await sleep(1000)
|
23 | close(table)
|
24 | })
|