From fd7441943d74b678cc884d49ff0337df9d01ff6d Mon Sep 17 00:00:00 2001 From: "Jonathan Leibiusky @xetorthio" Date: Mon, 24 Jul 2017 13:46:48 -0300 Subject: [PATCH] EventType should be an enumerable type --- event/event.go | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/event/event.go b/event/event.go index 3216a88..1a94766 100644 --- a/event/event.go +++ b/event/event.go @@ -1,19 +1,37 @@ package event -type EventType string +var events []string + +type EventType int func (e EventType) String() string { - return string(e) + return events[int(e)] } -const INSTANCE_VIEWPORT_RESIZE EventType = "instance viewport resize" -const INSTANCE_DELETE EventType = "instance delete" -const INSTANCE_NEW EventType = "instance new" -const INSTANCE_STATS EventType = "instance stats" -const INSTANCE_TERMINAL_OUT EventType = "instance terminal out" -const SESSION_END EventType = "session end" -const SESSION_READY EventType = "session ready" -const SESSION_BUILDER_OUT EventType = "session builder out" +func ciota(s string) EventType { + events = append(events, s) + return EventType(len(events) - 1) +} + +func FindEventType(name string) (EventType, bool) { + for i, event := range events { + if event == name { + return EventType(i), true + } + } + return EventType(-1), false +} + +var ( + INSTANCE_VIEWPORT_RESIZE = ciota("instance viewport resize") + INSTANCE_DELETE = ciota("instance delete") + INSTANCE_NEW = ciota("instance new") + INSTANCE_STATS = ciota("instance stats") + INSTANCE_TERMINAL_OUT = ciota("instance terminal out") + SESSION_END = ciota("session end") + SESSION_READY = ciota("session ready") + SESSION_BUILDER_OUT = ciota("session builder out") +) type Handler func(sessionId string, args ...interface{}) type AnyHandler func(eventType EventType, sessionId string, args ...interface{})