@@ -33,14 +33,20 @@ var _ cdp.Executor = &Connection{}
33
33
// chrome, it's best to work with unique ids to avoid the Execute
34
34
// handlers working with the wrong response, or handlers deadlocking
35
35
// when their response is rerouted to the wrong handler.
36
+ //
37
+ // Use the msgIDGenerator interface to abstract `id` away.
36
38
type msgID struct {
37
39
id int64
38
40
}
39
41
40
- func (m * msgID ) new () int64 {
42
+ func (m * msgID ) newID () int64 {
41
43
return atomic .AddInt64 (& m .id , 1 )
42
44
}
43
45
46
+ type msgIDGenerator interface {
47
+ newID () int64
48
+ }
49
+
44
50
type executorEmitter interface {
45
51
cdp.Executor
46
52
EventEmitter
@@ -125,7 +131,7 @@ type Connection struct {
125
131
done chan struct {}
126
132
closing chan struct {}
127
133
shutdownOnce sync.Once
128
- msgID * msgID
134
+ msgIDGen msgIDGenerator
129
135
130
136
sessionsMu sync.RWMutex
131
137
sessions map [target.SessionID ]* Session
@@ -163,7 +169,7 @@ func NewConnection(ctx context.Context, wsURL string, logger *log.Logger) (*Conn
163
169
errorCh : make (chan error ),
164
170
done : make (chan struct {}),
165
171
closing : make (chan struct {}),
166
- msgID : & msgID {},
172
+ msgIDGen : & msgID {},
167
173
sessions : make (map [target.SessionID ]* Session ),
168
174
}
169
175
@@ -329,7 +335,7 @@ func (c *Connection) recvLoop() {
329
335
sid , tid := eva .SessionID , eva .TargetInfo .TargetID
330
336
331
337
c .sessionsMu .Lock ()
332
- session := NewSession (c .ctx , c , sid , tid , c .logger , c .msgID )
338
+ session := NewSession (c .ctx , c , sid , tid , c .logger , c .msgIDGen )
333
339
c .logger .Debugf ("Connection:recvLoop:EventAttachedToTarget" , "sid:%v tid:%v wsURL:%q" , sid , tid , c .wsURL )
334
340
c .sessions [sid ] = session
335
341
c .sessionsMu .Unlock ()
@@ -509,7 +515,7 @@ func (c *Connection) Close(args ...goja.Value) {
509
515
// Execute implements cdproto.Executor and performs a synchronous send and receive.
510
516
func (c * Connection ) Execute (ctx context.Context , method string , params easyjson.Marshaler , res easyjson.Unmarshaler ) error {
511
517
c .logger .Debugf ("connection:Execute" , "wsURL:%q method:%q" , c .wsURL , method )
512
- id := c .msgID . new ()
518
+ id := c .msgIDGen . newID ()
513
519
514
520
// Setup event handler used to block for response to message being sent.
515
521
ch := make (chan * cdproto.Message , 1 )
0 commit comments