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

#40 Typescript transition step 12: fix contact.ts & tslint

上级 26da6ba6
......@@ -11,20 +11,54 @@ import UtilLib from './util-lib'
import log from './brolog-env'
type ContactObj = {
id: string
uin: string
name: string
remark: string
weixin: string
sex: string
province: string
city: string
signature: string
address: string
stranger: boolean
star: boolean
}
type ContactRawObj = {
UserName: string
Uin: string
Alias: string
RemarkName: string
Sex: string
Province: string
City: string
NickName: string
StarFriend: string
stranger: string
Signature: string
}
class Contact {
constructor(id) {
private static pool = new Map<string, Contact>()
private obj: ContactObj
private rawObj: ContactRawObj
constructor(public readonly id: string) {
log.silly('Contact', `constructor(${id})`)
if (id && typeof id !== 'string') { throw new Error('id must be string if provided. we got: ' + typeof id) }
this.id = id
this.obj = {}
if (typeof id !== 'string') {
throw new Error('id must be string. found: ' + typeof id)
}
}
toString() { return this.id }
toStringEx() { return `Contact(${this.obj.name}[${this.id}])` }
public toString() { return this.id }
public toStringEx() { return `Contact(${this.obj && this.obj.name}[${this.id}])` }
parse(rawObj) {
return !rawObj ? {} : {
private parse(rawObj: ContactRawObj): ContactObj {
return !rawObj ? null : {
id: rawObj.UserName
, uin: rawObj.Uin // stable id: 4763975 || getCookie("wxuin")
, weixin: rawObj.Alias // Wechat ID
......@@ -42,14 +76,14 @@ class Contact {
}
}
name() { return UtilLib.plainText(this.obj.name) }
remark() { return this.obj.remark }
stranger() { return this.obj.stranger }
star() { return this.obj.star }
public name() { return UtilLib.plainText(this.obj && this.obj.name) }
public remark() { return this.obj && this.obj.remark }
public stranger() { return this.obj && this.obj.stranger }
public star() { return this.obj && this.obj.star }
get(prop) { return this.obj[prop] }
public get(prop) { return this.obj && this.obj[prop] }
ready(contactGetter) {
public ready(contactGetter?: (id: string) => Promise<ContactRawObj>): Promise<Contact> {
log.silly('Contact', 'ready(' + (contactGetter ? typeof contactGetter : '') + ')')
if (!this.id) {
log.warn('Contact', 'ready() call on an un-inited contact')
......@@ -79,17 +113,17 @@ class Contact {
})
}
dumpRaw() {
public dumpRaw() {
console.error('======= dump raw contact =======')
Object.keys(this.rawObj).forEach(k => console.error(`${k}: ${this.rawObj[k]}`))
}
dump() {
public dump() {
console.error('======= dump contact =======')
Object.keys(this.obj).forEach(k => console.error(`${k}: ${this.obj[k]}`))
}
// private
static _find({
private static _find({
name
}) {
log.silly('Cotnact', '_find(%s)', name)
......@@ -118,14 +152,14 @@ class Contact {
})
}
static find({
public static find({
name
}) {
log.verbose('Contact', 'find(%s)', name)
return Contact._find({name})
.then(idList => {
if (!idList || !Array.isArray(idList)){
if (!idList || !Array.isArray(idList)) {
throw new Error('_find return error')
}
if (idList.length < 1) {
......@@ -140,7 +174,7 @@ class Contact {
})
}
static findAll({
public static findAll({
name
}) {
log.verbose('Contact', 'findAll(%s)', name)
......@@ -148,7 +182,7 @@ class Contact {
return Contact._find({name})
.then(idList => {
// console.log(idList)
if (!idList || !Array.isArray(idList)){
if (!idList || !Array.isArray(idList)) {
throw new Error('_find return error')
}
if (idList.length < 1) {
......@@ -162,21 +196,19 @@ class Contact {
})
}
}
Contact.init = function() { Contact.pool = {} }
Contact.init()
public static load(id: string) {
if (!id || typeof id !== 'string') {
return null
}
Contact.load = function(id) {
if (!id || typeof id !== 'string') {
return null
if (!(id in Contact.pool)) {
Contact.pool[id] = new Contact(id)
}
return Contact.pool[id]
}
if (!(id in Contact.pool)) {
Contact.pool[id] = new Contact(id)
}
return Contact.pool[id]
}
// Contact.search = function(options) {
// if (options.name) {
// const regex = new RegExp(options.name)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册