github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/rpc/chain/subscribe_finalized_heads.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 chain
    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  // FinalizedHeadsSubscription is a subscription established through one of the Client's subscribe methods.
    29  type FinalizedHeadsSubscription struct {
    30  	sub      *gethrpc.ClientSubscription
    31  	channel  chan types.Header
    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 *FinalizedHeadsSubscription) Chan() <-chan types.Header {
    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 *FinalizedHeadsSubscription) 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 *FinalizedHeadsSubscription) Unsubscribe() {
    57  	s.sub.Unsubscribe()
    58  	s.quitOnce.Do(func() {
    59  		close(s.channel)
    60  	})
    61  }
    62  
    63  // SubscribeFinalizedHeads subscribes the best finalized headers, returning a subscription that will
    64  // receive server notifications containing the Header.
    65  func (c *Chain) SubscribeFinalizedHeads() (*FinalizedHeadsSubscription, error) {
    66  	ctx, cancel := context.WithTimeout(context.Background(), config.Default().SubscribeTimeout)
    67  	defer cancel()
    68  
    69  	ch := make(chan types.Header)
    70  
    71  	sub, err := c.client.Subscribe(ctx, "chain", "subscribeFinalizedHeads", "unsubscribeFinalizedHeads",
    72  		"finalizedHead", ch)
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  
    77  	return &FinalizedHeadsSubscription{sub: sub, channel: ch}, nil
    78  }