Skip to content

Commit

Permalink
feat(webgroup): new onMyId callback
Browse files Browse the repository at this point in the history
  • Loading branch information
kalitine committed Aug 1, 2018
1 parent 6d7ad97 commit a8fa20d
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 26 deletions.
1 change: 1 addition & 0 deletions src/Bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export class Bot {
}
wc.init(key as string, wcId)
this.onWebGroup(wg)
wc.onMyId(wc.myId)
webSocketBuilder = wc.webSocketBuilder
} else {
const wg = this.webGroups.get(wcId) as WebGroup
Expand Down
3 changes: 3 additions & 0 deletions src/WebChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export class WebChannel implements IStream<OutWcMessage, InWcMsg> {
public onMemberJoin: (id: number) => void
public onMemberLeave: (id: number) => void
public onMessage: (id: number, msg: UserDataType) => void
public onMyId: (id: number) => void

public webSocketBuilder: WebSocketBuilder
public channelBuilder: ChannelBuilder
Expand Down Expand Up @@ -109,6 +110,7 @@ export class WebChannel implements IStream<OutWcMessage, InWcMsg> {
this.onMemberJoin = function none() {}
this.onMemberLeave = function none() {}
this.onMessage = function none() {}
this.onMyId = function none() {}
this.onStateChange = function none() {}
this.onSignalingStateChange = function none() {}

Expand Down Expand Up @@ -236,6 +238,7 @@ export class WebChannel implements IStream<OutWcMessage, InWcMsg> {
this.members = [this.myId]
this.key = key
this.rejoinEnabled = this.autoRejoin
this.onMyId(this.myId)
if (this.rejoinTimer) {
clearTimeout(this.rejoinTimer)
this.rejoinTimer = undefined
Expand Down
45 changes: 19 additions & 26 deletions src/WebChannelFacade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export class WebGroup {
public signalingServer: string
public autoRejoin: boolean
public onMessage: ((id: number, data: DataType) => void) | undefined | null
public onMyId: ((id: number) => void) | undefined | null
public onMemberJoin: ((id: number) => void) | undefined | null
public onMemberLeave: ((id: number) => void) | undefined | null
public onStateChange: ((state: number) => void) | undefined | null
Expand Down Expand Up @@ -188,7 +189,6 @@ export class WebGroup {
/**
* This handler is called when a message has been received from the group.
* `id` is an identifier of the member who sent this message.
* and false if sent via {@link WebGroup#sendTo}.
* @type {function(id: number, data: DataType)}
*/
this.onMessage = undefined as any
Expand All @@ -197,11 +197,20 @@ export class WebGroup {
enumerable: true,
get: () => (wc.onMessage.name === 'none' ? undefined : wc.onMessage),
set: (handler: (id: number, data: DataType) => void) => {
if (typeof handler !== 'function') {
wc.onMessage = function none() {}
} else {
wc.onMessage = handler
}
wc.onMessage = typeof handler !== 'function' ? function none() {} : handler
},
})
/**
* This handler is called when `myId` is set.
* @type {function(myId: number)}
*/
this.onMyId = undefined as any
Reflect.defineProperty(this, 'onMyId', {
configurable: true,
enumerable: true,
get: () => (wc.onMyId.name === 'none' ? undefined : wc.onMyId),
set: (handler: (myId: number) => void) => {
wc.onMyId = typeof handler !== 'function' ? function none() {} : handler
},
})
/**
Expand All @@ -214,11 +223,7 @@ export class WebGroup {
enumerable: true,
get: () => (wc.onMemberJoin.name === 'none' ? undefined : wc.onMemberJoin),
set: (handler: (id: number) => void) => {
if (typeof handler !== 'function') {
wc.onMemberJoin = function none() {}
} else {
wc.onMemberJoin = handler
}
wc.onMemberJoin = typeof handler !== 'function' ? function none() {} : handler
},
})
/**
Expand All @@ -231,11 +236,7 @@ export class WebGroup {
enumerable: true,
get: () => (wc.onMemberLeave.name === 'none' ? undefined : wc.onMemberLeave),
set: (handler: (id: number) => void) => {
if (typeof handler !== 'function') {
wc.onMemberLeave = function none() {}
} else {
wc.onMemberLeave = handler
}
wc.onMemberLeave = typeof handler !== 'function' ? function none() {} : handler
},
})
/**
Expand All @@ -248,11 +249,7 @@ export class WebGroup {
enumerable: true,
get: () => (wc.onStateChange.name === 'none' ? undefined : wc.onStateChange),
set: (handler: (state: WebChannelState) => void) => {
if (typeof handler !== 'function') {
wc.onStateChange = function none() {}
} else {
wc.onStateChange = handler
}
wc.onStateChange = typeof handler !== 'function' ? function none() {} : handler
},
})
/**
Expand All @@ -266,11 +263,7 @@ export class WebGroup {
get: () =>
wc.onSignalingStateChange.name === 'none' ? undefined : wc.onSignalingStateChange,
set: (handler: (state: SignalingState) => void) => {
if (typeof handler !== 'function') {
wc.onSignalingStateChange = function none() {}
} else {
wc.onSignalingStateChange = handler
}
wc.onSignalingStateChange = typeof handler !== 'function' ? function none() {} : handler
},
})
}
Expand Down
20 changes: 20 additions & 0 deletions test/functional/1-bot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ describe('🤖 - 1 bot', () => {
})
})

/** @test {WebGroup#onMyId} */
it('should be called', (done) => {
botWaitJoin(key)
.then(() => botGetData(key))
.then((data) => {
expect(data.onMyIdToBeCalled).toEqual(1)
expect(data.messages).toEqual([])
done()
})
})

/** @test {WebGroup#autoRejoin} */
it('autoRejoin should be disabled', (done) => {
botWaitJoin(key)
Expand Down Expand Up @@ -243,6 +254,15 @@ describe('🤖 - 1 bot', () => {
})
})

/** @test {WebGroup#onMyId} */
it('should NOT be called', (done) => {
botLeave(key).then((data) => {
expect(data.onMyIdToBeCalled).toEqual(1)
expect(data.messages).toEqual([])
done()
})
})

/** @test {WebGroup#autoRejoin} */
it('autoRejoin should be disabled', (done) => {
botLeave(key).then((data) => {
Expand Down
38 changes: 38 additions & 0 deletions test/functional/1-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ describe('🙂 - 1 client', () => {
expect(wg.onMemberJoin).toBeUndefined()
expect(wg.onMemberLeave).toBeUndefined()
expect(wg.onMessage).toBeUndefined()
expect(wg.onMyId).toBeUndefined()
expect(wg.onStateChange).toBeUndefined()
expect(wg.onSignalingStateChange).toBeUndefined()

Expand Down Expand Up @@ -221,6 +222,26 @@ describe('🙂 - 1 client', () => {
wg1.join()
})

/** @test {WebGroup#onMyId} */
it('should be called', (done) => {
let called1 = 0

wg1.onMyId = (id) => {
expect(id).toEqual(wg1.myId)
called1++
}
wg1.onStateChange = (state: WebGroupState) => {
if (state === WebGroupState.JOINED) {
wait(1000).then(() => {
expect(called1).toEqual(1)
done()
})
}
}

wg1.join()
})

/** @test {WebGroup#members} */
it('should have only me as a member', (done) => {
wg1.onStateChange = (state: WebGroupState) => {
Expand Down Expand Up @@ -488,6 +509,23 @@ describe('🙂 - 1 client', () => {
wg1.leave()
})

/** @test {WebGroup#onMyId} */
it('should NOT be called', (done) => {
let called1 = 0

wg1.onMyId = () => called1++
wg1.onStateChange = (state: WebGroupState) => {
if (state === WebGroupState.LEFT) {
wait(1000).then(() => {
expect(called1).toEqual(0)
done()
})
}
}

wg1.leave()
})

/** @test {WebGroup#members} */
it('should have no members', (done) => {
wg1.onStateChange = (state: WebGroupState) => {
Expand Down
17 changes: 17 additions & 0 deletions test/functional/2-bot_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,23 @@ describe('🤖 🙂 - 2 members: bot first, then client', () => {
client.join(key)
})

/** @test {WebGroup#onMyId} */
it('should be called', (done) => {
client.onMyId = (id) => called++

client.onStateChange = (state: WebGroupState) => {
if (state === WebGroupState.JOINED) {
botGetData(key).then((bot) => {
expect(called).toEqual(1)
expect(bot.onMyIdToBeCalled).toEqual(1)
done()
})
}
}

client.join(key)
})

/** @test {WebGroup#members} */
it('should have 2 members', (done) => {
client.onStateChange = (state: WebGroupState) => {
Expand Down
14 changes: 14 additions & 0 deletions test/functional/2-client_bot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,20 @@ describe('🙂 🤖 - 2 members: client invites bot', () => {
.catch(fail)
})

/** @test {WebGroup#onMyId} */
it('should be called', (done) => {
client.invite(BOT_URL)

// Check bot bot
botWaitJoin(client.key)
.then(() => botGetData(client.key))
.then((bot: IBotData) => {
expect(bot.onMyIdToBeCalled).toEqual(1)
done()
})
.catch(fail)
})

/** @test {WebGroup#members} */
it('should have 2 members', (done) => {
let _bot: IBotData
Expand Down
16 changes: 16 additions & 0 deletions test/functional/2-clients.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,22 @@ describe('🙂 🙂 - 2 clients', () => {
wg2.join(wg1.key)
})

/** @test {WebGroup#onMyId} */
it('should be called', (done) => {
// Code for peer 2
wg2.onMyId = () => called2++
wg2.onStateChange = (state: WebGroupState) => {
if (state === WebGroupState.JOINED) {
wait(1000).then(() => {
expect(called2).toEqual(1)
done()
})
}
}

wg2.join(wg1.key)
})

/** @test {WebGroup#members} */
it('should have 2 members', (done) => {
const queue = new Queue(3, () => {
Expand Down
2 changes: 2 additions & 0 deletions test/util/botServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ function configWebGroup(wg: WebGroup) {
signalingStates: [],
messages: [],
onMessageToBeCalled: 0,
onMyIdToBeCalled: 0,
signalingServer: '',
}
const anyWg = wg as any
Expand All @@ -137,6 +138,7 @@ function configWebGroup(wg: WebGroup) {
data.states.push(state)
}
})
wg.onMyId = (id) => data.onMyIdToBeCalled++
wg.onMessage = (id, msg) => {
data.onMessageToBeCalled++
data.messages.push({
Expand Down
1 change: 1 addition & 0 deletions test/util/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export interface IBotData {
signalingStates: number[]
messages: IBotMessage[]
onMessageToBeCalled: number
onMyIdToBeCalled: number
state: WebGroupState
signalingState: SignalingState
key: string
Expand Down

0 comments on commit a8fa20d

Please sign in to comment.