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

[MMB-247] Channel tagging #166

Merged
merged 2 commits into from
Sep 8, 2023
Merged
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
4 changes: 2 additions & 2 deletions docs/channel-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ The below channels are used by the Spaces library internally.

Each `Space` (as defined by the [`Space` class](/docs/class-definitions.md#space)) creates its own [Ably Channel](https://ably.com/docs/channels).

The channel name is defined by the `name` of the Space and takes the form: `_ably_space_${name}`. The full name of a `channel` belonging to a `Space` called 'my_space' would therefore be `_ably_space_my_space`.
The channel name is defined by the `name` of the Space and takes the form: `${name}-space`. The full name of a `channel` belonging to a `Space` called 'slides' would therefore be `slides-space`.

### Cursors

If any member of a `Space` subscribes to or sets cursor updates a channel is created for `cursors` updates.

The channel name is defined by the channel name of the Space with the `_cursors` suffix, taking the form: `${space.getChannelName()}_cursors`. The full name of a channel belonging to a `Space` called 'my_space' would therefore be `_ably_space_my_space_cursors`.
The channel name is defined by the name of the Space with the `::$cursors` suffix, taking the form: `${space.name}::$cursors`. The full name of a channel belonging to a `Space` called 'slides' would therefore be `slides::$cursors`.

#### Events published

Expand Down
7 changes: 5 additions & 2 deletions src/Cursors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@ type CursorsEventMap = {
update: CursorUpdate;
};

const CURSORS_CHANNEL_TAG = '::$cursors';

export default class Cursors extends EventEmitter<CursorsEventMap> {
private readonly cursorBatching: CursorBatching;
private readonly cursorDispensing: CursorDispensing;
private readonly cursorHistory: CursorHistory;
readonly options: CursorsOptions;
readonly channelName: string;

public channel?: Types.RealtimeChannelPromise;

constructor(private space: Space) {
super();

this.options = this.space.options.cursors;
this.channelName = `${this.space.name}${CURSORS_CHANNEL_TAG}`;

this.cursorHistory = new CursorHistory();
this.cursorBatching = new CursorBatching(this.options.outboundBatchInterval);
Expand Down Expand Up @@ -62,8 +66,7 @@ export default class Cursors extends EventEmitter<CursorsEventMap> {
}

private initializeCursorsChannel(): Types.RealtimeChannelPromise {
const spaceChannelName = this.space.channelName;
const channel = this.space.client.channels.get(`${spaceChannelName}_cursors`);
const channel = this.space.client.channels.get(this.channelName);
channel.presence.subscribe(this.onPresenceUpdate.bind(this));
channel.presence.enter();
return channel;
Expand Down
2 changes: 1 addition & 1 deletion src/Space.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('Space', () => {
const channelSpy = vi.spyOn(channels, 'get');
const space = new Space('test', client);

expect(channelSpy).toHaveBeenNthCalledWith(1, '_ably_space_test');
expect(channelSpy).toHaveBeenNthCalledWith(1, 'test-space');
expectTypeOf(space).toMatchTypeOf<Space>();
});
});
Expand Down
7 changes: 5 additions & 2 deletions src/Space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import { isFunction, isObject } from './utilities/is.js';
import type { SpaceOptions, SpaceMember, ProfileData } from './types.js';
import type { Subset, PresenceMember } from './utilities/types.js';

const SPACE_CHANNEL_PREFIX = '_ably_space_';
// Replace by ::$space when that channel tag will be available
const SPACE_CHANNEL_TAG = '-space';

const SPACE_OPTIONS_DEFAULTS = {
offlineTimeout: 120_000,
Expand All @@ -42,13 +43,15 @@ class Space extends EventEmitter<SpaceEventsMap> {
readonly members: Members;
readonly channel: Types.RealtimeChannelPromise;
readonly locks: Locks;
readonly name: string;

constructor(name: string, readonly client: Types.RealtimePromise, options?: Subset<SpaceOptions>) {
super();

this.options = this.setOptions(options);
this.connectionId = this.client.connection.id;
this.channelName = `${SPACE_CHANNEL_PREFIX}${name}`;
this.name = name;
this.channelName = `${name}${SPACE_CHANNEL_TAG}`;

this.channel = this.client.channels.get(this.channelName);
this.onPresenceUpdate = this.onPresenceUpdate.bind(this);
Expand Down
2 changes: 1 addition & 1 deletion src/Spaces.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('Spaces', () => {
await spaces.get('test');

expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith('_ably_space_test');
expect(spy).toHaveBeenCalledWith('test-space');
});

it<SpacesTestContext>('applies the agent header to an existing SDK instance', ({ client }) => {
Expand Down
Loading