Skip to content

Commit

Permalink
Removing setting of action in Publish.
Browse files Browse the repository at this point in the history
- For now this isn't needed, messages will automatically bet set with `message_create`.

- Removed serial and action as required from InboundMessage type.
These are not strictly required, a message without them is still valid.
  • Loading branch information
splindsay-92 committed Oct 24, 2024
1 parent 58cdb05 commit 0506a3c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 39 deletions.
2 changes: 1 addition & 1 deletion ably.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2432,7 +2432,7 @@ export type MessageAction =
/**
* A message received from Ably.
*/
export type InboundMessage = Message & Required<Pick<Message, 'id' | 'timestamp' | 'serial' | 'action'>>;
export type InboundMessage = Message & Required<Pick<Message, 'id' | 'timestamp'>>;

/**
* Static utilities related to messages.
Expand Down
11 changes: 5 additions & 6 deletions src/common/lib/client/realtimechannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import * as Utils from '../util/utils';
import Logger from '../util/logger';
import RealtimePresence from './realtimepresence';
import Message, {
fromValues as messageFromValues,
fromValuesArray as messagesFromValuesArray,
encodeArray as encodeMessagesArray,
decode as decodeMessage,
getMessagesSize,
CipherOptions,
EncodingDecodingContext,
messageFromValuesArrayWithAction,
messageFromValuesWithAction,
} from '../types/message';
import ChannelStateChange from './channelstatechange';
import ErrorInfo, { PartialErrorInfo } from '../types/errorinfo';
Expand Down Expand Up @@ -234,17 +234,16 @@ class RealtimeChannel extends EventEmitter {
throw this.connectionManager.getError();
}
if (argCount == 1) {
// setting the action to `message_create` because this is a standard publish
if (Utils.isObject(messages)) messages = [messageFromValuesWithAction(messages, 'message_create')];
else if (Array.isArray(messages)) messages = messageFromValuesArrayWithAction(messages, 'message_create');
if (Utils.isObject(messages)) messages = [messageFromValues(messages)];
else if (Array.isArray(messages)) messages = messagesFromValuesArray(messages);
else
throw new ErrorInfo(
'The single-argument form of publish() expects a message object or an array of message objects',
40013,
400,
);
} else {
messages = [messageFromValuesWithAction({ name: args[0], data: args[1] }, 'message_create')];
messages = [messageFromValues({ name: args[0], data: args[1] })];
}
const maxMessageSize = this.client.options.maxMessageSize;
await encodeMessagesArray(messages, this.channelOptions as CipherOptions);
Expand Down
10 changes: 5 additions & 5 deletions src/common/lib/client/restchannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import Message, {
serialize as serializeMessage,
getMessagesSize,
CipherOptions,
messageFromValuesWithAction,
messageFromValuesArrayWithAction,
fromValues as messageFromValues,
fromValuesArray as messagesFromValuesArray,
} from '../types/message';
import ErrorInfo from '../types/errorinfo';
import { PaginatedResult } from './paginatedresource';
Expand Down Expand Up @@ -74,13 +74,13 @@ class RestChannel {

if (typeof first === 'string' || first === null) {
/* (name, data, ...) */
messages = [messageFromValuesWithAction({ name: first, data: second }, 'message_create')];
messages = [messageFromValues({ name: first, data: second })];
params = args[2];
} else if (Utils.isObject(first)) {
messages = [messageFromValuesWithAction(first, 'message_create')];
messages = [messageFromValues(first)];
params = args[1];
} else if (Array.isArray(first)) {
messages = messageFromValuesArrayWithAction(first, 'message_create');
messages = messagesFromValuesArray(first);
params = args[1];
} else {
throw new ErrorInfo(
Expand Down
27 changes: 0 additions & 27 deletions src/common/lib/types/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,33 +294,6 @@ export async function fromResponseBody(
return body;
}

/**
* This is used to return a new message with a given action type set.
* @param values - This is a message-like object, with the values to be set on the new message object.
* @param action - This is the action type that will be applied to the message
* @returns {Message} - This is a new message, with the provided action type set.
*/
export function messageFromValuesWithAction(
values: Message | Record<string, unknown>,
action: API.MessageAction,
): Message {
return fromValues({ ...values, action: action });
}

/**
* This is used to return an array of new messages, each set with the provided action type.
* It will apply the same action type to ALL messages.
* @param values - This is the array of message-like objects, with the values to be set on the new messages.
* @param action - This is the action type, applied to each message in the array.
* @returns {Message[]} - This is an array of new messages, each with the provided action type set.
*/
export function messageFromValuesArrayWithAction(values: unknown[], action: API.MessageAction): Message[] {
const count = values.length,
result = new Array(count);
for (let i = 0; i < count; i++) result[i] = messageFromValuesWithAction(values[i] as Record<string, unknown>, action);
return result;
}

export function fromValues(
values: Message | Record<string, unknown>,
options?: { stringifyAction?: boolean },
Expand Down

0 comments on commit 0506a3c

Please sign in to comment.