system.go 1018 Bytes
Newer Older
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
package provider

import (
	"context"
	"github.com/ipfs/go-cid"
)

// System defines the interface for interacting with the value
// provider system
type System interface {
	Run()
	Close() error
	Provide(cid.Cid) error
	Reprovide(context.Context) error
}

type system struct {
	provider   Provider
	reprovider Reprovider
}

// NewSystem constructs a new provider system from a provider and reprovider
func NewSystem(provider Provider, reprovider Reprovider) System {
	return &system{provider, reprovider}
}

// Run the provider system by running the provider and reprovider
func (s *system) Run() {
	go s.provider.Run()
	go s.reprovider.Run()
}

// Close the provider and reprovider
func (s *system) Close() error {
	// TODO: Close reprovider here
	return s.provider.Close()
}

// Provide a value
func (s *system) Provide(cid cid.Cid) error {
	return s.provider.Provide(cid)
}

// Reprovide all the previously provided values
func (s *system) Reprovide(ctx context.Context) error {
	return s.reprovider.Trigger(ctx)
}