-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split event api from sdk pkg to eventsdk pkg
- Loading branch information
Showing
9 changed files
with
107 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package eventsdk | ||
|
||
import ( | ||
"github.com/cskr/pubsub" | ||
"github.com/mesg-foundation/core/database" | ||
"github.com/mesg-foundation/core/event" | ||
"github.com/mesg-foundation/core/utils/hash" | ||
) | ||
|
||
const ( | ||
topic = "Event" | ||
) | ||
|
||
// Event exposes event APIs of MESG. | ||
type Event struct { | ||
ps *pubsub.PubSub | ||
db database.ServiceDB | ||
} | ||
|
||
// New creates a new Event SDK with given options. | ||
func New(ps *pubsub.PubSub, db database.ServiceDB) *Event { | ||
return &Event{ | ||
ps: ps, | ||
db: db, | ||
} | ||
} | ||
|
||
// Emit emits a MESG event eventKey with eventData for service token. | ||
func (e *Event) Emit(token, eventKey string, eventData map[string]interface{}) error { | ||
s, err := e.db.Get(token) | ||
if err != nil { | ||
return err | ||
} | ||
ev, err := event.Create(s, eventKey, eventData) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
go e.ps.Pub(ev, subTopic(s.Hash)) | ||
return nil | ||
} | ||
|
||
// Listen listens events matches with eventFilter on serviceID. | ||
func (e *Event) Listen(service string, f *Filter) (*Listener, error) { | ||
s, err := e.db.Get(service) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if f.HasKey() { | ||
if _, err := s.GetEvent(f.Key); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
l := NewListener(e.ps, subTopic(s.Hash), f) | ||
go l.Listen() | ||
return l, nil | ||
} | ||
|
||
// subTopic returns the topic to listen for events from this service. | ||
func subTopic(serviceHash string) string { | ||
return hash.Calculate([]string{serviceHash, topic}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package eventsdk | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/mesg-foundation/core/utils/hash" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSubTopic(t *testing.T) { | ||
serviceHash := "1" | ||
require.Equal(t, subTopic(serviceHash), hash.Calculate([]string{serviceHash, topic})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters