提交 f6ec50f4 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

code clean

上级 81112d56
......@@ -4,16 +4,21 @@ import * as test from 'blue-tape'
import { cloneClass } from 'clone-class'
import { Contact } from './contact'
import { Contact as GlobalContact } from './contact'
// tslint:disable-next-line:variable-name
const Contact = cloneClass(GlobalContact)
test('Should not be able to instanciate directly', async t => {
// tslint:disable-next-line:variable-name
const MyContact = cloneClass(Contact)
t.throws(() => {
const c = new Contact('xxx')
const c = new MyContact('xxx')
t.fail(c.name())
}, 'should throw when `new Contact()`')
t.throws(() => {
const c = Contact.load('xxx')
const c = MyContact.load('xxx')
t.fail(c.name())
}, 'should throw when `Contact.load()`')
})
......@@ -34,7 +39,7 @@ test('Should not be able to instanciate through cloneClass without puppet', asyn
})
test('Should be able to instanciate through cloneClass with puppet', async t => {
test('should be able to instanciate through cloneClass with puppet', async t => {
// tslint:disable-next-line:variable-name
const MyContact = cloneClass(Contact)
MyContact.puppet = {} as any
......@@ -50,3 +55,11 @@ test('Should be able to instanciate through cloneClass with puppet', async t =>
}, 'should not throw when `MyContact.load()`')
})
test('should throw when instanciate the global class', async t => {
t.throws(() => {
const c = GlobalContact.load('xxx')
t.fail('should not run to here')
t.fail(c.toString())
}, 'should throw when we instanciate a global class')
})
......@@ -93,11 +93,9 @@ export class Contact extends PuppetAccessory implements Sayable {
id : string,
): T['prototype'] {
if (!this.pool) {
log.verbose('Contact', 'load(%s) init pool', id)
this.pool = new Map<string, Contact>()
}
if (this === Contact) {
throw new Error('must not use the global Contact. use a cloned child via cloneClass instead.')
}
if (this.pool === Contact.pool) {
throw new Error('the current pool is equal to the global pool error!')
}
......@@ -213,7 +211,10 @@ export class Contact extends PuppetAccessory implements Sayable {
const MyClass = instanceToClass(this, Contact)
if (MyClass === Contact) {
throw new Error('Contact class can not be instanciated directly! See: https://github.com/Chatie/wechaty/issues/1217')
throw new Error(
'Contact class can not be instanciated directly!'
+ 'See: https://github.com/Chatie/wechaty/issues/1217',
)
}
if (!this.puppet) {
......
......@@ -75,7 +75,7 @@ export interface RoomPayload {
*/
export class Room extends PuppetAccessory implements Sayable {
protected static readonly pool = new Map<string, Room>()
protected static pool: Map<string, Room>
/**
* Create a new room.
......@@ -175,6 +175,10 @@ export class Room extends PuppetAccessory implements Sayable {
throw new Error('no id')
}
if (!this.pool) {
this.pool = new Map<string, Room>()
}
const existingRoom = this.pool.get(id)
if (existingRoom) {
return existingRoom
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册