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

Add optional onUserJoin and onUserLeave hooks #467

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

francislavoie
Copy link
Contributor

@francislavoie francislavoie commented Jun 21, 2023

I think this would help resolve #196.

Adds optional onUserJoin and onUserLeave methods to the Methods interface which can be implemented in Impl to react to users joining and leaving. This can be useful to clean up InternalState immediately when users leave.

For example, the Uno demo could be updated to remove that user's entry from hands in state if the user leaves (contrived example, obviously that wouldn't make sense in an IRL game of Uno because players can't really join/leave in the middle of a match).

Implementation detail: I used _ prefixed methods in the wrapper because I think we should pass this.state through, and that seems to be the pattern used here.

I didn't test this yet because I didn't look into how to use this repo as-is, so this is probably WIP if it needs tests added or some code to be generated.

@@ -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.

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

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.

Check if player is still online
2 participants