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

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

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