我想做的是有一组生产者goroutines(其中一些可能会或可能不会完成)和一个消费者例程.问题在于括号中的警告 - 我们不知道将返回答案的总数.
所以我想做的是:
package main
import (
"fmt"
"math/rand"
)
func producer(c chan int) {
// May or may not produce.
success := rand.Float32() > 0.5
if success {
c <- rand.Int()
}
}
func main() {
c := make(chan int, 10)
for i := 0; i < 10; i++ {
go producer(c, signal)
}
// If we include a close, then that's WRONG. Chan will be closed
// but a producer will try to write to it. Runtime …Run Code Online (Sandbox Code Playgroud) 我正在研究Javascript库的事件处理代码,我正在尝试实现类似于stopImmediatePropagation()的东西,它也适用于IE 6.
事件处理当前的工作方式是,我们的事件处理代码向对象注册,然后用户使用我们的事件处理程序注册所有事件.
我尝试模拟stopImmediatePropagation()的第一种方法是简单地将该方法添加到事件中(如果它尚不存在):
if (event != null && event.isImmediatePropagationEnabled == null) {
event.stopImmediatePropagation = function () {
this.isImmediatePropagationEnabled = false;
};
event.isImmediatePropagationEnabled = true;
event.isImmediatePropagationStopped = function () {
return !this.isImmediatePropagationEnabled;
};
}
Run Code Online (Sandbox Code Playgroud)
当调用用户的事件处理程序回调时,它们将在我们获得的同一事件对象中传递.如果他们愿意,他们可以在事件上调用stopImmediatePropagation().
当我们循环遍历在我们注册的所有事件处理回调时,我们每次检查传播布尔值:
given some event
for [all the callbacks] {
if (event != null &&
event.isImmediatePropagationStopped != null &&
event.isImmediatePropagationStopped()) {
stopPropagation = true;
break;
}
execute the callback, passing in the event
}
Run Code Online (Sandbox Code Playgroud)
这在某些浏览器中效果很好.因为事件仍然存在,即使我们的事件处理代码退出并且事件冒泡到下一个元素,一旦我们的事件处理代码再次被命中,isImmediatePropagationStopped属性仍然存在,因此不再执行回调(已向我们注册) .
在Internet Explorer(即使是8)中,这不起作用.在同一个元素上,事情很好; 但是一旦事件冒泡,就好像生成了一个全新的事件对象,我们失去了isImmediatePropagationStopped属性.这意味着我们无法检查用户是否关闭了传播.
所以我的问题是,有没有人对如何做到这一点有任何想法?我知道jquery管理类似的专长(http://bugs.jquery.com/ticket/3355),但他们以类似的方式执行 - 将其存储在对象的额外数据中.我不知道的是对象的非持久性如何不会像对待我一样伤害它们.(由于各种原因,使用jquery本身不是一个选项)
如果有人有任何见解 - …