Skip to content

Commit

Permalink
catch error on main funciton. (#989)
Browse files Browse the repository at this point in the history
fix multiple clalback onPlayer
  • Loading branch information
gonpombo8 authored Aug 9, 2024
1 parent 369e5b3 commit b049f13
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
13 changes: 9 additions & 4 deletions packages/@dcl/sdk-commands/src/logic/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,16 @@ for (const path in compositeFromLoader) {
if ((entrypoint as any).main !== undefined) {
function _INTERNAL_startup_system() {
const maybePromise = (entrypoint as any).main()
if (maybePromise && typeof maybePromise === 'object' && typeof (maybePromise as unknown as Promise<unknown>).then === 'function') {
maybePromise.catch(console.error)
try {
const maybePromise = (entrypoint as any).main()
if (maybePromise && typeof maybePromise === 'object' && typeof (maybePromise as unknown as Promise<unknown>).then === 'function') {
maybePromise.catch(console.error)
}
} catch (e) {
console.error(e)
} finally {
engine.removeSystem(_INTERNAL_startup_system)
}
engine.removeSystem(_INTERNAL_startup_system)
}
engine.addSystem(_INTERNAL_startup_system, Infinity)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/@dcl/sdk/src/network/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function entityUtils(engine: IEngine, profile: IProfile) {
let componentsIdsMutable = [...componentIds]
// Profile not initialized
if (!profile?.networkId) {
throw new Error('Profile not initialized. Called syncEntity inside the main() function.')
throw new Error('Profile not initialized. Call syncEntity inside the main() function.')
}

// We use the networkId generated by the user address to identify this entity through the network
Expand Down
16 changes: 8 additions & 8 deletions packages/@dcl/sdk/src/players/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ function definePlayerHelper(engine: IEngine) {
const AvatarBase = defineAvatarBase(engine)
const playerEntities = new Map<Entity, string>()

let onEnterSceneCb: ((player: GetPlayerDataRes) => void) | undefined = undefined
let onLeaveSceneCb: ((userId: string) => void) | undefined = undefined
const onEnterSceneCb: ((player: GetPlayerDataRes) => void)[] = []
const onLeaveSceneCb: ((userId: string) => void)[] = []

engine.addSystem(() => {
const players = Array.from(engine.getEntitiesWith(PlayerIdentityData, AvatarBase))
Expand All @@ -41,14 +41,14 @@ function definePlayerHelper(engine: IEngine) {
playerEntities.set(entity, identity.address)

// Call onEnter callback
if (onEnterSceneCb) {
onEnterSceneCb(getPlayer({ userId: identity.address })!)
if (onEnterSceneCb.length) {
onEnterSceneCb.forEach((cb) => cb(getPlayer({ userId: identity.address })!))
}

// Check for changes/remove callbacks
AvatarBase.onChange(entity, (value) => {
if (!value && onLeaveSceneCb && playerEntities.get(entity)) {
onLeaveSceneCb(playerEntities.get(entity)!)
if (!value && onLeaveSceneCb.length && playerEntities.get(entity)) {
onLeaveSceneCb.forEach((cb) => cb(playerEntities.get(entity)!))
playerEntities.delete(entity)
}
})
Expand All @@ -58,10 +58,10 @@ function definePlayerHelper(engine: IEngine) {

return {
onEnterScene(cb: (player: GetPlayerDataRes) => void) {
onEnterSceneCb = cb
onEnterSceneCb.push(cb)
},
onLeaveScene(cb: (userId: string) => void) {
onLeaveSceneCb = cb
onLeaveSceneCb.push(cb)
},
/**
* Returns the info of the player if it's in the scene.
Expand Down

0 comments on commit b049f13

Please sign in to comment.