-
Notifications
You must be signed in to change notification settings - Fork 244
/
api.go
53 lines (42 loc) · 1.14 KB
/
api.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
package subscriptions
import (
"fmt"
"time"
"github.com/status-im/status-go/node"
)
type API struct {
node *node.StatusNode
activeSubscriptions *Subscriptions
}
func NewPublicAPI(node *node.StatusNode) *API {
return &API{
node: node,
activeSubscriptions: NewSubscriptions(100 * time.Millisecond),
}
}
func (api *API) SubscribeSignal(method string, args []interface{}) (SubscriptionID, error) {
var (
filter filter
err error
namespace = method[:3]
)
rpc := api.node.RPCPrivateClient()
switch namespace {
case "shh":
filter, err = installShhFilter(rpc, method, args)
case "eth":
filter, err = installEthFilter(rpc, method, args)
default:
err = fmt.Errorf("unexpected namespace: %s", namespace)
}
if err != nil {
return SubscriptionID(""), fmt.Errorf("[SubscribeSignal] could not subscribe, failed to call %s: %v", method, err)
}
return api.activeSubscriptions.Create(namespace, filter)
}
func (api *API) UnsubscribeSignal(id string) error {
return api.activeSubscriptions.Remove(SubscriptionID(id))
}
func (api *API) shutdown() error {
return api.activeSubscriptions.removeAll()
}