forked from mkenney/go-chrome
/
socket.command_mapper.go
65 lines (55 loc) · 1.16 KB
/
socket.command_mapper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package socket
import (
"fmt"
"sync"
errs "github.com/bdlm/errors"
)
/*
NewCommandMap creates and returns a pointer to a struct that implements the
CommandMapper interface.
*/
func NewCommandMap() *CommandMap {
return &CommandMap{
stack: make(map[int]Commander),
mux: &sync.Mutex{},
}
}
/*
CommandMap provides a CommandMapper interface implementation for managing the
command stack.
*/
type CommandMap struct {
mux *sync.Mutex
stack map[int]Commander
}
/*
Delete removes a command from the stack.
Delete is a CommandMapper implementation.
*/
func (stack *CommandMap) Delete(id int) {
stack.mux.Lock()
delete(stack.stack, id)
stack.mux.Unlock()
}
/*
Get retrieves a command from the stack.
Get is a CommandMapper implementation.
*/
func (stack *CommandMap) Get(id int) (Commander, error) {
stack.mux.Lock()
command, ok := stack.stack[id]
stack.mux.Unlock()
if !ok {
return nil, errs.New(0, fmt.Sprintf("Command %d not found", id))
}
return command, nil
}
/*
Set sets a command in the stack.
Set is a CommandMapper implementation.
*/
func (stack *CommandMap) Set(cmd Commander) {
stack.mux.Lock()
stack.stack[cmd.ID()] = cmd
stack.mux.Unlock()
}