Skip to content

Commit

Permalink
Merge branch 'feat/set-any' of github.com:iotaledger/hive.go into fea…
Browse files Browse the repository at this point in the history
…t/set-any
  • Loading branch information
hmoog committed Nov 10, 2023
2 parents 2370496 + 62119c6 commit 7135670
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 20 deletions.
5 changes: 5 additions & 0 deletions ds/reactive/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ type ReadableSet[ElementType comparable] interface {
// set minus the elements of the other sets.
SubtractReactive(others ...ReadableSet[ElementType]) Set[ElementType]

// WithElements is a utility function that allows to set up dynamic behavior based on the elements of the Set which
// is torn down once the element is removed gi(or the returned teardown function is called). It accepts an optional
// condition that has to be satisfied for the setup function to be called.
WithElements(setup func(element ElementType) (teardown func()), condition ...func(ElementType) bool) (teardown func())

// ReadableSet imports the read methods of the Set interface.
ds.ReadableSet[ElementType]
}
Expand Down
35 changes: 35 additions & 0 deletions ds/reactive/set_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,41 @@ func (r *readableSet[ElementType]) SubtractReactive(others ...ReadableSet[Elemen
return s
}

// WithElements is a utility function that allows to set up dynamic behavior based on the elements of the Set which is
// torn down once the element is removed (or the returned teardown function is called). It accepts an optional
// condition that has to be satisfied for the setup function to be called.
func (r *readableSet[ElementType]) WithElements(setup func(element ElementType) (teardown func()), condition ...func(ElementType) bool) (teardown func()) {
teardownFunctions := make(map[ElementType]func())

return lo.Batch(
r.OnUpdate(func(appliedMutations ds.SetMutations[ElementType]) {
appliedMutations.AddedElements().Range(func(element ElementType) {
if len(condition) == 0 || condition[0](element) {
if teardownFunc := setup(element); teardownFunc != nil {
teardownFunctions[element] = teardownFunc
}
}
})

appliedMutations.DeletedElements().Range(func(element ElementType) {
if teardownFunc, exists := teardownFunctions[element]; exists {
delete(teardownFunctions, element)

teardownFunc()
}
})
}),

func() {
for element, teardownFunc := range teardownFunctions {
delete(teardownFunctions, element)

teardownFunc()
}
},
)
}

// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////

// region derivedSet ///////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion ds/reactive/variable_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (r *readableVariable[Type]) WithValue(setup func(value Type) (teardown func
if len(condition) == 0 || condition[0](value) {
unsubscribeOnUpdate(func() func() { return setup(value) })
}
})
}, true)
}

// WithNonEmptyValue is a utility function that allows to set up dynamic behavior based on the latest (non-empty)
Expand Down
4 changes: 1 addition & 3 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package log
import (
"log/slog"
"os"

"github.com/iotaledger/hive.go/ds/reactive"
)

// Logger is a reactive logger that can be used to log messages with different log levels.
Expand Down Expand Up @@ -86,7 +84,7 @@ type Logger interface {
// NewEntityLogger creates a new logger for an entity with the given name. The logger is automatically shut down
// when the given shutdown event is triggered. The initLogging function is called with the new logger instance and
// can be used to configure the logger.
NewEntityLogger(entityName string, shutdownEvent reactive.Event, initLogging func(entityLogger Logger)) Logger
NewEntityLogger(entityName string) (entityLogger Logger, shutdown func())
}

// NewLogger creates a new logger with the given name and an optional handler. If no handler is provided, the logger
Expand Down
16 changes: 5 additions & 11 deletions log/logger_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,20 +224,14 @@ func (l *logger) NewChildLogger(name string) (childLogger Logger, shutdown func(
return nestedLoggerInstance, nestedLoggerInstance.reactiveLevel.InheritFrom(l.reactiveLevel)
}

// NewEntityLogger creates a new logger for an entity with the given name. The logger is automatically shut down when
// the given shutdown event is triggered. The initLogging function is called with the new logger instance and can be
// used to configure the logger.
func (l *logger) NewEntityLogger(entityName string, shutdownEvent reactive.Event, initLogging func(entityLogger Logger)) Logger {
// NewEntityLogger is identical to NewChildLogger with the difference that the name of the logger is automatically
// extended with a unique identifier to avoid name collisions.
func (l *logger) NewEntityLogger(entityName string) (entityLogger Logger, shutdown func()) {
if l == nil {
return l
return l, func() {}
}

embeddedLogger, shutdown := l.NewChildLogger(l.uniqueEntityName(entityName))
shutdownEvent.OnTrigger(shutdown)

initLogging(embeddedLogger)

return embeddedLogger
return l.NewChildLogger(l.uniqueEntityName(entityName))
}

// uniqueEntityName returns the name of an embedded instance of the given type.
Expand Down
10 changes: 5 additions & 5 deletions log/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ func NewTestObject(logger log.Logger) *TestObject {
IsEvicted: reactive.NewEvent(),
}

t.Logger = logger.NewEntityLogger("TestObject", t.IsEvicted, func(entityLogger log.Logger) {
t.ImportantValue1.LogUpdates(entityLogger, log.LevelInfo, "ImportantValue1")
t.ImportantValue2.LogUpdates(entityLogger, log.LevelInfo, "ImportantValue2")
t.LessImportantValue1.LogUpdates(entityLogger, log.LevelDebug, "LessImportantValue1")
})
t.Logger, _ = logger.NewEntityLogger("TestObject")

t.ImportantValue1.LogUpdates(t.Logger, log.LevelInfo, "ImportantValue1")
t.ImportantValue2.LogUpdates(t.Logger, log.LevelInfo, "ImportantValue2")
t.LessImportantValue1.LogUpdates(t.Logger, log.LevelDebug, "LessImportantValue1")

return t
}

0 comments on commit 7135670

Please sign in to comment.