Skip to content

Instantly share code, notes, and snippets.

@dragonsinth
Created December 16, 2021 17:50
Show Gist options
  • Save dragonsinth/5bef35eb998d22f1fbf2af63ae1338bb to your computer and use it in GitHub Desktop.
Save dragonsinth/5bef35eb998d22f1fbf2af63ae1338bb to your computer and use it in GitHub Desktop.
type LogReplicatedModel struct {
mu sync.RWMutex
snapshot Model
stream eventstream.EventStream
}
func (m *LogReplicatedModel) ReadAndSubscribe() (Model, eventstream.Promise) {
m.mu.RLock()
defer m.mu.RUnlock()
return m.snapshot, m.stream.Subscribe()
}
func (m *LogReplicatedModel) WriteAndPublish(mutation Mutation) {
m.mu.Lock()
defer m.mu.Unlock()
m.snapshot = m.snapshot.ApplyMutation(mutation)
m.stream.Publish(mutation)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment