mirror of
https://github.com/MontFerret/ferret.git
synced 2025-11-06 08:39:09 +02:00
change WaitForNavigation to avoid race condition/panic sending on closed channel (#84)
This commit is contained in:
@@ -557,26 +557,20 @@ func (doc *HTMLDocument) WaitForClassAll(selector, class values.String, timeout
|
||||
}
|
||||
|
||||
func (doc *HTMLDocument) WaitForNavigation(timeout values.Int) error {
|
||||
timer := time.NewTimer(time.Millisecond * time.Duration(timeout))
|
||||
onEvent := make(chan bool)
|
||||
onEvent := make(chan struct{})
|
||||
listener := func(_ interface{}) {
|
||||
onEvent <- true
|
||||
close(onEvent)
|
||||
}
|
||||
|
||||
defer doc.events.RemoveEventListener("load", listener)
|
||||
defer close(onEvent)
|
||||
|
||||
doc.events.AddEventListener("load", listener)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-onEvent:
|
||||
timer.Stop()
|
||||
|
||||
return nil
|
||||
case <-timer.C:
|
||||
return core.ErrTimeout
|
||||
}
|
||||
select {
|
||||
case <-onEvent:
|
||||
return nil
|
||||
case <-time.After(time.Millisecond * time.Duration(timeout)):
|
||||
return core.ErrTimeout
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user