// 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 streams

import "github.com/dotchain/dot/changes"

// Int implements an int stream.
type Int struct {
	Stream Stream
	Value  int
}

// Next returns the next if there is one.
func (s *Int) Next() (*Int, changes.Change) {
	if s.Stream == nil {
		return nil, nil
	}

	next, nextc := s.Stream.Next()
	if next == nil {
		return nil, nil
	}

	v := s.Value
	val, ok := (changes.Atomic{Value: v}).Apply(nil, nextc).(changes.Atomic)
	if ok {
		v, ok = val.Value.(int)
	}
	if !ok {
		next = nil
		v = s.Value
		nextc = nil
	}
	return &Int{Stream: next, Value: v}, nextc
}

// Latest returns the latest non-nil entry in the stream
func (s *Int) Latest() *Int {
	for next, _ := s.Next(); next != nil; next, _ = s.Next() {
		s = next
	}
	return s
}

// Update replaces the current value with the new value
func (s *Int) Update(val int) *Int {
	before, after := changes.Atomic{Value: s.Value}, changes.Atomic{Value: val}

	if s.Stream != nil {
		nexts := s.Stream.Append(changes.Replace{Before: before, After: after})
		s = &Int{Stream: nexts, Value: val}
	}
	return s
}

Related articles

dot diff_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 diff_test import ( "testing" "github.com/dotchain/dot/changes" "github.com/dotchain

dot dir

// 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 branch_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 streams_test import ( "testing" "github.com/dotchain/dot/changes" "github.com/dotch

dot errors

// 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 import ( "errors" "strconv" ) // the following are runtime errors var errRecurs

dot refs_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 ( "github.com/dotchain/dot/changes" "github.com/dotchain/dot/changes

dot union

// 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 dotc import ( "io" "text/template" ) // Union has the type information of a union fo

dot generic

// 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 types import "github.com/dotchain/dot/changes" // Generic is a helper to build value a

dot ord

// 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 import ( "math/big" "strconv" "strings" ) // NextOrd returns the next ordinal.

dot heading_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 data_test import ( "reflect" "testing" "github.com/dotchain/dot/changes" "github.c

dot typename

// 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 sjson import ( "errors" "reflect" "strconv" "strings" "time" ) func typeName(v re