Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions templates/base/server/.hathora/methods.ts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export interface Methods<T> {
{{@key}}(state: T, userId: UserId, ctx: Context, request: {{makeRequestName @key}}): Response;
{{/each}}
getUserState(state: T, userId: UserId): UserState;
onUserJoin?(state: T, userId: UserId, ctx: Context): void;
onUserLeave?(state: T, userId: UserId, ctx: Context): void;
{{#if tick}}
onTick(state: T, ctx: Context, timeDelta: number): void;
{{/if}}
Expand Down
9 changes: 6 additions & 3 deletions templates/base/server/.hathora/store.ts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,17 @@ const store: Application = {
}
}
sendSnapshot(roomId, userId);
const { impl, chance } = rooms.get(roomId)!;
impl._onUserJoin(userId, ctx(chance, Date.now(), roomId));
Copy link
Contributor

@philihp philihp Jul 8, 2023

Choose a reason for hiding this comment

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

does this work if _onUserJoin is undefined? should it be

impl._onUserJoin?.(userId, ctx(chance, Date.now(), roomId));

Copy link
Contributor Author

@francislavoie francislavoie Jul 8, 2023

Choose a reason for hiding this comment

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

_onUserJoin is an internal function and should always be defined, and its job may be a no-op if the user's function is not defined. See the wrapper file.

},
async unsubscribeUser(roomId: RoomId, userId: UserId): Promise<void> {
if (!rooms.has(roomId)) {
return;
}
const subscribers = rooms.get(roomId)!.subscriptions;
subscribers.delete(userId);
if (subscribers.size === 0) {
const { impl, chance, subscriptions } = rooms.get(roomId)!;
impl._onUserLeave(userId, ctx(chance, Date.now(), roomId));
Copy link
Contributor

Choose a reason for hiding this comment

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

similarly

subscriptions.delete(userId);
if (subscriptions.size === 0) {
changedStates.delete(roomId);
pendingMessages.delete(roomId);
}
Expand Down
12 changes: 12 additions & 0 deletions templates/base/server/.hathora/wrapper.ts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ export class ImplWrapper {
const impl = await getLatestImpl();
return impl.getUserState(onChange.target(this.state), userId);
}
public async _onUserJoin(userId: UserId, ctx: Context) {
const impl = await getLatestImpl();
if (impl.onUserJoin !== undefined) {
await impl.onUserJoin(this.state, userId, ctx);
}
}
public async _onUserLeave(userId: UserId, ctx: Context) {
const impl = await getLatestImpl();
if (impl.onUserLeave !== undefined) {
await impl.onUserLeave(this.state, userId, ctx);
}
}
public changedAt(): number | undefined {
const res = this._changedAt;
this._changedAt = undefined;
Expand Down