-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
load all memberList #275
load all memberList #275
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I don't think this can fix anything.
Then, what's your suggestion? |
My suggestion is: keep thinking hard, instead of keep asking. |
The best practice of making Pull Request is to keep improving it instead of closing it because close it will lose all the review information. |
src/room.ts
Outdated
nameMap: Map<string, string>, | ||
aliasMap: Map<string, string>, | ||
memberList?: Contact[], | ||
nameMap?: Map<string, string>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why I said "right" is because you starting to notice the right problem.
But what you did modify here is not correct.
the memberList
, nameMap
and aliasMap
should not defined by ?
, which means they should always not to be undefined
or null
.
If there's no data for them, they should be empty array, or a empty {}
.
src/room.ts
Outdated
@@ -130,11 +130,17 @@ export class Room extends EventEmitter implements Sayable { | |||
const data = await contactGetter(this.id) | |||
log.silly('Room', `contactGetter(${this.id}) resolved`) | |||
this.rawObj = data | |||
if (!this.rawObj.MemberList) { | |||
return this.ready() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should not do this because this will cause very bad problems.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, sorry, I added before and forgot to delete it.......
src/room.ts
Outdated
await this.readyAllMembers(this.rawObj.MemberList) | ||
this.obj = this.parse(this.rawObj) | ||
if (!this.obj) { | ||
throw new Error('no this.obj set after contactGetter') | ||
} | ||
if (!this.obj.memberList) { | ||
throw new Error('no this.obj.memberLit set after contactGetter') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should not throw here.
memberList is empty should be treated a normal situation, because the data is async.
If this.rawObj.MemberList is undefined
, we should init obj.memberList
with []
.
src/room.ts
Outdated
|
||
return { | ||
id: rawObj.UserName, | ||
encryId: rawObj.EncryChatRoomId, // ??? | ||
topic: rawObj.NickName, | ||
ownerUin: rawObj.OwnerUin, | ||
|
||
memberList, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this.rawObj.MemberList is undefined
, we should init memberList with []
.
Changes Unknown when pulling 2f11cc1 on lijiarui:273 into ** on wechaty:master**. |
Changes Unknown when pulling c7e7cf3 on lijiarui:273 into ** on wechaty:master**. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rethink the design.
src/room.ts
Outdated
nameMap: Map<string, string>, | ||
aliasMap: Map<string, string>, | ||
memberList?: Contact[], | ||
nameMap?: Map<string, string>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why I said "right" is because you starting to notice the right problem.
But what you did modify here is not correct.
the memberList
, nameMap
and aliasMap
should not defined by ?
, which means they should always not to be undefined
or null
.
If there's no data for them, they should be empty array, or a empty {}
.
src/room.ts
Outdated
@@ -130,11 +130,17 @@ export class Room extends EventEmitter implements Sayable { | |||
const data = await contactGetter(this.id) | |||
log.silly('Room', `contactGetter(${this.id}) resolved`) | |||
this.rawObj = data | |||
if (!this.rawObj.MemberList) { | |||
this.rawObj.MemberList = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not modify rawObj
, because this data comes from Browser, which should be treated as immutable.
Do this job on this.obj
instead.
src/room.ts
Outdated
await this.readyAllMembers(this.rawObj.MemberList) | ||
this.obj = this.parse(this.rawObj) | ||
if (!this.obj) { | ||
throw new Error('no this.obj set after contactGetter') | ||
} | ||
if (!this.obj.memberList) { | ||
this.obj.memberList = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not do this job here.
Do this job inside this.parse
instead.
src/room.ts
Outdated
@@ -204,6 +210,16 @@ export class Room extends EventEmitter implements Sayable { | |||
return null | |||
} | |||
|
|||
if (!rawObj.MemberList) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Get rid of this if
, and also get rid of return
inside it.
We should prevent to use multiple return statement for the final data.
Only return the final data at the end of function statements. The "return"s before that should only return for the error conditions.
You are right, it's really a bad design |
Changes Unknown when pulling 56be013 on lijiarui:273 into ** on wechaty:master**. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know what are you thinking. It seems you had lost your mind.
src/room.ts
Outdated
@@ -130,6 +130,9 @@ export class Room extends EventEmitter implements Sayable { | |||
const data = await contactGetter(this.id) | |||
log.silly('Room', `contactGetter(${this.id}) resolved`) | |||
this.rawObj = data | |||
if (!this.rawObj.MemberList) { | |||
throw new Error('no this.obj.memberList, set after contactGetter') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I said before: should NOT throw when !this.rawObj.MemberList
.
Because this is OK.
src/room.ts
Outdated
@@ -130,6 +130,9 @@ export class Room extends EventEmitter implements Sayable { | |||
const data = await contactGetter(this.id) | |||
log.silly('Room', `contactGetter(${this.id}) resolved`) | |||
this.rawObj = data | |||
if (!this.rawObj.MemberList) { | |||
throw new Error('no this.obj.memberList, set after contactGetter') | |||
} | |||
await this.readyAllMembers(this.rawObj.MemberList) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
await this.readyAllMembers(this.rawObj.MemberList || [])
src/room.ts
Outdated
@@ -203,6 +206,9 @@ export class Room extends EventEmitter implements Sayable { | |||
log.warn('Room', 'parse() on a empty rawObj?') | |||
return null | |||
} | |||
if (!rawObj.MemberList) { | |||
rawObj.MemberList = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Delete them.
As I mentioned before, you should NOT change any value of rawObj
.
Changes Unknown when pulling 81c2400 on lijiarui:273 into ** on wechaty:master**. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After this, I believe the code change can be reduced to no more than 5 lines.
src/room.ts
Outdated
@@ -130,7 +130,7 @@ export class Room extends EventEmitter implements Sayable { | |||
const data = await contactGetter(this.id) | |||
log.silly('Room', `contactGetter(${this.id}) resolved`) | |||
this.rawObj = data | |||
await this.readyAllMembers(this.rawObj.MemberList) | |||
await this.readyAllMembers(this.rawObj.MemberList || []) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not do it here.
Do it inside readyAllMembers()
src/room.ts
Outdated
const aliasMap = this.parseMap(rawObj.MemberList, 'alias') | ||
const memberList = this.parseMemberList(rawObj.MemberList || []) | ||
const nameMap = this.parseMap(rawObj.MemberList || [], 'name') | ||
const aliasMap = this.parseMap(rawObj.MemberList || [], 'alias') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The above three: Do not do it here.
Do it inside parse*()
function.
src/room.ts
Outdated
nameMap, | ||
aliasMap, | ||
memberList: memberList, | ||
nameMap: nameMap, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should keep using memberList
instead of memberList: memberList
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better now.
#273