Skip to content
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

Merged
merged 8 commits into from Mar 1, 2017
Merged

load all memberList #275

merged 8 commits into from Mar 1, 2017

Conversation

lijiarui
Copy link
Member

Copy link
Member

@huan huan left a 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.

@lijiarui
Copy link
Member Author

Then, what's your suggestion?

@huan
Copy link
Member

huan commented Feb 22, 2017

My suggestion is: keep thinking hard, instead of keep asking.

@lijiarui lijiarui closed this Feb 22, 2017
@huan
Copy link
Member

huan commented Feb 22, 2017

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.

@lijiarui lijiarui reopened this Feb 23, 2017
src/room.ts Outdated
nameMap: Map<string, string>,
aliasMap: Map<string, string>,
memberList?: Contact[],
nameMap?: Map<string, string>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right.

Copy link
Member

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()
Copy link
Member

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.

Copy link
Member Author

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')
Copy link
Member

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,
Copy link
Member

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 [].

@coveralls
Copy link

coveralls commented Feb 28, 2017

Coverage Status

Changes Unknown when pulling 2f11cc1 on lijiarui:273 into ** on wechaty:master**.

@coveralls
Copy link

coveralls commented Feb 28, 2017

Coverage Status

Changes Unknown when pulling c7e7cf3 on lijiarui:273 into ** on wechaty:master**.

Copy link
Member

@huan huan left a 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>,
Copy link
Member

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 = []
Copy link
Member

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 = []
Copy link
Member

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) {
Copy link
Member

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.

@lijiarui
Copy link
Member Author

You are right, it's really a bad design

@coveralls
Copy link

coveralls commented Feb 28, 2017

Coverage Status

Changes Unknown when pulling 56be013 on lijiarui:273 into ** on wechaty:master**.

Copy link
Member

@huan huan left a 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')
Copy link
Member

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)
Copy link
Member

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 = []
Copy link
Member

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.

@coveralls
Copy link

coveralls commented Feb 28, 2017

Coverage Status

Changes Unknown when pulling 81c2400 on lijiarui:273 into ** on wechaty:master**.

Copy link
Member

@huan huan left a 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 || [])
Copy link
Member

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')
Copy link
Member

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,
Copy link
Member

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.

Copy link
Member

@huan huan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better now.

@huan huan merged commit 52cde27 into wechaty:master Mar 1, 2017
@lijiarui lijiarui deleted the 273 branch March 1, 2017 11:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants