2019-05-07 07:05:38 +00:00
|
|
|
package subscriptions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2020-01-10 10:09:07 +00:00
|
|
|
"github.com/status-im/status-go/rpc"
|
2019-05-07 07:05:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type API struct {
|
2020-01-10 10:09:07 +00:00
|
|
|
rpcPrivateClientFunc func() *rpc.Client
|
|
|
|
activeSubscriptions *Subscriptions
|
2019-05-07 07:05:38 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 10:09:07 +00:00
|
|
|
func NewPublicAPI(rpcPrivateClientFunc func() *rpc.Client) *API {
|
2019-05-07 07:05:38 +00:00
|
|
|
return &API{
|
2020-01-10 10:09:07 +00:00
|
|
|
rpcPrivateClientFunc: rpcPrivateClientFunc,
|
|
|
|
activeSubscriptions: NewSubscriptions(100 * time.Millisecond),
|
2019-05-07 07:05:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) SubscribeSignal(method string, args []interface{}) (SubscriptionID, error) {
|
|
|
|
var (
|
|
|
|
filter filter
|
|
|
|
err error
|
|
|
|
namespace = method[:3]
|
|
|
|
)
|
|
|
|
|
|
|
|
switch namespace {
|
|
|
|
case "shh":
|
2020-01-10 10:09:07 +00:00
|
|
|
filter, err = installShhFilter(api.rpcPrivateClientFunc(), method, args)
|
2019-05-07 07:05:38 +00:00
|
|
|
case "eth":
|
2020-01-10 10:09:07 +00:00
|
|
|
filter, err = installEthFilter(api.rpcPrivateClientFunc(), method, args)
|
2019-05-07 07:05:38 +00:00
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unexpected namespace: %s", namespace)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2019-12-11 08:44:57 +00:00
|
|
|
return "", fmt.Errorf("[SubscribeSignal] could not subscribe, failed to call %s: %v", method, err)
|
2019-05-07 07:05:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return api.activeSubscriptions.Create(namespace, filter)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) UnsubscribeSignal(id string) error {
|
|
|
|
return api.activeSubscriptions.Remove(SubscriptionID(id))
|
|
|
|
}
|