-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Passing a channel as an argument valid? #2
Comments
Hi @loganpowell , You have a few deadlocks here Deadlock 1Description
SolutionOne way of fixing that issue (though not idiomatic of how CSP is supposed to be used) is to not wait for a process to take a brick from the co(function *() {
_I_.put("Clojure")
yield namer(_I_, _O_)
const fancy_name = yield _O_.take()
console.log("hello", fancy_name)
}) However, even with this, you'll run into the second deadlock. Deadlock 2DescriptionThe SolutionOne way of fixing that issue (though not idiomatic of how CSP is supposed to be used) is to not wait for taking a brick from the const namer = (_I_, _O_) => co(function *() {
const name = yield _I_.take() // This waits until a first brick is added on the _I_ channel.
const fancy_name = `${name}izer`
_O_.put(fancy_name)
}) Combined SolutionsIf we put those 2 solutions together, the code looks like this: const co = require('co')
const { Channel } = require("core-async")
const namer = (_I_, _O_) => co(function *() {
const name = yield _I_.take() // This waits until a first brick is added on the _I_ channel.
const fancy_name = `${name}izer`
_O_.put(fancy_name) // REMOVING DEADLOCK 2
})
const _I_ = new Channel()
const _O_ = new Channel()
co(function *() {
_I_.put("Clojure") // REMOVING DEADLOCK 1
yield namer(_I_, _O_)
const fancy_name = yield _O_.take()
console.log("hello", fancy_name)
}) Better ApproachHowever, as I briefly mentioned before, this is not idiomatic of CSP. Indeed, what if the Your original const co = require('co')
const { Channel } = require("core-async")
// This is your original func
const namer = (_I_, _O_) => co(function *() {
const name = yield _I_.take() // This waits until a first brick is added on the _I_ channel.
const fancy_name = `${name}izer`
yield _O_.put(fancy_name) // This waits until a first request for a brick is emitted on the _O_ channel.
})
const _I_ = new Channel()
const _O_ = new Channel()
// 'namer' does stuff asynchronously
co(function *() {
console.log(`Doing some work before 'namer' starts`)
yield namer(_I_, _O_)
console.log(`Doing some more work now that 'namer' is done`)
})
// Asynchronously do things each time a brick is added to the _O_ channel.
co(function *() {
const fancy_name = yield _O_.take()
console.log("hello", fancy_name)
})
// Asynchronously do things each time a brick is taken from the _I_ channel.
co(function *() {
yield _I_.put("Clojure") //?
console.log(`Great, somebody took the 'Clojure' brick. I can do other awesome stuff now.`)
}) If you're craving for more Clojure stuff in JS (e.g., combining CSP patterns with transmuters), then a more mature library is js-csp. The only reason I built I hope my explanation shed some light on your problem. Cheers, Nic |
Dude, just awesome all around. |
Hey man! Just now getting a chance to play with this. Wondering if I'm just doing something wrong, but the way I usually use channels are as arguments to an async function. Is something like this possible?
The text was updated successfully, but these errors were encountered: