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

Renaming types for clarity #478

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions demo/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FC, useEffect, useState } from 'react';
import { Chat } from './containers/Chat';
import { OccupancyComponent } from './components/OccupancyComponent';
import { UserPresenceComponent } from './components/UserPresenceComponent';
import { DefaultRoomOptions, ChatRoomProvider } from '@ably/chat';
import { AllFeaturesEnabled, ChatRoomProvider } from '@ably/chat';

// We read the roomID from the URL query string and default to 'abcd' if none
// provided. We make sure the URL is updated to always include the roomId. This
Expand Down Expand Up @@ -52,7 +52,7 @@ const App: FC<AppProps> = () => {
id={roomIdState}
release={true}
attach={true}
options={DefaultRoomOptions}
options={AllFeaturesEnabled}
>
<div
style={{ display: 'flex', justifyContent: 'space-between', width: '800px', margin: 'auto', height: '650px' }}
Expand Down
8 changes: 4 additions & 4 deletions src/core/chat.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Ably from 'ably';

import { ClientOptions, normalizeClientOptions, NormalizedClientOptions } from './config.js';
import { ChatClientOptions, normalizeClientOptions, NormalizedChatClientOptions } from './config.js';
import { Connection, DefaultConnection } from './connection.js';
import { Logger, makeLogger } from './logger.js';
import { RealtimeWithOptions } from './realtime-extensions.js';
Expand All @@ -24,7 +24,7 @@ export class ChatClient {
/**
* @internal
*/
private readonly _clientOptions: NormalizedClientOptions;
private readonly _clientOptions: NormalizedChatClientOptions;

/**
* @internal
Expand All @@ -41,7 +41,7 @@ export class ChatClient {
* @param realtime - The Ably Realtime client.
* @param clientOptions - The client options.
*/
constructor(realtime: Ably.Realtime, clientOptions?: ClientOptions) {
constructor(realtime: Ably.Realtime, clientOptions?: ChatClientOptions) {
this._realtime = realtime;
this._clientOptions = normalizeClientOptions(clientOptions);
this._logger = makeLogger(this._clientOptions);
Expand Down Expand Up @@ -91,7 +91,7 @@ export class ChatClient {
* Returns the resolved client options for the client, including any defaults that have been set.
* @returns The client options.
*/
get clientOptions(): ClientOptions {
get clientOptions(): ChatClientOptions {
return this._clientOptions;
}

Expand Down
8 changes: 4 additions & 4 deletions src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { LogHandler, LogLevel } from './logger.js';
/**
* Configuration options for the chat client.
*/
export interface ClientOptions {
export interface ChatClientOptions {
/**
* A custom log handler that will be used to log messages from the client.
* @defaultValue The client will log messages to the console.
Expand Down Expand Up @@ -33,14 +33,14 @@ type Modify<T, R> = Omit<T, keyof R> & R;
/**
* These are the normalized client options, with default values filled in for any missing properties.
*/
export type NormalizedClientOptions = Modify<
ClientOptions,
export type NormalizedChatClientOptions = Modify<
ChatClientOptions,
{
logLevel: LogLevel;
}
>;

export const normalizeClientOptions = (options?: ClientOptions): NormalizedClientOptions => {
export const normalizeClientOptions = (options?: ChatClientOptions): NormalizedChatClientOptions => {
options = options ?? {};

return {
Expand Down
4 changes: 2 additions & 2 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

export { ChatClient } from './chat.js';
export type { ClientOptions } from './config.js';
export type { ChatClientOptions } from './config.js';
export type {
Connection,
ConnectionStatusChange,
Expand Down Expand Up @@ -51,7 +51,7 @@ export type {
RoomReactionsOptions,
TypingOptions,
} from './room-options.js';
export { DefaultRoomOptions } from './room-options.js';
export { AllFeaturesEnabled } from './room-options.js';
export type {
RoomReactionListener,
RoomReactions,
Expand Down
4 changes: 2 additions & 2 deletions src/core/logger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Ably from 'ably';

import { NormalizedClientOptions } from './config.js';
import { NormalizedChatClientOptions } from './config.js';

/**
* Interface for loggers.
Expand Down Expand Up @@ -131,7 +131,7 @@ const consoleLogger = (message: string, level: LogLevel, context?: LogContext) =
}
};

export const makeLogger = (options: NormalizedClientOptions): Logger => {
export const makeLogger = (options: NormalizedChatClientOptions): Logger => {
const logHandler = options.logHandler ?? consoleLogger;

return new DefaultLogger(logHandler, options.logLevel);
Expand Down
13 changes: 7 additions & 6 deletions src/core/room-options.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as Ably from 'ably';

/**
* Represents the default options for a chat room.
* A set of example options for a Chat room that enables all features, this is
* useful for testing and demonstration purposes.
*/
export const DefaultRoomOptions = {
export const AllFeaturesEnabled = {
/**
* The default presence options for a chat room.
*/
Expand Down Expand Up @@ -89,26 +90,26 @@ export type OccupancyOptions = object;
export interface RoomOptions {
/**
* The presence options for the room. To enable presence in the room, set this property. You may
* use {@link DefaultRoomOptions.presence} to enable presence with default options.
* use {@link AllFeaturesEnabled.presence} to enable presence with default options.
* @defaultValue undefined
*/
presence?: PresenceOptions;

/**
* The typing options for the room. To enable typing in the room, set this property. You may use
* {@link DefaultRoomOptions.typing} to enable typing with default options.
* {@link AllFeaturesEnabled.typing} to enable typing with default options.
*/
typing?: TypingOptions;

/**
* The reactions options for the room. To enable reactions in the room, set this property. You may use
* {@link DefaultRoomOptions.reactions} to enable reactions with default options.
* {@link AllFeaturesEnabled.reactions} to enable reactions with default options.
*/
reactions?: RoomReactionsOptions;

/**
* The occupancy options for the room. To enable occupancy in the room, set this property. You may use
* {@link DefaultRoomOptions.occupancy} to enable occupancy with default options.
* {@link AllFeaturesEnabled.occupancy} to enable occupancy with default options.
*/
occupancy?: OccupancyOptions;
}
Expand Down
12 changes: 6 additions & 6 deletions src/core/rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as Ably from 'ably';
import { dequal } from 'dequal';

import { ChatApi } from './chat-api.js';
import { ClientOptions, NormalizedClientOptions } from './config.js';
import { ChatClientOptions, NormalizedChatClientOptions } from './config.js';
import { ErrorCodes } from './errors.js';
import { randomId } from './id.js';
import { Logger } from './logger.js';
Expand Down Expand Up @@ -49,9 +49,9 @@ export interface Rooms {

/**
* Get the client options used to create the Chat instance.
* @returns ClientOptions
* @returns ChatClientOptions
*/
get clientOptions(): ClientOptions;
get clientOptions(): ChatClientOptions;
}

/**
Expand Down Expand Up @@ -85,7 +85,7 @@ interface RoomMapEntry {
export class DefaultRooms implements Rooms {
private readonly _realtime: Ably.Realtime;
private readonly _chatApi: ChatApi;
private readonly _clientOptions: NormalizedClientOptions;
private readonly _clientOptions: NormalizedChatClientOptions;
private readonly _rooms: Map<string, RoomMapEntry> = new Map<string, RoomMapEntry>();
private readonly _releasing = new Map<string, Promise<void>>();
private readonly _logger: Logger;
Expand All @@ -98,7 +98,7 @@ export class DefaultRooms implements Rooms {
* @param clientOptions The client options from the chat instance.
* @param logger An instance of the Logger.
*/
constructor(realtime: Ably.Realtime, clientOptions: NormalizedClientOptions, logger: Logger) {
constructor(realtime: Ably.Realtime, clientOptions: NormalizedChatClientOptions, logger: Logger) {
this._realtime = realtime;
this._chatApi = new ChatApi(realtime, logger);
this._clientOptions = clientOptions;
Expand Down Expand Up @@ -187,7 +187,7 @@ export class DefaultRooms implements Rooms {
/**
* @inheritDoc
*/
get clientOptions(): ClientOptions {
get clientOptions(): ChatClientOptions {
return this._clientOptions;
}

Expand Down
10 changes: 5 additions & 5 deletions src/react/providers/chat-room-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ChatClient } from '../../core/chat.js';
import { Logger } from '../../core/logger.js';
import { Room } from '../../core/room.js';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { type DefaultRoomOptions, RoomOptions } from '../../core/room-options.js';
import { type AllFeaturesEnabled, RoomOptions } from '../../core/room-options.js';
import { ChatRoomContext, ChatRoomContextType } from '../contexts/chat-room-context.js';
import { useChatClient } from '../hooks/use-chat-client.js';
import { useLogger } from '../hooks/use-logger.js';
Expand All @@ -19,15 +19,15 @@ export interface ChatRoomProviderProps {

/**
* The options to use when creating the room. A convenient default value is
* provided by {@link DefaultRoomOptions}, but it must explicitly be set
* provided by {@link AllFeaturesEnabled}, but it must explicitly be set
* here.
*
* {@link DefaultRoomOptions} can also be used partially, for example:
* {@link AllFeaturesEnabled} can also be used partially, for example:
*
* ```tsx
* <ChatRoomProvider id="room-id" options={{
* presence: DefaultRoomOptions.presence,
* reactions: DefaultRoomOptions.reactions,
* presence: AllFeaturesEnabled.presence,
* reactions: AllFeaturesEnabled.reactions,
* }} />
* ```
*
Expand Down
4 changes: 2 additions & 2 deletions test/core/chat.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ConnectionStatus } from '../../src/core/connection.ts';
import { LogLevel } from '../../src/core/logger.ts';
import { RealtimeWithOptions } from '../../src/core/realtime-extensions.ts';
import { CHANNEL_OPTIONS_AGENT_STRING_REACT, VERSION } from '../../src/core/version.ts';
import { DefaultRoomOptions } from '../../src/index.ts';
import { AllFeaturesEnabled } from '../../src/index.ts';
import { newChatClient } from '../helper/chat.ts';
import { testClientOptions } from '../helper/options.ts';
import { ablyRealtimeClient } from '../helper/realtime-client.ts';
Expand Down Expand Up @@ -48,7 +48,7 @@ describe('Chat', () => {
const chat = newChatClient(testClientOptions());
chat.addReactAgent();

const room = await chat.rooms.get('room', DefaultRoomOptions);
const room = await chat.rooms.get('room', AllFeaturesEnabled);

const channelOptions = (room.messages.channel as unknown as { channelOptions: Ably.ChannelOptions }).channelOptions;
expect(channelOptions.params).toEqual(
Expand Down
4 changes: 2 additions & 2 deletions test/core/messages.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ChatMessageActions, MessageEvents } from '../../src/core/events.ts';
import { Message } from '../../src/core/message.ts';
import { OrderBy } from '../../src/core/messages.ts';
import { RealtimeChannelWithOptions } from '../../src/core/realtime-extensions.ts';
import { DefaultRoomOptions } from '../../src/core/room-options.ts';
import { AllFeaturesEnabled } from '../../src/core/room-options.ts';
import { RoomStatus } from '../../src/core/room-status.ts';
import { CHANNEL_OPTIONS_AGENT_STRING } from '../../src/core/version.ts';
import { newChatClient } from '../helper/chat.ts';
Expand Down Expand Up @@ -781,7 +781,7 @@ describe('messages integration', { timeout: 10000 }, () => {
it<TestContext>('handles the room being released before getPreviousMessages is called', async (context) => {
const chat = context.chat;
const roomId = randomRoomId();
const room = await chat.rooms.get(roomId, DefaultRoomOptions);
const room = await chat.rooms.get(roomId, AllFeaturesEnabled);

// Create a subscription to messages
room.messages.subscribe(() => {});
Expand Down
4 changes: 2 additions & 2 deletions test/core/presence.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ChatClient } from '../../src/core/chat.ts';
import { PresenceEvents } from '../../src/core/events.ts';
import { PresenceData, PresenceEvent } from '../../src/core/presence.ts';
import { Room } from '../../src/core/room.ts';
import { DefaultRoomOptions } from '../../src/core/room-options.ts';
import { AllFeaturesEnabled } from '../../src/core/room-options.ts';
import { RoomStatus } from '../../src/core/room-status.ts';
import { newChatClient } from '../helper/chat.ts';
import { randomRoomId } from '../helper/identifier.ts';
Expand Down Expand Up @@ -102,7 +102,7 @@ describe('UserPresence', { timeout: 30000 }, () => {
const roomId = randomRoomId();
context.chat = newChatClient(undefined, context.realtime);
context.defaultTestClientId = context.realtime.auth.clientId;
context.chatRoom = await context.chat.rooms.get(roomId, { presence: DefaultRoomOptions.presence });
context.chatRoom = await context.chat.rooms.get(roomId, { presence: AllFeaturesEnabled.presence });
});

// Test for successful entering with clientId and custom user data
Expand Down
28 changes: 14 additions & 14 deletions test/core/room.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ChatApi } from '../../src/core/chat-api.ts';
import { randomId } from '../../src/core/id.ts';
import { DefaultRoom, Room } from '../../src/core/room.ts';
import { RoomLifecycleManager } from '../../src/core/room-lifecycle-manager.ts';
import { DefaultRoomOptions, normalizeRoomOptions, RoomOptions } from '../../src/core/room-options.ts';
import { AllFeaturesEnabled, normalizeRoomOptions, RoomOptions } from '../../src/core/room-options.ts';
import { RoomStatus } from '../../src/core/room-status.ts';
import { DefaultTyping } from '../../src/core/typing.ts';
import {
Expand All @@ -17,7 +17,7 @@ import {
import { randomRoomId } from '../helper/identifier.ts';
import { makeTestLogger } from '../helper/logger.ts';
import { ablyRealtimeClient } from '../helper/realtime-client.ts';
import { defaultRoomOptions, waitForRoomStatus } from '../helper/room.ts';
import { waitForRoomStatus } from '../helper/room.ts';

vi.mock('ably');

Expand Down Expand Up @@ -56,10 +56,10 @@ describe('Room', () => {

describe.each([
['messages', {}, (room: Room) => room.messages],
['presence', { presence: DefaultRoomOptions.presence }, (room: Room) => room.presence],
['occupancy', { occupancy: DefaultRoomOptions.occupancy }, (room: Room) => room.occupancy],
['typing', { typing: DefaultRoomOptions.typing }, (room: Room) => room.typing],
['reactions', { reactions: DefaultRoomOptions.reactions }, (room: Room) => room.reactions],
['presence', { presence: AllFeaturesEnabled.presence }, (room: Room) => room.presence],
['occupancy', { occupancy: AllFeaturesEnabled.occupancy }, (room: Room) => room.occupancy],
['typing', { typing: AllFeaturesEnabled.typing }, (room: Room) => room.typing],
['reactions', { reactions: AllFeaturesEnabled.reactions }, (room: Room) => room.reactions],
])('feature configured', (description: string, options: RoomOptions, featureLoader: (room: Room) => unknown) => {
it<TestContext>(`should not throw an error when trying to access ${description} whilst enabled`, (context) => {
const room = context.getRoom(options);
Expand Down Expand Up @@ -97,7 +97,7 @@ describe('Room', () => {
(description: string, setReact: boolean, agentString: string, defaultOptions: unknown) => {
it<TestContext>('applies the correct options', (context) => {
vi.spyOn(context.realtime.channels, 'get');
const room = context.getRoom(defaultRoomOptions, setReact) as DefaultRoom;
const room = context.getRoom(AllFeaturesEnabled, setReact) as DefaultRoom;

// Check that the shared channel for messages, occupancy and presence was called with the correct options
const expectedMessagesChannelOptions = {
Expand Down Expand Up @@ -132,7 +132,7 @@ describe('Room', () => {

describe('room status', () => {
it<TestContext>('should have a room status and error', async (context) => {
const room = context.getRoom(defaultRoomOptions);
const room = context.getRoom(AllFeaturesEnabled);
expect(room.status).toBe(RoomStatus.Initialized);

// Wait for the room to be initialized
Expand All @@ -153,7 +153,7 @@ describe('Room', () => {
});

it<TestContext>('should allow subscriptions to status changes', async (context) => {
const room = context.getRoom(defaultRoomOptions);
const room = context.getRoom(AllFeaturesEnabled);

const statuses: RoomStatus[] = [];
const errors: Ably.ErrorInfo[] = [];
Expand Down Expand Up @@ -188,7 +188,7 @@ describe('Room', () => {
});

it<TestContext>('should allow all subscriptions to be removed', async (context) => {
const room = context.getRoom(defaultRoomOptions);
const room = context.getRoom(AllFeaturesEnabled);

const statuses: RoomStatus[] = [];
const errors: Ably.ErrorInfo[] = [];
Expand Down Expand Up @@ -235,7 +235,7 @@ describe('Room', () => {

describe('room release', () => {
it<TestContext>('should release the room', async (context) => {
const room = context.getRoom(defaultRoomOptions) as DefaultRoom;
const room = context.getRoom(AllFeaturesEnabled) as DefaultRoom;
const lifecycleManager = (room as unknown as { _lifecycleManager: RoomLifecycleManager })._lifecycleManager;

// Setup spies on the realtime client and the room lifecycle manager
Expand Down Expand Up @@ -268,7 +268,7 @@ describe('Room', () => {
});

it<TestContext>('should only release with enabled features', async (context) => {
const room = context.getRoom({ typing: DefaultRoomOptions.typing }) as DefaultRoom;
const room = context.getRoom({ typing: AllFeaturesEnabled.typing }) as DefaultRoom;
const lifecycleManager = (room as unknown as { _lifecycleManager: RoomLifecycleManager })._lifecycleManager;

// Setup spies on the realtime client and the room lifecycle manager
Expand All @@ -292,7 +292,7 @@ describe('Room', () => {
});

it<TestContext>('releasing multiple times is idempotent', async (context) => {
const room = context.getRoom(defaultRoomOptions) as DefaultRoom;
const room = context.getRoom(AllFeaturesEnabled) as DefaultRoom;
const lifecycleManager = (room as unknown as { _lifecycleManager: RoomLifecycleManager })._lifecycleManager;

// Setup spies on the realtime client and the room lifecycle manager
Expand All @@ -315,7 +315,7 @@ describe('Room', () => {
});

it<TestContext>('can be released immediately without unhandled rejections', async (context) => {
const room = context.getRoom(defaultRoomOptions);
const room = context.getRoom(AllFeaturesEnabled);

// Release the room
// Note that an unhandled rejection will not cause the test to fail, but it will cause the process to exit
Expand Down
Loading
Loading