2.1.0 • Published 8 years ago

@carrd/csp v2.1.0

Weekly downloads
1
License
ISC
Repository
github
Last release
8 years ago

CSP - Communicating Sequential Processes

go flavored channel implementation in JavaScript, using ES7 features async, await.

Example

go ping pong example implemented in JavaScript:

import { Channel, sleep } from '@carrd/csp'

async function pinger (chan) {
  while (!chan.closed) {
    await chan.push('ping')
  }
}

async function ponger (chan) {
  while (!chan.closed) {
    await chan.push('pong')
  }
}

async function printer (chan) {
  while (!chan.closed) {
    console.log(await chan.pull())
    await sleep(1000)
  }
}

async function main () {
  const chan = Channel()

  pinger(chan)
  ponger(chan)
  printer(chan)
}

main()

Channel

const chan = Channel(options)
options(chanel)
  • buffer (default 0) - channels can accept a number of values without blocking and waiting for a receiver, when the buffer limit is reached operations like push() will be suspended until a pull() will occur
async chan.push(data)

Push a value into a channel, suspending the function until a pull occurs if the channel buffer limit is reached.

async chan.pull()

Pull a value from the channel, suspending the function until a push occurs if no values were buffered.

chan.close()

Closes the channel and resumes all suspended operations, passing a null value, resulting in chan.closed == true

chan.clear()

Resumes all suspended operations on the channel, passing a null value

chan.toString()

Returns a string value [Channel] or [Channel name] if channel has a name.

chan.closed

Return a boolean that indicates if the channel is closed.

If you try to send() or receive() data from a closed channel an Error will be thrown.

chan.length

The length of the buffered data on the channel.

chan.type

Same output as chan.toString()

chan.name

Channel name set using Channel({ name: 'value' })

Utilities

sleep(milliseconds)

Perform a timeout for a number of milliseconds.

import { sleep } from '@carrd/csp'

async function main () {
  // sleep for 3 seconds
  await sleep(3000) 
}

main()
merge(channel, channel)

Merge multiple channels into one:

import { Channel, merge } from '@carrd/csp'

const source1 = Channel()
const source2 = Channel()
const source3 = Channel()

async function main () {
  const chan = merge(source1, source2, source3)

  while (!chan.closed) {
    let data = await chan.pull()
  }
}

main()
await select(channel, channel)

The select function selects which channel replies first and returns an array containing the channel and a value passed from that channel:

import { Channel, select } from '@carrd/csp'

const source1 = Channel()
const source2 = Channel()

async function main () {
  let [chan, data] = await select(source1, source2)

  if (chan == source1) {
    // le code 
  }

  if (chan == source2) {
    // le code 
  }
}

main()

By default select does a .pull() operation, if you want to do a .push() operation pass an array select([channel, data]).

const chan1 = Channel()
const chan2 = Channel()
const chan3 = Channel()

let [chan, data] = await select([chan1, '1'], chan2, [chan3, '3'])

// In the example above select(...) will perform:
// await chan1.push('1')
// await chan2.pull()
// await chan3.push('3')
// and return the result from the channel that will reply first

License ISC

2.1.0

8 years ago

2.0.0

8 years ago

1.3.2

8 years ago

1.3.1

8 years ago

1.3.0

8 years ago

1.1.0

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago