Skip to content

Commit

Permalink
fix send returns
Browse files Browse the repository at this point in the history
  • Loading branch information
RSamaium committed Nov 22, 2023
1 parent ee1c9d5 commit 7007559
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
4 changes: 3 additions & 1 deletion sync-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { User } from './user'
import { Transmitter } from './transmitter'
import MockSocketIo from './testing/mock-socket'
import { Utils } from './utils'
import { Transport } from './transports/socket'

export {
World,
Expand All @@ -20,5 +21,6 @@ export {
User,
Transmitter,
MockSocketIo,
Utils
Utils,
Transport
}
3 changes: 2 additions & 1 deletion sync-server/src/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ export class Room {
...this.memoryTotalObject,
join: firstJoin
}, <string>room.id)
Transmitter.emit(userProxy, packet, room)

await Transmitter.emit(userProxy, packet, room)

return true
}
Expand Down
18 changes: 11 additions & 7 deletions sync-server/src/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ export class WorldClass {
*
* @method send()
*/
send(): void {
this.rooms.forEach((room: any, id: string) => {
async send(): Promise<void> {
for (let [_, room] of this.rooms) {
const obj = room.$currentState()
if (Object.keys(obj).length == 0) {
return
Expand All @@ -155,12 +155,12 @@ export class WorldClass {
const packets = Transmitter.getPackets(room)
if (packets) {
for (let packet of packets) {
Transmitter.emit(user, packet, room)
await Transmitter.emit(user, packet, room)
}
}
}
room.$clearCurrentState()
})
}
Transmitter.clear()
}

Expand All @@ -170,9 +170,13 @@ export class WorldClass {
* @method connectUser()
* @param {object} socket
* @param {id} userId
* @param {object} options
* - getUserInstance: function that returns a new instance of the user
* @returns {User}
*/
connectUser<T = User>(socket, id: string): T {
connectUser<T = User>(socket, id: string, options: {
getUserInstance?: any
} = {}): T {
const existingUser = this.getUser(id, false)
if (existingUser) {
if (existingUser._timeoutDisconnect) {
Expand All @@ -183,7 +187,7 @@ export class WorldClass {
existingUser.$state = UserState.Connected
return existingUser as any
}
const user = new this.userClass()
const user = options.getUserInstance?.(socket) ?? new this.userClass()
user.id = id
socket.emit('uid', id)
this.setUser(user, socket)
Expand All @@ -200,7 +204,7 @@ export class WorldClass {
disconnectUser(userId: string): Promise<void> {
return new Promise((resolve: any, reject) => {
const user = this.getUser(userId)

if (!user) return resolve()

user.$state = UserState.Disconnected
Expand Down
10 changes: 5 additions & 5 deletions sync-server/tests/user.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ test('Change Room', () => {
await World.joinRoom(room1.id, CLIENT_ID)
user = World.getUser(CLIENT_ID)
user.position = {x: 10, y: 10}
World.send()
await World.send()

await World.leaveRoom(room1.id, CLIENT_ID)

Expand All @@ -147,10 +147,10 @@ test('Change Room', () => {
user = World.getUser(CLIENT_ID)
user.position.x = 20
user.position.y = 20
World.send()
await World.send()
})
})

// afterEach(() => {
// World.clear()
// })
afterEach(() => {
World.clear()
})

0 comments on commit 7007559

Please sign in to comment.