dvlsg / async-csp

CSP style channels using ES7 async/await
MIT License
317 stars 18 forks source link

Default size to zero. #2

Closed trusktr closed 8 years ago

trusktr commented 8 years ago

Opening this thread separately, regarding https://github.com/dvlsg/async-csp/issues/1#issuecomment-154479943.

If we set the Channel.DEFAULT_SIZE to zero, it doesn't seem like it'll work at all right now because the queue can never be empty, which the code relies on at the moment? Seems like the FixedQueue structure could be used for when size >= 1, but a separate mechanism for when size is zero?

dvlsg commented 8 years ago

Hm. One question for you first: in the following code, how would you expect this to work (or not work) when default size is zero (assume execution is in an async context):

let ch = new Channel();
await ch.put(1);
let val = await ch.take();

Would you expect the ch.take() to never execute, since ch.put() would block until a take is executed?

dvlsg commented 8 years ago

After poking around with the code for a while, I feel that the best way to accomplish that functionality would be to work on a refactor which would allow buffer-less channels.

Right now, slide() is looking to see if space on the buffer is available before resolving the put, but in order to accomplish blocking on the first put when no buffer is in use, the logic would need to wait until at least one take is available instead.

A lot of the code is currently designed around interacting with the buffer between puts/takes to lazily run the transform functions, so that would all have to be reworked. Very possible, but not an insignificant amount of work.

trusktr commented 8 years ago
let ch = new Channel();
await ch.put(1);
let val = await ch.take();

In that case it just wouldn't work. The reference to ch would have to be created outside of the function, and another process would have to call ch.take() on it in order to make this process resume, and then this process would pause at let val = await ch.take() until another process calls ch.put.

Let's make sure the docs mention that this won't be possible. :+1:

dvlsg commented 8 years ago

I just pushed the refactor to the develop branch, if you were interested in taking a look. There is still lots to do (performance, tests, docs, examples, etc), but the functionality should all be there, with non-buffered channels as the new default.

dvlsg commented 8 years ago

Landed in v0.3.0.

trusktr commented 8 years ago

Looks awesome!! Thanks for the update. This is one of my new favorite libs right now. :smiley:

dvlsg commented 8 years ago

No problem! It was a good suggestion. And I'm glad someone other than myself has a use for the code. :+1: