Skip to content

Commit

Permalink
Merge pull request #1645 from ably/no-upgrade-v2
Browse files Browse the repository at this point in the history
feat: no upgrade
  • Loading branch information
owenpearson committed Mar 19, 2024
2 parents 48883e1 + b54bcf5 commit af0f3f1
Show file tree
Hide file tree
Showing 43 changed files with 627 additions and 1,545 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const client = new BaseRealtime({
You must provide:

- at least one HTTP request implementation; that is, one of `FetchRequest` or `XHRRequest`;
- at least one realtime transport implementation; that is, one of `WebSocketTransport`, `XHRStreaming`, or `XHRPolling`.
- at least one realtime transport implementation; that is, one of `WebSocketTransport` or `XHRPolling`.

`BaseRealtime` offers the same API as the `Realtime` class described in the rest of this `README`. This means that you can develop an application using the default variant of the SDK and switch to the modular version when you wish to optimize your bundle size.

Expand Down
2 changes: 1 addition & 1 deletion ably.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ export type HTTPMethod = HTTPMethods.GET | HTTPMethods.POST;
/**
* A type which specifies the valid transport names. [See here](https://faqs.ably.com/which-transports-are-supported) for more information.
*/
export type Transport = 'web_socket' | 'xhr_streaming' | 'xhr_polling' | 'comet';
export type Transport = 'web_socket' | 'xhr_polling' | 'comet';

/**
* Contains the details of a {@link Channel} or {@link RealtimeChannel} object such as its ID and {@link ChannelStatus}.
Expand Down
29 changes: 5 additions & 24 deletions modular.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,19 @@ export declare const RealtimePresence: unknown;
* const realtime = new BaseRealtime({ ...options, plugins: { WebSocketTransport, FetchRequest } });
* ```
*
* Note that network conditions, such as firewalls or proxies, might prevent the client from establishing a WebSocket connection. For this reason, you may wish to provide the `BaseRealtime` instance with the ability to alternatively establish a connection using a transport that is less susceptible to these external conditions. You do this by passing one or more alternative transport plugins, namely {@link XHRStreaming} and/or {@link XHRPolling}, alongside `WebSocketTransport`:
* Note that network conditions, such as firewalls or proxies, might prevent the client from establishing a WebSocket connection. For this reason, you may wish to provide the `BaseRealtime` instance with the ability to alternatively establish a connection using long polling which is less susceptible to these external conditions. You do this by passing in the {@link XHRPolling} module, alongside `WebSocketTransport`:
*
* ```javascript
* import { BaseRealtime, WebSocketTransport, FetchRequest } from 'ably/modular';
* const realtime = new BaseRealtime({ ...options, plugins: { WebSocketTransport, XHRStreaming, FetchRequest } });
* import { BaseRealtime, WebSocketTransport, FetchRequest } from 'ably/modules';
* const realtime = new BaseRealtime(options, { WebSocketTransport, XHRPolling, FetchRequest });
* ```
*/
export declare const WebSocketTransport: unknown;

/**
* Provides a {@link BaseRealtime} instance with the ability to establish a connection with the Ably realtime service using the browser’s [XMLHttpRequest API](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest).
*
* `XHRPolling` uses HTTP long polling; that is, it will make a new HTTP request each time a message is received from Ably. This is less efficient than {@link XHRStreaming}, but is also more likely to succeed in the presence of certain network conditions such as firewalls or proxies.
* `XHRPolling` uses HTTP long polling; that is, it will make a new HTTP request each time a message is received from Ably.
*
* ```javascript
* import { BaseRealtime, WebSocketTransport, FetchRequest } from 'ably/modular';
Expand All @@ -164,20 +164,6 @@ export declare const WebSocketTransport: unknown;
*/
export declare const XHRPolling: unknown;

/**
* Provides a {@link BaseRealtime} instance with the ability to establish a connection with the Ably realtime service using the browser’s [XMLHttpRequest API](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest).
*
* `XHRStreaming` uses HTTP streaming; that is, in contrast to {@link XHRPolling}, it does not need to make a new HTTP request each time a message is received from Ably. This is more efficient than `XHRPolling`, but is more likely to be blocked by certain network conditions such as firewalls or proxies.
*
* ```javascript
* import { BaseRealtime, WebSocketTransport, FetchRequest } from 'ably/modular';
* const realtime = new BaseRealtime({ ...options, plugins: { XHRStreaming, FetchRequest } });
* ```
*
* Provide this plugin if, for example, you wish the client to have an alternative mechanism for connecting to Ably if it’s unable to establish a WebSocket connection.
*/
export declare const XHRStreaming: unknown;

/**
* Provides a {@link BaseRest} or {@link BaseRealtime} instance with the ability to make HTTP requests using the browser’s [XMLHttpRequest API](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest).
*
Expand Down Expand Up @@ -250,11 +236,6 @@ export interface ModularPlugins {
*/
XHRPolling?: typeof XHRPolling;

/**
* See {@link XHRStreaming | documentation for the `XHRStreaming` plugin}.
*/
XHRStreaming?: typeof XHRStreaming;

/**
* See {@link XHRRequest | documentation for the `XHRRequest` plugin}.
*/
Expand Down Expand Up @@ -342,7 +323,7 @@ export declare class BaseRealtime implements RealtimeClient {
* You must provide:
*
* - at least one HTTP request implementation; that is, one of {@link FetchRequest} or {@link XHRRequest} — for minimum bundle size, favour `FetchRequest`;
* - at least one realtime transport implementation; that is, one of {@link WebSocketTransport}, {@link XHRStreaming}, or {@link XHRPolling} — for minimum bundle size, favour `WebSocketTransport`.
* - at least one realtime transport implementation; that is, one of {@link WebSocketTransport} or {@link XHRPolling} — for minimum bundle size, favour `WebSocketTransport`.
*/
constructor(options: ClientOptions<CorePlugins & ModularPlugins>);

Expand Down
2 changes: 0 additions & 2 deletions scripts/moduleReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const pluginNames = [
'MsgPack',
'RealtimePresence',
'XHRPolling',
'XHRStreaming',
'WebSocketTransport',
'XHRRequest',
'FetchRequest',
Expand Down Expand Up @@ -233,7 +232,6 @@ async function checkBaseRealtimeFiles() {
// The threshold is chosen pretty arbitrarily. There are some files (e.g. presencemessage.ts) whose bulk should not be included in the BaseRealtime bundle, but which make a small contribution to the bundle (probably because we make use of one exported constant or something; I haven’t looked into it).
const allowedFiles = new Set([
'src/common/constants/HttpStatusCodes.ts',
'src/common/constants/TransportName.ts',
'src/common/constants/XHRStates.ts',
'src/common/lib/client/auth.ts',
'src/common/lib/client/baseclient.ts',
Expand Down
7 changes: 1 addition & 6 deletions src/common/constants/TransportName.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
export namespace TransportNames {
export const WebSocket = 'web_socket' as const;
export const Comet = 'comet' as const;
export const XhrStreaming = 'xhr_streaming' as const;
export const XhrPolling = 'xhr_polling' as const;
}

type TransportName =
| typeof TransportNames.WebSocket
| typeof TransportNames.Comet
| typeof TransportNames.XhrStreaming
| typeof TransportNames.XhrPolling;
type TransportName = typeof TransportNames.WebSocket | typeof TransportNames.Comet | typeof TransportNames.XhrPolling;

export default TransportName;
3 changes: 0 additions & 3 deletions src/common/lib/client/baserealtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ class BaseRealtime extends BaseClient {
if (plugins?.WebSocketTransport) {
transports[TransportNames.WebSocket] = plugins.WebSocketTransport;
}
if (plugins?.XHRStreaming) {
transports[TransportNames.XhrStreaming] = plugins.XHRStreaming;
}
if (plugins?.XHRPolling) {
transports[TransportNames.XhrPolling] = plugins.XHRPolling;
}
Expand Down
4 changes: 2 additions & 2 deletions src/common/lib/client/defaultrealtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { DefaultMessage } from '../types/defaultmessage';
import { MsgPack } from 'common/types/msgpack';
import RealtimePresence from './realtimepresence';
import { DefaultPresenceMessage } from '../types/defaultpresencemessage';
import initialiseWebSocketTransport from '../transport/websockettransport';
import WebSocketTransport from '../transport/websockettransport';
import { FilteredSubscriptions } from './filteredsubscriptions';
import {
fromValues as presenceMessageFromValues,
Expand Down Expand Up @@ -39,7 +39,7 @@ export class DefaultRealtime extends BaseRealtime {
presenceMessageFromValues,
presenceMessagesFromValuesArray,
},
WebSocketTransport: initialiseWebSocketTransport,
WebSocketTransport,
MessageInteractions: FilteredSubscriptions,
}),
);
Expand Down
7 changes: 3 additions & 4 deletions src/common/lib/client/modularplugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { Rest } from './rest';
import { IUntypedCryptoStatic } from '../../types/ICryptoStatic';
import { MsgPack } from 'common/types/msgpack';
import RealtimePresence from './realtimepresence';
import { TransportInitialiser } from '../transport/connectionmanager';
import XHRRequest from 'platform/web/lib/http/request/xhrrequest';
import fetchRequest from 'platform/web/lib/http/request/fetchrequest';
import { FilteredSubscriptions } from './filteredsubscriptions';
import {
fromValues as presenceMessageFromValues,
fromValuesArray as presenceMessagesFromValuesArray,
} from '../types/presencemessage';
import { TransportCtor } from '../transport/transport';

export interface PresenceMessagePlugin {
presenceMessageFromValues: typeof presenceMessageFromValues;
Expand All @@ -25,9 +25,8 @@ export interface ModularPlugins {
Crypto?: IUntypedCryptoStatic;
MsgPack?: MsgPack;
RealtimePresence?: RealtimePresencePlugin;
WebSocketTransport?: TransportInitialiser;
XHRPolling?: TransportInitialiser;
XHRStreaming?: TransportInitialiser;
WebSocketTransport?: TransportCtor;
XHRPolling?: TransportCtor;
XHRRequest?: typeof XHRRequest;
FetchRequest?: typeof fetchRequest;
MessageInteractions?: typeof FilteredSubscriptions;
Expand Down
4 changes: 1 addition & 3 deletions src/common/lib/client/realtimechannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -753,9 +753,7 @@ class RealtimeChannel extends EventEmitter {
checkPendingState(): void {
/* if can't send events, do nothing */
const cmState = this.connectionManager.state;
/* Allow attach messages to queue up when synchronizing, since this will be
* the state we'll be in when upgrade transport.active triggers a checkpendingstate */
if (!(cmState.sendEvents || cmState.forceQueueEvents)) {
if (!cmState.sendEvents) {
Logger.logAction(
Logger.LOG_MINOR,
'RealtimeChannel.checkPendingState',
Expand Down
Loading

0 comments on commit af0f3f1

Please sign in to comment.