@@ -16,6 +16,7 @@ type Room struct {
1616 // Private
1717 id uint64
1818 code * string
19+ name * string
1920 scope string
2021 conn * Conn
2122 waitJoin chan error
@@ -36,9 +37,8 @@ type Room struct {
3637//
3738// Example:
3839//
39- // // Suppose Collection stuff has a room (.room)
40- // room := thingsdb.NewRoom("//stuff", ".room.id();")
41- //
40+ // // Suppose Collection stuff has a room (.room)
41+ // room := thingsdb.NewRoom("//stuff", ".room.id();")
4242func NewRoom (scope string , code string ) * Room {
4343 room := NewRoomFromId (scope , 0 )
4444 room .code = & code
@@ -51,14 +51,14 @@ func NewRoom(scope string, code string) *Room {
5151//
5252// Example:
5353//
54- // // Suppose Collection stuff has a room with Id 17
55- // room := thingsdb.NewRoomFromId("//stuff", 17)
56- //
54+ // // Suppose Collection stuff has a room with Id 17
55+ // room := thingsdb.NewRoomFromId("//stuff", 17)
5756func NewRoomFromId (scope string , id uint64 ) * Room {
5857 return & Room {
5958 // Private
6059 id : id ,
6160 code : nil ,
61+ name : nil ,
6262 scope : scope ,
6363 conn : nil ,
6464 waitJoin : nil ,
@@ -74,6 +74,20 @@ func NewRoomFromId(scope string, id uint64) *Room {
7474 }
7575}
7676
77+ // NewRoom creates a new room using the room name.
78+ //
79+ // Example:
80+ //
81+ // // Suppose Collection stuff has a room (.room)
82+ // room := thingsdb.NewRoomFromName("//stuff", "my_room")
83+ func NewRoomFromName (scope string , name string ) * Room {
84+ room := NewRoomFromId (scope , 0 )
85+ code := "room(name).id();"
86+ room .code = & code
87+ room .name = & name
88+ return room
89+ }
90+
7791// Id returns the Id of the room.
7892//
7993// > Note: Id() returns `0` when the room was created using `NewRoom(..)` and the room has never been joined.
@@ -90,26 +104,25 @@ func (room *Room) Scope() string {
90104//
91105// Example:
92106//
93- // func onNewMessage(room *thingsdb.Room, args []interface{}) {
94- // if len(args) != 1 {
95- // fmt.Println("Invalid number of arguments")
96- // return
97- // }
107+ // func onNewMessage(room *thingsdb.Room, args []interface{}) {
108+ // if len(args) != 1 {
109+ // fmt.Println("Invalid number of arguments")
110+ // return
111+ // }
98112//
99- // msg, ok := args[0].(string)
100- // if !ok {
101- // fmt.Println("Expecting argument 1 to be of type string")
102- // return
103- // }
113+ // msg, ok := args[0].(string)
114+ // if !ok {
115+ // fmt.Println("Expecting argument 1 to be of type string")
116+ // return
117+ // }
104118//
105- // fmt.Println(msg) // Just print the message
106- // }
119+ // fmt.Println(msg) // Just print the message
120+ // }
107121//
108- // room = thingsdb.NewRoom("//stuff", ".chatRoom.id();")
109- //
110- // // Add event handler for the "new-message" event
111- // room.HandleEvent("new-message", onNewMessage)
122+ // room = thingsdb.NewRoom("//stuff", ".chatRoom.id();")
112123//
124+ // // Add event handler for the "new-message" event
125+ // room.HandleEvent("new-message", onNewMessage)
113126func (room * Room ) HandleEvent (event string , handle func (room * Room , args []interface {})) {
114127 room .eventHandlers [event ] = handle
115128}
@@ -123,8 +136,7 @@ func (room *Room) HandleEvent(event string, handle func(room *Room, args []inter
123136//
124137// Example:
125138//
126- // err := room.Join(conn, thingsdb.DefaultWait)
127- //
139+ // err := room.Join(conn, thingsdb.DefaultWait)
128140func (room * Room ) Join (conn * Conn , wait time.Duration ) error {
129141
130142 if wait > 0 {
@@ -142,7 +154,7 @@ func (room *Room) Join(conn *Conn, wait time.Duration) error {
142154
143155 go func () {
144156 time .Sleep (wait )
145- room .waitJoin <- fmt .Errorf ("Timeout while waiting for the join event on room Id %d" , room .id )
157+ room .waitJoin <- fmt .Errorf ("timeout while waiting for the join event on room Id %d" , room .id )
146158 }()
147159
148160 for {
@@ -159,8 +171,7 @@ func (room *Room) Join(conn *Conn, wait time.Duration) error {
159171//
160172// Example:
161173//
162- // err := room.Leave()
163- //
174+ // err := room.Leave()
164175func (room * Room ) Leave () error {
165176 if room .id == 0 {
166177 return fmt .Errorf ("Room Id is zero (0), most likely the room has never been joined" )
@@ -187,13 +198,12 @@ func (room *Room) Leave() error {
187198//
188199// Example:
189200//
190- // args := []interface{}{"Just some chat message"}
191- //
192- // err := room.Emit(
193- // "new-message", // Event to emit
194- // args // Arguments (may be nil)
195- // );
201+ // args := []interface{}{"Just some chat message"}
196202//
203+ // err := room.Emit(
204+ // "new-message", // Event to emit
205+ // args // Arguments (may be nil)
206+ // );
197207func (room * Room ) Emit (event string , args []interface {}) error {
198208 if room .conn == nil {
199209 return fmt .Errorf ("Room Id %d is not joined" , room .id )
@@ -206,10 +216,16 @@ func (room *Room) join(conn *Conn) error {
206216 defer conn .rooms .mux .Unlock ()
207217
208218 if room .id == 0 {
219+ var vars map [string ]interface {}
209220 if room .code == nil {
210221 return fmt .Errorf ("Code or a room Id > 0 is required" )
211222 }
212- val , err := conn .Query (room .scope , * room .code , nil )
223+ if room .name != nil {
224+ vars = map [string ]interface {}{
225+ "name" : * room .name ,
226+ }
227+ }
228+ val , err := conn .Query (room .scope , * room .code , vars )
213229 if err != nil {
214230 return err
215231 }
@@ -229,7 +245,7 @@ func (room *Room) join(conn *Conn) error {
229245 case uint64 :
230246 roomId = val
231247 default :
232- return fmt .Errorf ("Expecting code `%s` to return with a room Id (type integer), bot got: %v" , * room .code , val )
248+ return fmt .Errorf ("expecting code `%s` to return with a room Id (type integer), bot got: %v" , * room .code , val )
233249 }
234250
235251 roomIds := []* uint64 {& roomId }
0 commit comments