-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdevices_view_controller.go
More file actions
116 lines (91 loc) · 2.74 KB
/
devices_view_controller.go
File metadata and controls
116 lines (91 loc) · 2.74 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package sdk
import (
"context"
"slices"
"github.com/urnetwork/glog"
"github.com/urnetwork/connect"
)
type NetworkClientsListener interface {
NetworkClientsChanged(networkClients *NetworkClientInfoList)
}
type DevicesViewController struct {
ctx context.Context
cancel context.CancelFunc
device Device
networkClientsListeners *connect.CallbackList[NetworkClientsListener]
}
func newDevicesViewController(ctx context.Context, device Device) *DevicesViewController {
cancelCtx, cancel := context.WithCancel(ctx)
vc := &DevicesViewController{
ctx: cancelCtx,
cancel: cancel,
device: device,
networkClientsListeners: connect.NewCallbackList[NetworkClientsListener](),
}
return vc
}
func (self *DevicesViewController) ClientId() *Id {
return self.device.GetClientId()
}
func (self *DevicesViewController) Start() {
// FIXME
// request clients
self.device.GetApi().GetNetworkClients(GetNetworkClientsCallback(connect.NewApiCallback[*NetworkClientsResult](
func(result *NetworkClientsResult, err error) {
if err == nil {
// FIXME sort
networkClients := []*NetworkClientInfo{}
for i := 0; i < result.Clients.Len(); i += 1 {
networkClient := result.Clients.Get(i)
networkClients = append(networkClients, networkClient)
}
slices.SortStableFunc(networkClients, self.cmpNetworkClientLayout)
exportedNetworkClients := NewNetworkClientInfoList()
exportedNetworkClients.addAll(networkClients...)
self.networkClientsChanged(exportedNetworkClients)
}
},
)))
}
func (self *DevicesViewController) Stop() {
// FIXME
}
func (self *DevicesViewController) AddNetworkClientsListener(listener NetworkClientsListener) Sub {
callbackId := self.networkClientsListeners.Add(listener)
return newSub(func() {
self.networkClientsListeners.Remove(callbackId)
})
}
// `NetworkClientsListener`
func (self *DevicesViewController) networkClientsChanged(networkClients *NetworkClientInfoList) {
for _, listener := range self.networkClientsListeners.Get() {
connect.HandleError(func() {
listener.NetworkClientsChanged(networkClients)
})
}
}
func (self *DevicesViewController) Close() {
glog.Info("[dvc]close")
self.cancel()
}
func (self *DevicesViewController) cmpNetworkClientLayout(a *NetworkClientInfo, b *NetworkClientInfo) int {
if a == b {
return 0
}
clientId := *self.ClientId()
if (clientId == *a.ClientId) != (clientId == *b.ClientId) {
if clientId == *a.ClientId {
return -1
} else {
return 1
}
}
if (a.Connections != nil && 0 < a.Connections.Len()) != (b.Connections != nil && 0 < b.Connections.Len()) {
if a.Connections != nil && 0 < a.Connections.Len() {
return -1
} else {
return 1
}
}
return a.ClientId.Cmp(b.ClientId)
}