room.ts 14.9 KB
Newer Older
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
1 2 3 4 5 6 7
/**
 *
 * wechaty: Wechat for Bot. and for human who talk to bot/robot
 *
 * Licenst: ISC
 * https://github.com/zixia/wechaty
 *
8 9
 * Add/Del/Topic: https://github.com/wechaty/wechaty/issues/32
 *
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
10
 */
11
import { EventEmitter } from 'events'
12

13 14 15
import {
    Config
  , Sayable
16
  , log
17
}                 from './config'
18 19 20
import { Contact }    from './contact'
import { Message }    from './message'
import { UtilLib }    from './util-lib'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
21

22 23 24 25 26 27
type RoomObj = {
  id:         string
  encryId:    string
  topic:      string
  ownerUin:   number
  memberList: Contact[]
ruiruibupt's avatar
1  
ruiruibupt 已提交
28 29
  nameMap:    Map<string, string>
  aliasMap:   Map<string, string>
30 31
}

ruiruibupt's avatar
1  
ruiruibupt 已提交
32
type NameType = 'nick' | 'alias'
33

34
export type RoomRawMember = {
35
  UserName:     string
36
  NickName:     string
37 38 39
  DisplayName:  string
}

40
export type RoomRawObj = {
41 42 43 44
  UserName:         string
  EncryChatRoomId:  string
  NickName:         string
  OwnerUin:         number
45
  ChatRoomOwner:    string
46
  MemberList:       RoomRawMember[]
47 48
}

49 50 51
export type RoomEventName = 'join'
                          | 'leave'
                          | 'topic'
52 53
                          | 'EVENT_PARAM_ERROR'

54
export type RoomQueryFilter = {
55 56 57
  topic: string | RegExp
}

58
export type MemberQueryFilter = {
ruiruibupt's avatar
1  
ruiruibupt 已提交
59 60
  name?:  string
  alias?: string
61 62
}

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
63
export class Room extends EventEmitter implements Sayable {
64 65
  private static pool = new Map<string, Room>()

66 67
  private dirtyObj: RoomObj | null // when refresh, use this to save dirty data for query
  private obj:      RoomObj | null
68 69
  private rawObj:   RoomRawObj

70
  constructor(public id: string) {
71
    super()
72
    log.silly('Room', `constructor(${id})`)
73
  }
74

75
  public toString()    { return this.id }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
76
  public toStringEx()  { return `Room(${this.obj && this.obj.topic}[${this.id}])` }
77

78
  public isReady(): boolean {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
79
    return !!(this.obj && this.obj.memberList && this.obj.memberList.length)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
80 81
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
82 83 84 85 86
  // public refresh() {
  //   log.warn('Room', 'refresh() DEPRECATED. use reload() instead.')
  //   return this.reload()
  // }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
87
  public async refresh(): Promise<void> {
88 89 90
    if (this.isReady()) {
      this.dirtyObj = this.obj
    }
91
    this.obj = null
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
92 93
    await this.ready()
    return
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
94 95
  }

96 97 98 99 100 101 102 103
  private async readyAllMembers(memberList: RoomRawMember[]): Promise<void> {
    for (let member of memberList) {
      let contact = Contact.load(member.UserName)
      await contact.ready()
    }
    return
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
104 105 106 107 108
  // public ready(contactGetter?: (id: string) => Promise<any>) {
  //   log.warn('Room', 'ready() DEPRECATED. use load() instad.')
  //   return this.load(contactGetter)
  // }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
109
  public async ready(contactGetter?: (id: string) => Promise<any>): Promise<void> {
110
    log.silly('Room', 'ready(%s)', contactGetter ? contactGetter.constructor.name : '')
111
    if (!this.id) {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
112 113
      const e = new Error('ready() on a un-inited Room')
      log.warn('Room', e.message)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
114
      throw e
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
115
    } else if (this.isReady()) {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
116
      return
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
117
    } else if (this.obj && this.obj.id) {
118
      log.warn('Room', 'ready() has obj.id but memberList empty in room %s. reloading', this.obj.topic)
119
    }
120

121 122 123 124 125 126 127 128
    if (!contactGetter) {
      contactGetter = Config.puppetInstance()
                            .getContact.bind(Config.puppetInstance())
    }
    if (!contactGetter) {
      throw new Error('no contactGetter')
    }

129 130
    try {
      const data = await contactGetter(this.id)
131
      log.silly('Room', `contactGetter(${this.id}) resolved`)
132
      this.rawObj = data
133 134
      await this.readyAllMembers(this.rawObj.MemberList)
      this.obj    = this.parse(this.rawObj)
135 136 137
      if (!this.obj) {
        throw new Error('no this.obj set after contactGetter')
      }
138
      await Promise.all(this.obj.memberList.map(c => c.ready(contactGetter)))
139

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
140
      return
141

142
    } catch (e) {
143 144
      log.error('Room', 'contactGetter(%s) exception: %s', this.id, e.message)
      throw e
145
    }
146 147
  }

148 149 150
  public on(event: 'leave', listener: (this: Room, leaver: Contact) => void): this
  public on(event: 'join' , listener: (this: Room, inviteeList: Contact[] , inviter: Contact)  => void): this
  public on(event: 'topic', listener: (this: Room, topic: string, oldTopic: string, changer: Contact) => void): this
151
  public on(event: 'EVENT_PARAM_ERROR', listener: () => void): this
152

153
  public on(event: RoomEventName, listener: Function): this {
154
    log.verbose('Room', 'on(%s, %s)', event, typeof listener)
155

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
156 157 158 159 160 161 162 163 164 165 166
    // const thisWithSay = {
    //   say: (content: string) => {
    //     return Config.puppetInstance()
    //                   .say(content)
    //   }
    // }
    // super.on(event, function() {
    //   return listener.apply(thisWithSay, arguments)
    // })

    super.on(event, listener) // Room is `Sayable`
167
    return this
168 169
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
170 171 172
  public say(content: string): Promise<any>
  public say(content: string, replyTo: Contact): Promise<void>
  public say(content: string, replyTo: Contact[]): Promise<void>
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
173

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
174
  public say(content: string, replyTo?: Contact|Contact[]): Promise<void> {
175 176 177 178
    log.verbose('Room', 'say(%s, %s)'
                      , content
                      , Array.isArray(replyTo)
                        ? replyTo.map(c => c.name()).join(', ')
179
                        : replyTo ? replyTo.name() : ''
180
    )
181 182 183 184

    const m = new Message()
    m.room(this)

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
185
    const replyToList: Contact[] = [].concat(replyTo as any || [])
186

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
187 188 189 190 191 192 193
    if (replyToList.length > 0) {
      const mentionList = replyToList.map(c => '@' + c.name()).join(' ')
      m.content(mentionList + ' ' + content)
    } else {
      m.content(content)
    }
    // m.to(replyToList[0])
194 195 196 197 198

    return Config.puppetInstance()
                  .send(m)
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
199
  public get(prop): string { return (this.obj && this.obj[prop]) || (this.dirtyObj && this.dirtyObj[prop]) }
200

201
  private parse(rawObj: RoomRawObj): RoomObj | null {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
202
    if (!rawObj) {
203
      log.warn('Room', 'parse() on a empty rawObj?')
204
      return null
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
205
    }
Huan (李卓桓)'s avatar
#104  
Huan (李卓桓) 已提交
206

ruiruibupt's avatar
1  
ruiruibupt 已提交
207 208 209
    const memberList = this.parseMemberList(rawObj.MemberList)
    const nameMap    = this.parseMap(rawObj.MemberList, 'nick')
    const aliasMap   = this.parseMap(rawObj.MemberList, 'alias')
Huan (李卓桓)'s avatar
#104  
Huan (李卓桓) 已提交
210

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
211
    return {
Huan (李卓桓)'s avatar
#104  
Huan (李卓桓) 已提交
212 213 214 215
      id:         rawObj.UserName,
      encryId:    rawObj.EncryChatRoomId, // ???
      topic:      rawObj.NickName,
      ownerUin:   rawObj.OwnerUin,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
216

Huan (李卓桓)'s avatar
#104  
Huan (李卓桓) 已提交
217
      memberList,
218 219
      nameMap,
      aliasMap,
220 221 222
    }
  }

223 224
  private parseMemberList(rawMemberList: RoomRawMember[]): Contact[] {
    if (!rawMemberList || !rawMemberList.map) {
225 226
      return []
    }
227
    return rawMemberList.map(m => Contact.load(m.UserName))
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
228
  }
229 230
  private parseMap(memberList: RoomRawMember[], parseContent: NameType): Map<string, string> {
    const mapList: Map<string, string> = new Map<string, string>()
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
231
    if (memberList && memberList.map) {
Huan (李卓桓)'s avatar
#104  
Huan (李卓桓) 已提交
232
      memberList.forEach(member => {
233 234 235
        let tmpName: string
        let contact = Contact.load(member.UserName)
        switch (parseContent) {
ruiruibupt's avatar
1  
ruiruibupt 已提交
236 237
          case 'nick':
            tmpName = contact.alias() || contact.name()
238
            break
239
          case 'alias':
ruiruibupt's avatar
1  
ruiruibupt 已提交
240
            tmpName = member.DisplayName || contact.name()
241 242 243 244
            break
          default:
            throw new Error('parseMap failed, member not found')
        }
245 246
        /**
         * ISSUE #64 emoji need to be striped
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
247
         * ISSUE #104 never use remark name because sys group message will never use that
ruiruibupt's avatar
#217  
ruiruibupt 已提交
248
         * @rui: Wrong for 'never use remark name because sys group message will never use that', see more in the latest comment in #104
ruiruibupt's avatar
1  
ruiruibupt 已提交
249 250
         * @rui: webwx's NickName here return contactAlias, if not set contactAlias, return name
         * @rui: 2017-7-2 webwx's NickName just ruturn name, no contactAlias
251
         */
252
        mapList[member.UserName] = UtilLib.stripEmoji(tmpName)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
253
      })
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
254
    }
255
    return mapList
256 257
  }

258
  public dumpRaw() {
259
    console.error('======= dump raw Room =======')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
260
    Object.keys(this.rawObj).forEach(k => console.error(`${k}: ${this.rawObj[k]}`))
261
  }
262
  public dump() {
263
    console.error('======= dump Room =======')
264
    Object.keys(this.obj).forEach(k => console.error(`${k}: ${this.obj && this.obj[k]}`))
265 266
  }

Huan (李卓桓)'s avatar
#119  
Huan (李卓桓) 已提交
267
  public async add(contact: Contact): Promise<number> {
268
    log.verbose('Room', 'add(%s)', contact)
269 270 271 272 273

    if (!contact) {
      throw new Error('contact not found')
    }

Huan (李卓桓)'s avatar
#119  
Huan (李卓桓) 已提交
274 275 276
    const n = Config.puppetInstance()
                      .roomAdd(this, contact)
    return n
277 278
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
279
  public async del(contact: Contact): Promise<number> {
280
    log.verbose('Room', 'del(%s)', contact.name())
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
281 282 283 284

    if (!contact) {
      throw new Error('contact not found')
    }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
285
    const n = await Config.puppetInstance()
Huan (李卓桓)'s avatar
#119  
Huan (李卓桓) 已提交
286 287
                            .roomDel(this, contact)
                            .then(_ => this.delLocal(contact))
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
288
    return n
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
289 290
  }

291
  private delLocal(contact: Contact): number {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
292 293
    log.verbose('Room', 'delLocal(%s)', contact)

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
294
    const memberList = this.obj && this.obj.memberList
295
    if (!memberList || memberList.length === 0) {
296
      return 0 // already in refreshing
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
297 298 299
    }

    let i
300 301
    for (i = 0; i < memberList.length; i++) {
      if (memberList[i].id === contact.id) {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
302 303 304
        break
      }
    }
305 306
    if (i < memberList.length) {
      memberList.splice(i, 1)
307
      return 1
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
308
    }
309
    return 0
310
  }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
311

312
  public quit() {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
313 314
    throw new Error('wx web not implement yet')
    // WechatyBro.glue.chatroomFactory.quit("@@1c066dfcab4ef467cd0a8da8bec90880035aa46526c44f504a83172a9086a5f7"
315
  }
316

317 318 319 320 321 322 323 324 325 326
  /**
   * get topic
   */
  public topic(): string
  /**
   * set topic
   */
  public topic(newTopic: string): void

  public topic(newTopic?: string): string | void {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
327
    if (!this.isReady()) {
328
      log.warn('Room', 'topic() room not ready')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
329 330
    }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
331 332
    if (newTopic) {
      log.verbose('Room', 'topic(%s)', newTopic)
333
      Config.puppetInstance().roomTopic(this, newTopic)
334 335 336 337 338 339 340 341 342
                              .catch(e => {
                                log.warn('Room', 'topic(newTopic=%s) exception: %s',
                                                  newTopic, e && e.message || e
                                )
                              })
      if (!this.obj) {
        this.obj = <RoomObj>{}
      }
      Object.assign(this.obj, { topic: newTopic })
343
      return
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
344
    }
345
    return UtilLib.plainText(this.obj ? this.obj.topic : '')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
346 347
  }

348
  // should be deprecated
349
  public nick(contact: Contact): string {
ruiruibupt's avatar
1  
ruiruibupt 已提交
350
    log.warn('Room', 'nick(Contact) DEPRECATED, use alias(Contact) instead.')
ruiruibupt's avatar
#217  
ruiruibupt 已提交
351
    return this.alias(contact)
352 353
  }

ruiruibupt's avatar
#217  
ruiruibupt 已提交
354
  public alias(contact: Contact): string {
ruiruibupt's avatar
1  
ruiruibupt 已提交
355
    if (!this.obj) {
356 357
      return ''
    }
ruiruibupt's avatar
1  
ruiruibupt 已提交
358
    return this.obj.aliasMap[contact.id]
359 360
  }

361
  public has(contact: Contact): boolean {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
362
    if (!this.obj || !this.obj.memberList) {
363 364 365 366 367 368 369
      return false
    }
    return this.obj.memberList
                    .filter(c => c.id === contact.id)
                    .length > 0
  }

370
  public owner(): Contact | null {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
371 372
    const ownerUin = this.obj && this.obj.ownerUin
    let memberList = (this.obj && this.obj.memberList) || []
373 374 375 376 377 378 379 380

    let user = Config.puppetInstance()
                      .user

    if (user && user.get('uin') === ownerUin) {
      return user
    }

381
    memberList = memberList.filter(m => m.get('uin') === ownerUin)
382 383 384
    if (memberList.length > 0) {
      return memberList[0]
    }
J
jaslin 已提交
385 386

    if (this.rawObj.ChatRoomOwner) {
387 388 389 390
      return Contact.load(this.rawObj.ChatRoomOwner)
    }

    return null
391 392
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
393
  /**
ruiruibupt's avatar
1  
ruiruibupt 已提交
394 395
   * find member priority by `name`(contactAlias) / `alias`(roomAlias)
   * when use member(name:string), equals to member({name:string})
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
396
   */
397 398 399 400 401 402

  public member(filter: MemberQueryFilter): Contact | null
  public member(name: string): Contact | null

  public member(queryArg: MemberQueryFilter | string): Contact | null {
    if (typeof queryArg === 'string') {
ruiruibupt's avatar
1  
ruiruibupt 已提交
403
      return this.member({name: queryArg})
404 405 406 407 408 409 410 411 412
    }

    log.silly('Room', 'member({ %s })'
                        , Object.keys(queryArg)
                                .map(k => `${k}: ${queryArg[k]}`)
                                .join(', ')
            )

    if (Object.keys(queryArg).length !== 1) {
ruiruibupt's avatar
1  
ruiruibupt 已提交
413
      throw new Error('Room member find queryArg only support one key. multi key support is not availble now.')
414
    }
415

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
416
    if (!this.obj || !this.obj.memberList) {
417
      log.warn('Room', 'member() not ready')
418 419
      return null
    }
420
    let filterKey            = Object.keys(queryArg)[0]
421 422 423
    /**
     * ISSUE #64 emoji need to be striped
     */
424 425 426
    let filterValue: string  = UtilLib.stripEmoji(queryArg[filterKey])

    const keyMap = {
427 428
      name:       'nameMap',
      alias:      'aliasMap',
429 430 431 432 433 434 435 436 437 438
    }

    filterKey = keyMap[filterKey]
    if (!filterKey) {
      throw new Error('unsupport filter key')
    }

    if (!filterValue) {
      throw new Error('filterValue not found')
    }
439

440 441 442
    const filterMap = this.obj[filterKey]
    const idList = Object.keys(filterMap)
                          .filter(k => filterMap[k] === filterValue)
443

ruiruibupt's avatar
1  
ruiruibupt 已提交
444
    log.silly('Room', 'member() check %s from %s: %s', filterValue, filterKey, JSON.stringify(filterMap))
445

446 447 448 449 450
    if (idList.length) {
      return Contact.load(idList[0])
    } else {
      return null
    }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
451 452
  }

453
  public memberList(): Contact[] {
454
    log.verbose('Room', 'memberList')
455 456 457

    if (!this.obj || !this.obj.memberList || this.obj.memberList.length < 1) {
      log.warn('Room', 'memberList() not ready')
458
      return []
459 460 461 462
    }
    return this.obj.memberList
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
463
  public static create(contactList: Contact[], topic?: string): Promise<Room> {
464
    log.verbose('Room', 'create(%s, %s)', contactList.join(','), topic)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
465

Huan (李卓桓)'s avatar
bug fix  
Huan (李卓桓) 已提交
466
    if (!contactList || !Array.isArray(contactList)) {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
467 468
      throw new Error('contactList not found')
    }
469

470
    return Config.puppetInstance()
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
471
                  .roomCreate(contactList, topic)
472 473 474
                  .catch(e => {
                    log.error('Room', 'create() exception: %s', e && e.stack || e.message || e)
                    throw e
475
                  })
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
476 477
  }

478 479 480 481
  public static async findAll(query?: RoomQueryFilter): Promise<Room[]> {
    if (!query) {
      query = { topic: /.*/ }
    }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
482
    log.verbose('Room', 'findAll({ topic: %s })', query.topic)
483

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
484
    let topicFilter = query.topic
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
485

486 487
    if (!topicFilter) {
      throw new Error('topicFilter not found')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
488 489
    }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
490 491
    let filterFunction: string

492
    if (topicFilter instanceof RegExp) {
493
      filterFunction = `(function (c) { return ${topicFilter.toString()}.test(c) })`
494
    } else if (typeof topicFilter === 'string') {
495
      topicFilter = topicFilter.replace(/'/g, '\\\'')
496
      filterFunction = `(function (c) { return c === '${topicFilter}' })`
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
497
    } else {
498
      throw new Error('unsupport topic type')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
499 500
    }

501 502 503
    return Config.puppetInstance()
                  .roomFind(filterFunction)
                  .catch(e => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
504
                    log.verbose('Room', 'findAll() rejected: %s', e.message)
505 506
                    return [] // fail safe
                  })
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
507 508
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
509
  public static async find(query: RoomQueryFilter): Promise<Room> {
510
    log.verbose('Room', 'find({ topic: %s })', query.topic)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
511

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
512 513 514 515
    const roomList = await Room.findAll(query)
    if (!roomList || roomList.length < 1) {
      throw new Error('no room found')
    }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
516 517 518
    const room = roomList[0]
    await room.ready()
    return room
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
519 520
  }

521 522 523 524
  public static load(id: string): Room {
    if (!id) {
      throw new Error('Room.load() no id')
    }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
525 526 527 528 529 530 531

    if (id in Room.pool) {
      return Room.pool[id]
    }
    return Room.pool[id] = new Room(id)
  }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
532
}