github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/rpc/state/subscribe_runtime_version.go (about)

     1  // Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
     2  //
     3  // Copyright 2020 Stafi Protocol
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package state //nolint:dupl
    18  
    19  import (
    20  	"context"
    21  	"sync"
    22  
    23  	"github.com/stafiprotocol/go-substrate-rpc-client/config"
    24  	gethrpc "github.com/stafiprotocol/go-substrate-rpc-client/pkg/gethrpc"
    25  	"github.com/stafiprotocol/go-substrate-rpc-client/types"
    26  )
    27  
    28  // RuntimeVersionSubscription is a subscription established through one of the Client's subscribe methods.
    29  type RuntimeVersionSubscription struct {
    30  	sub      *gethrpc.ClientSubscription
    31  	channel  chan types.RuntimeVersion
    32  	quitOnce sync.Once // ensures quit is closed once
    33  }
    34  
    35  // Chan returns the subscription channel.
    36  //
    37  // The channel is closed when Unsubscribe is called on the subscription.
    38  func (s *RuntimeVersionSubscription) Chan() <-chan types.RuntimeVersion {
    39  	return s.channel
    40  }
    41  
    42  // Err returns the subscription error channel. The intended use of Err is to schedule
    43  // resubscription when the client connection is closed unexpectedly.
    44  //
    45  // The error channel receives a value when the subscription has ended due
    46  // to an error. The received error is nil if Close has been called
    47  // on the underlying client and no other error has occurred.
    48  //
    49  // The error channel is closed when Unsubscribe is called on the subscription.
    50  func (s *RuntimeVersionSubscription) Err() <-chan error {
    51  	return s.sub.Err()
    52  }
    53  
    54  // Unsubscribe unsubscribes the notification and closes the error channel.
    55  // It can safely be called more than once.
    56  func (s *RuntimeVersionSubscription) Unsubscribe() {
    57  	s.sub.Unsubscribe()
    58  	s.quitOnce.Do(func() {
    59  		close(s.channel)
    60  	})
    61  }
    62  
    63  // SubscribeRuntimeVersion subscribes the runtime version, returning a subscription that will
    64  // receive server notifications containing the RuntimeVersion.
    65  func (s *State) SubscribeRuntimeVersion() (
    66  	*RuntimeVersionSubscription, error) {
    67  	ctx, cancel := context.WithTimeout(context.Background(), config.Default().SubscribeTimeout)
    68  	defer cancel()
    69  
    70  	c := make(chan types.RuntimeVersion)
    71  
    72  	sub, err := s.client.Subscribe(ctx, "state", "subscribeRuntimeVersion", "unsubscribeRuntimeVersion",
    73  		"runtimeVersion", c)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  
    78  	return &RuntimeVersionSubscription{sub: sub, channel: c}, nil
    79  }