// Copyright (C) 2018 rameshvk. All rights reserved.
// Use of this source code is governed by a MIT-style license
// that can be found in the LICENSE file.

package sync

import (
	"time"

	"github.com/dotchain/dot/log"
	"github.com/dotchain/dot/ops"
)

// Config defines the configuration options for synchronization
type Config struct {
	// A reliable ops store.
	ops.Store

	// AutoTransform is off by default but can be set to true to
	// automatically transform the provided store
	AutoTransform bool

	// NonBlocking is inititally off but can be set to true to
	// make GetSince non-blocking
	NonBlocking bool
	ops.Cache

	// Session state
	Version    int
	Pending    []ops.Op
	MergeChain []ops.Op

	// logger
	log.Log

	// Session state notifier
	Notify func(version int, pending, mergeChain []ops.Op)

	// Backoff configures the exponential backoff settings
	Backoff struct {
		Rand         func() float64
		Initial, Max time.Duration
	}
}

// Option can be used to configure Sync behavior
//
// See WithSession and WithCloseNotify
type Option func(c *Config)

// WithSession configures the connector to start with the provided
// version and pending instead of starting from scrach
func WithSession(version int, pending, mergeChain []ops.Op) Option {
	return func(c *Config) {
		c.Version = version
		c.Pending = pending
		c.MergeChain = mergeChain
	}
}

// WithNotify configures a callback to be called when the
// session state changes. In particular, this is called when the
// session is closed.
func WithNotify(fn func(version int, pending, mergeChain []ops.Op)) Option {
	return func(c *Config) {
		c.Notify = fn
	}
}

// WithLog configures the logger to use
func WithLog(l log.Log) Option {
	return func(c *Config) {
		c.Log = l
	}
}

// WithBackoff configures the binary-exponential backoff settings
func WithBackoff(rng func() float64, initial, max time.Duration) Option {
	return func(c *Config) {
		c.Backoff.Rand = rng
		c.Backoff.Initial = initial
		c.Backoff.Max = max
	}
}

// WithAutoTransform specifies that the initial store yields
// untransformed operations and must be automatically transformed.
//
// The cache is required
func WithAutoTransform(cache ops.Cache) Option {
	return func(c *Config) {
		c.AutoTransform = true
		c.Cache = cache
	}
}

// WithNonBlocking sets the non-blocking flag to true.
//
// This makes Pull() return synchronously with any remote fetches in
// the background.
func WithNonBlocking(nonBlock bool) Option {
	return func(c *Config) {
		c.NonBlocking = nonBlock
	}
}

Related articles

dot empty

// Copyright (C) 2018 rameshvk. All rights reserved. // Use of this source code is governed by a MIT-style license // that can be found in the LICENSE file. package changes // Nil represents an empty value. It can be used with Replace or // Splice to i

dot caret_test

// Copyright (C) 2018 rameshvk. All rights reserved. // Use of this source code is governed by a MIT-style license // that can be found in the LICENSE file. package refs_test import ( "reflect" "testing" "github.com/dotchain/dot/changes" "github.c

dot nw_test

// Copyright (C) 2018 rameshvk. All rights reserved. // Use of this source code is governed by a MIT-style license // that can be found in the LICENSE file. package nw_test import ( "bytes" "context" "errors" "net/http" "net/http/httptest" "testi

dot image

// Copyright (C) 2019 rameshvk. All rights reserved. // Use of this source code is governed by a MIT-style license // that can be found in the LICENSE file. package data import ( "github.com/dotchain/dot/changes" "github.com/dotchain/dot/changes/type

dot rank_test

// Copyright (C) 2019 rameshvk. All rights reserved. // Use of this source code is governed by a MIT-style license // that can be found in the LICENSE file. package crdt_test import ( "testing" "github.com/dotchain/dot/changes/crdt" ) func TestNewR

dot ord_test

// Copyright (C) 2019 rameshvk. All rights reserved. // Use of this source code is governed by a MIT-style license // that can be found in the LICENSE file. package crdt_test import ( "testing" "github.com/dotchain/dot/changes/crdt" ) func TestIntN

dot generated2

// Generated. DO NOT EDIT. package mystruct import ( "github.com/dotchain/dot/changes" "github.com/dotchain/dot/changes/types" "github.com/dotchain/dot/streams" ) func (my *myStructp) get(key interface{}) changes.Value { switch key { case "b":

dot todo

// Code generated by github.com/tvastar/test/cmd/testmd/testmd.go. DO NOT EDIT. package example import ( "net/http" "sync" "time" "github.com/dotchain/dot" "github.com/dotchain/dot/ops/nw" ) func Server() { // import net/http // import github.

dot callable_test

// Copyright (C) 2019 rameshvk. All rights reserved. // Use of this source code is governed by a MIT-style license // that can be found in the LICENSE file. package eval_test import ( "testing" "github.com/dotchain/dot/changes" "github.com/dotchain

dot nonblocking

// Copyright (C) 2018 rameshvk. All rights reserved. // Use of this source code is governed by a MIT-style license // that can be found in the LICENSE file. package sync import ( "context" "sync" "github.com/dotchain/dot/ops" ) // NonBlocking conv