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
12 changes: 11 additions & 1 deletion src/commands/spaces/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
formatProgress,
formatResource,
formatSuccess,
formatWarning,
} from "../../utils/output.js";

export default class SpacesCreate extends SpacesBaseCommand {
Expand Down Expand Up @@ -45,14 +46,23 @@ export default class SpacesCreate extends SpacesBaseCommand {
setupConnectionLogging: false,
});

const ephemeralSpaceWarning = `Space ${spaceName} is backed by Ably channel '${spaceName}::$space' and is ephemeral — it becomes active when at least one member enters. This command initializes the space without entering it. To add a member to the space, use 'ably spaces members enter ${spaceName}'.`;

if (this.shouldOutputJson(flags)) {
this.logJsonResult({ space: { name: spaceName } }, flags);
this.logJsonResult(
{
space: { name: spaceName },
warning: ephemeralSpaceWarning,
},
flags,
);
} else {
this.log(
formatSuccess(
`Space ${formatResource(spaceName)} initialized. Use "ably spaces members enter" to activate it.`,
),
);
this.log(formatWarning(ephemeralSpaceWarning));
}
} catch (error) {
this.fail(error, flags, "spaceCreate");
Expand Down
29 changes: 26 additions & 3 deletions src/commands/spaces/cursors/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,33 @@ export default class SpacesCursorsSet extends SpacesBaseCommand {
{ position: { x: simulatedX, y: simulatedY } },
);

if (!this.shouldOutputJson(flags)) {
this.log(
`${formatLabel("Simulated")} cursor at (${simulatedX}, ${simulatedY})`,
if (this.shouldOutputJson(flags)) {
this.logJsonEvent(
{
cursor: {
clientId: this.realtimeClient!.auth.clientId,
connectionId: this.realtimeClient!.connection.id,
position: { x: simulatedX, y: simulatedY },
data: (cursorData.data as CursorData) ?? null,
},
},
flags,
);
this.logJsonStatus(
"holding",
"Holding cursor. Press Ctrl+C to exit.",
flags,
);
} else {
const simLines = [
`${formatLabel("Simulated")} cursor at (${simulatedX}, ${simulatedY})`,
];
if (cursorData.data) {
simLines.push(
` ${formatLabel("Data")} ${JSON.stringify(cursorData.data)}`,
);
}
this.log(simLines.join("\n"));
}
} catch (error) {
this.logCliEvent(
Expand Down
2 changes: 1 addition & 1 deletion src/commands/spaces/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default class SpacesGet extends SpacesBaseCommand {

if (items.length === 0) {
this.fail(
`Space ${spaceName} doesn't have any members currently present. Spaces only exist while members are present. Please enter at least one member using "ably spaces members enter".`,
`Space ${spaceName} doesn't have any members currently present. Spaces only exist while members are present. Please enter at least one member using 'ably spaces members enter ${spaceName}'.`,
flags,
"spaceGet",
{ spaceName },
Expand Down
94 changes: 44 additions & 50 deletions src/commands/spaces/locations/subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,62 +56,56 @@ export default class SpacesLocationsSubscribe extends SpacesBaseCommand {
"Subscribing to location updates",
);

try {
const locationHandler = (update: LocationsEvents.UpdateEvent) => {
try {
const timestamp = new Date().toISOString();
this.logCliEvent(
flags,
"location",
"updateReceived",
"Location update received",
{
clientId: update.member.clientId,
connectionId: update.member.connectionId,
timestamp,
},
);
const locationHandler = (update: LocationsEvents.UpdateEvent) => {
try {
const timestamp = new Date().toISOString();
this.logCliEvent(
flags,
"location",
"updateReceived",
"Location update received",
{
clientId: update.member.clientId,
connectionId: update.member.connectionId,
timestamp,
},
);

if (this.shouldOutputJson(flags)) {
this.logJsonEvent(
{
location: {
member: {
clientId: update.member.clientId,
connectionId: update.member.connectionId,
},
currentLocation: update.currentLocation,
previousLocation: update.previousLocation,
timestamp,
if (this.shouldOutputJson(flags)) {
this.logJsonEvent(
{
location: {
member: {
clientId: update.member.clientId,
connectionId: update.member.connectionId,
},
currentLocation: update.currentLocation,
previousLocation: update.previousLocation,
timestamp,
},
flags,
);
} else {
this.log(formatTimestamp(timestamp));
this.log(formatLocationUpdateBlock(update));
this.log("");
}
} catch (error) {
this.fail(error, flags, "locationSubscribe", {
spaceName,
});
},
flags,
);
} else {
this.log(formatTimestamp(timestamp));
this.log(formatLocationUpdateBlock(update));
this.log("");
}
};
} catch (error) {
this.fail(error, flags, "locationSubscribe", {
spaceName,
});
}
};

this.space!.locations.subscribe("update", locationHandler);
this.space!.locations.subscribe("update", locationHandler);

this.logCliEvent(
flags,
"location",
"subscribed",
"Successfully subscribed to location updates",
);
} catch (error) {
this.fail(error, flags, "locationSubscribe", {
spaceName,
});
}
this.logCliEvent(
flags,
"location",
"subscribed",
"Successfully subscribed to location updates",
);

await this.waitAndTrackCleanup(flags, "location", flags.duration);
} catch (error) {
Expand Down
25 changes: 5 additions & 20 deletions src/commands/spaces/members/subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ export default class SpacesMembersSubscribe extends SpacesBaseCommand {
...durationFlag,
};

private listener: ((member: SpaceMember) => void) | null = null;

async run(): Promise<void> {
const { args, flags } = await this.parse(SpacesMembersSubscribe);
const { space_name: spaceName } = args;
Expand Down Expand Up @@ -69,21 +67,15 @@ export default class SpacesMembersSubscribe extends SpacesBaseCommand {
"subscribing",
"Subscribing to member updates",
);
// Define the listener function
this.listener = (member: SpaceMember) => {

const memberListener = (member: SpaceMember) => {
const now = Date.now();

// Determine the action from the member's lastEvent
const action = member.lastEvent?.name || "unknown";
const action = member.lastEvent.name || "unknown";
const clientId = member.clientId || "Unknown";
const connectionId = member.connectionId || "Unknown";

// Skip self events - check connection ID
const selfConnectionId = this.realtimeClient!.connection.id;
if (member.connectionId === selfConnectionId) {
return;
}

// Create a unique key for this client+connection combination
const clientKey = `${clientId}:${connectionId}`;

Expand Down Expand Up @@ -130,8 +122,8 @@ export default class SpacesMembersSubscribe extends SpacesBaseCommand {
}
};

// Subscribe using the stored listener
await this.space!.members.subscribe("update", this.listener);
// Subscribe using the listener
await this.space!.members.subscribe("update", memberListener);

this.logCliEvent(
flags,
Expand All @@ -140,13 +132,6 @@ export default class SpacesMembersSubscribe extends SpacesBaseCommand {
"Subscribed to member updates",
);

this.logCliEvent(
flags,
"member",
"listening",
"Listening for member updates...",
);

// Wait until the user interrupts or the optional duration elapses
await this.waitAndTrackCleanup(flags, "member", flags.duration);
} catch (error) {
Expand Down
Loading
Loading