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

feat(client-presence): Presence emitsworkspaceActivatedwhen a new workspace is received #23917

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

WillieHabi
Copy link
Contributor

@WillieHabi WillieHabi commented Feb 24, 2025

Description

Follow up to work item AB#29541 and change #23809

When Presence receives an unregistered workspace in a datastore update message, we emit a new 'workspaceActivated' event.

Added two more tests as well for invalid workspace addresses (both public and internal).

Fixes AB#29939

@github-actions github-actions bot added area: framework Framework is a tag for issues involving the developer framework. Eg Aqueduct base: main PRs targeted against main branch labels Feb 24, 2025
@WillieHabi WillieHabi requested a review from jason-ha February 24, 2025 21:09
@WillieHabi WillieHabi marked this pull request as ready for review February 24, 2025 21:09
@Copilot Copilot bot review requested due to automatic review settings February 24, 2025 21:09

Choose a reason for hiding this comment

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

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (2)

packages/framework/presence/src/presenceDatastoreManager.ts:90

  • [nitpick] The function name 'isPresenceWorkspaceAddress' is misleading. Consider renaming it to 'hasColonInAddress' or something similar.
function isPresenceWorkspaceAddress(

packages/framework/presence/src/presenceDatastoreManager.ts:427

  • [nitpick] The variable name 'internalWorkspaceTypes' could be more descriptive. Consider renaming it to 'workspaceTypeMappings' or something similar.
const internalWorkspaceTypes = {
@WillieHabi WillieHabi requested a review from jason-ha February 25, 2025 17:53
Copy link
Contributor

@jason-ha jason-ha left a comment

Choose a reason for hiding this comment

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

Missing coverage and implementation for the main scenario - see comment for the test file.

Comment on lines +386 to +387
// Invalid public address (must be `${string}:${string}`)
"s:testStateWorkspace": statesWorkspaceUpdate,
Copy link
Contributor

Choose a reason for hiding this comment

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

"s:testStateWorkspace" sure looks like it satisfies ${string}:${string}.

@@ -336,6 +339,62 @@ describe("Presence", () => {
// Verify
assert.strictEqual(listener.called, false);
});

it("with workspace that has an invalid internal address does NOT emit 'workspaceActivated'", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would say "unrecognized" rather than "invalid".

Comment on lines +429 to +437
const internalWorkspaceTypes = {
s: "States",
n: "Notifications",
} as const;

// Convert prefix to internal workspace type
const internalWorkspaceType =
internalWorkspaceTypes[prefix as keyof typeof internalWorkspaceTypes] ??
"Unknown";
Copy link
Contributor

Choose a reason for hiding this comment

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

Warning: as keyword used.

This does not produce the correct type because prefix is coerced to keyof. TypeScript thinks that "Unknown" is unreachable.
Since you are going from an unknown string, internalWorkspaceTypes should be a read-only string record (Readonly<Record<string, ...>>). You can also define it outside of the class; so that it is only initialized once.

Comment on lines +90 to +95
function isPresenceWorkspaceAddress(
workspaceAddress: string,
): workspaceAddress is PresenceWorkspaceAddress {
return workspaceAddress.includes(":");
}

Copy link
Contributor

Choose a reason for hiding this comment

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

We can get rid of this by enhancing the regex

@@ -403,7 +411,36 @@ export class PresenceDatastoreManagerImpl implements PresenceDatastoreManager {
if (workspaceDatastore === undefined) {
workspaceDatastore = this.datastore[workspaceAddress] = {};
if (!workspaceAddress.startsWith("system:")) {
// TODO: Emit workspaceActivated event for PresenceEvents
// Separate internal type prefix from public workspace address
const match = workspaceAddress.match(/^([a-z]):(.*)/);
Copy link
Contributor

Choose a reason for hiding this comment

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

We should use a better match to simplify the code.
Then, because we have noUncheckedIndexAccess enabled and it is not good with array, we can do a little type fix-up based on the regex.

Suggested change
const match = workspaceAddress.match(/^([a-z]):(.*)/);
const match = workspaceAddress.match(/^([^:]):([^:]+:.+)$/) as null | [string,string,PresenceWorkspaceAddress];

(Optionally, define a regex outside of class right below InternalWorkspaceAddress.

const regexInternalWorkspaceAddress = /^([^:]):([^:]+:.+)$/;

)

@@ -403,7 +411,36 @@ export class PresenceDatastoreManagerImpl implements PresenceDatastoreManager {
if (workspaceDatastore === undefined) {
workspaceDatastore = this.datastore[workspaceAddress] = {};
if (!workspaceAddress.startsWith("system:")) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This if is no longer needed.

Copy link
Contributor

Choose a reason for hiding this comment

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

There are some test cases that need added:

  1. Notifications should only happen one time per workspace address. If another instance of that address comes thru, even if, there isn't a registered workspace, then we shouldn't see the event again.
  2. There is nothing for the key scenario (and the implementation doesn't support it), need to make sure a new registration can get updates. Best case is for Notifications, should be able to see one is activated, register within that event, and then see the notifications that have arrived. Might want to do all or at least these register and listen cases in the events test file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: framework Framework is a tag for issues involving the developer framework. Eg Aqueduct base: main PRs targeted against main branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants