mirror of https://github.com/matrix-org/gomatrix
Remove Rooms from Syncer, move to Storer
This commit is contained in:
parent
4f35a742e5
commit
03fda4b5d7
13
store.go
13
store.go
|
@ -10,6 +10,8 @@ type Storer interface {
|
||||||
LoadFilterID(userID string) string
|
LoadFilterID(userID string) string
|
||||||
SaveNextBatch(userID, nextBatchToken string)
|
SaveNextBatch(userID, nextBatchToken string)
|
||||||
LoadNextBatch(userID string) string
|
LoadNextBatch(userID string) string
|
||||||
|
SaveRoom(room *Room)
|
||||||
|
LoadRoom(roomID string) *Room
|
||||||
}
|
}
|
||||||
|
|
||||||
// InMemoryStore implements the Storer interface.
|
// InMemoryStore implements the Storer interface.
|
||||||
|
@ -20,6 +22,7 @@ type Storer interface {
|
||||||
type InMemoryStore struct {
|
type InMemoryStore struct {
|
||||||
Filters map[string]string
|
Filters map[string]string
|
||||||
NextBatch map[string]string
|
NextBatch map[string]string
|
||||||
|
Rooms map[string]*Room
|
||||||
}
|
}
|
||||||
|
|
||||||
// SaveFilterID to memory.
|
// SaveFilterID to memory.
|
||||||
|
@ -42,6 +45,16 @@ func (s *InMemoryStore) LoadNextBatch(userID string) string {
|
||||||
return s.NextBatch[userID]
|
return s.NextBatch[userID]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SaveRoom to memory.
|
||||||
|
func (s *InMemoryStore) SaveRoom(room *Room) {
|
||||||
|
s.Rooms[room.ID] = room
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadRoom from memory.
|
||||||
|
func (s *InMemoryStore) LoadRoom(roomID string) *Room {
|
||||||
|
return s.Rooms[roomID]
|
||||||
|
}
|
||||||
|
|
||||||
// NewInMemoryStore constructs a new InMemoryStore.
|
// NewInMemoryStore constructs a new InMemoryStore.
|
||||||
func NewInMemoryStore() *InMemoryStore {
|
func NewInMemoryStore() *InMemoryStore {
|
||||||
return &InMemoryStore{
|
return &InMemoryStore{
|
||||||
|
|
13
sync.go
13
sync.go
|
@ -20,10 +20,10 @@ type Syncer interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
// DefaultSyncer is the default syncing implementation. You can either write your own syncer, or selectively
|
// DefaultSyncer is the default syncing implementation. You can either write your own syncer, or selectively
|
||||||
// replace parts of this default syncer (e.g. the NextBatch/Filter storers, or the ProcessResponse method).
|
// replace parts of this default syncer (e.g. the ProcessResponse method). The default syncer uses the observer
|
||||||
|
// pattern to notify callers about incoming events. See DefaultSyncer.OnEventType for more information.
|
||||||
type DefaultSyncer struct {
|
type DefaultSyncer struct {
|
||||||
UserID string
|
UserID string
|
||||||
Rooms map[string]*Room
|
|
||||||
Store Storer
|
Store Storer
|
||||||
listeners map[string][]OnEventListener // event type to listeners array
|
listeners map[string][]OnEventListener // event type to listeners array
|
||||||
}
|
}
|
||||||
|
@ -35,14 +35,13 @@ type OnEventListener func(*Event)
|
||||||
func NewDefaultSyncer(userID string, store Storer) *DefaultSyncer {
|
func NewDefaultSyncer(userID string, store Storer) *DefaultSyncer {
|
||||||
return &DefaultSyncer{
|
return &DefaultSyncer{
|
||||||
UserID: userID,
|
UserID: userID,
|
||||||
Rooms: make(map[string]*Room),
|
|
||||||
Store: store,
|
Store: store,
|
||||||
listeners: make(map[string][]OnEventListener),
|
listeners: make(map[string][]OnEventListener),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProcessResponse processes the /sync response in a way suitable for bots. "Suitable for bots" means a stream of
|
// ProcessResponse processes the /sync response in a way suitable for bots. "Suitable for bots" means a stream of
|
||||||
// unrepeating events.
|
// unrepeating events. Returns a fatal error if a listener panics.
|
||||||
func (s *DefaultSyncer) ProcessResponse(res *RespSync, since string) (err error) {
|
func (s *DefaultSyncer) ProcessResponse(res *RespSync, since string) (err error) {
|
||||||
if !s.shouldProcessResponse(res, since) {
|
if !s.shouldProcessResponse(res, since) {
|
||||||
return
|
return
|
||||||
|
@ -126,10 +125,10 @@ func (s *DefaultSyncer) shouldProcessResponse(resp *RespSync, since string) bool
|
||||||
|
|
||||||
// getOrCreateRoom must only be called by the Sync() goroutine which calls ProcessResponse()
|
// getOrCreateRoom must only be called by the Sync() goroutine which calls ProcessResponse()
|
||||||
func (s *DefaultSyncer) getOrCreateRoom(roomID string) *Room {
|
func (s *DefaultSyncer) getOrCreateRoom(roomID string) *Room {
|
||||||
room := s.Rooms[roomID]
|
room := s.Store.LoadRoom(roomID)
|
||||||
if room == nil { // create a new Room
|
if room == nil { // create a new Room
|
||||||
room = NewRoom(roomID)
|
room = NewRoom(roomID)
|
||||||
s.Rooms[roomID] = room
|
s.Store.SaveRoom(room)
|
||||||
}
|
}
|
||||||
return room
|
return room
|
||||||
}
|
}
|
||||||
|
@ -144,7 +143,7 @@ func (s *DefaultSyncer) notifyListeners(event *Event) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// OnFailedSync always returns a 10 second wait period between failed /syncs.
|
// OnFailedSync always returns a 10 second wait period between failed /syncs, never a fatal error.
|
||||||
func (s *DefaultSyncer) OnFailedSync(res *RespSync, err error) (time.Duration, error) {
|
func (s *DefaultSyncer) OnFailedSync(res *RespSync, err error) (time.Duration, error) {
|
||||||
return 10 * time.Second, nil
|
return 10 * time.Second, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue