namespace messages

This commit is contained in:
nvms 2025-04-19 20:57:48 -04:00
parent 66803c1177
commit bbd48020de
3 changed files with 24 additions and 24 deletions

View File

@ -110,11 +110,11 @@ export class MeshClient extends EventEmitter {
this.connection.on("message", (data) => {
this.emit("message", data);
if (data.command === "record-update") {
if (data.command === "mesh/record-update") {
this.handleRecordUpdate(data.payload);
} else if (data.command === "presence-update") {
} else if (data.command === "mesh/presence-update") {
this.handlePresenceUpdate(data.payload);
} else if (data.command === "subscription-message") {
} else if (data.command === "mesh/subscription-message") {
this.emit(data.command, data.payload);
} else {
const systemCommands = [
@ -458,7 +458,7 @@ export class MeshClient extends EventEmitter {
options?: { historyLimit?: number }
): Promise<{ success: boolean; history: string[] }> {
this.on(
"subscription-message",
"mesh/subscription-message",
async (data: { channel: string; message: string }) => {
if (data.channel === channel) {
await callback(data.message);
@ -468,7 +468,7 @@ export class MeshClient extends EventEmitter {
const historyLimit = options?.historyLimit;
return this.command("subscribe-channel", { channel, historyLimit }).then(
return this.command("mesh/subscribe-channel", { channel, historyLimit }).then(
(result) => {
if (result.success && result.history && result.history.length > 0) {
result.history.forEach((message: string) => {
@ -491,7 +491,7 @@ export class MeshClient extends EventEmitter {
* @returns {Promise<boolean>} A promise that resolves to true if the unsubscription is successful, or false otherwise.
*/
unsubscribeChannel(channel: string): Promise<boolean> {
return this.command("unsubscribe-channel", { channel });
return this.command("mesh/unsubscribe-channel", { channel });
}
/**
@ -515,7 +515,7 @@ export class MeshClient extends EventEmitter {
const mode = options?.mode ?? "full";
try {
const result = await this.command("subscribe-record", { recordId, mode });
const result = await this.command("mesh/subscribe-record", { recordId, mode });
if (result.success) {
this.recordSubscriptions.set(recordId, {
@ -553,7 +553,7 @@ export class MeshClient extends EventEmitter {
*/
async unsubscribeRecord(recordId: string): Promise<boolean> {
try {
const success = await this.command("unsubscribe-record", { recordId });
const success = await this.command("mesh/unsubscribe-record", { recordId });
if (success) {
this.recordSubscriptions.delete(recordId);
}
@ -576,7 +576,7 @@ export class MeshClient extends EventEmitter {
*/
async publishRecordUpdate(recordId: string, newValue: any): Promise<boolean> {
try {
const result = await this.command("publish-record-update", {
const result = await this.command("mesh/publish-record-update", {
recordId,
newValue,
});
@ -608,7 +608,7 @@ export class MeshClient extends EventEmitter {
}) => void | Promise<void>
): Promise<{ success: boolean; present: string[] }> {
try {
const result = await this.command("subscribe-presence", { roomName });
const result = await this.command("mesh/subscribe-presence", { roomName });
if (result.success) {
this.presenceSubscriptions.set(roomName, callback);
@ -635,7 +635,7 @@ export class MeshClient extends EventEmitter {
*/
async unsubscribePresence(roomName: string): Promise<boolean> {
try {
const success = await this.command("unsubscribe-presence", { roomName });
const success = await this.command("mesh/unsubscribe-presence", { roomName });
if (success) {
this.presenceSubscriptions.delete(roomName);
}

View File

@ -237,7 +237,7 @@ export class MeshServer extends WebSocketServer {
for (const connection of this.channelSubscriptions[channel]) {
if (!connection.isDead) {
connection.send({
command: "presence-update",
command: "mesh/presence-update",
payload: JSON.parse(message),
});
}
@ -247,7 +247,7 @@ export class MeshServer extends WebSocketServer {
for (const connection of this.channelSubscriptions[channel]) {
if (!connection.isDead) {
connection.send({
command: "subscription-message",
command: "mesh/subscription-message",
payload: { channel, message },
});
}
@ -307,12 +307,12 @@ export class MeshServer extends WebSocketServer {
if (connection && !connection.isDead) {
if (mode === "patch" && patch) {
connection.send({
command: "record-update",
command: "mesh/record-update",
payload: { recordId, patch, version },
});
} else if (mode === "full" && newValue !== undefined) {
connection.send({
command: "record-update",
command: "mesh/record-update",
payload: { recordId, full: newValue, version },
});
}
@ -669,7 +669,7 @@ export class MeshServer extends WebSocketServer {
this.registerCommand<
{ channel: string; historyLimit?: number },
{ success: boolean; history?: string[] }
>("subscribe-channel", async (ctx) => {
>("mesh/subscribe-channel", async (ctx) => {
const { channel, historyLimit } = ctx.payload;
if (!(await this.isChannelExposed(channel, ctx.connection))) {
@ -704,7 +704,7 @@ export class MeshServer extends WebSocketServer {
});
this.registerCommand<{ channel: string }, boolean>(
"unsubscribe-channel",
"mesh/unsubscribe-channel",
async (ctx) => {
const { channel } = ctx.payload;
if (this.channelSubscriptions[channel]) {
@ -729,7 +729,7 @@ export class MeshServer extends WebSocketServer {
this.registerCommand<
{ recordId: string; mode?: "patch" | "full" },
{ success: boolean; record?: any; version?: number }
>("subscribe-record", async (ctx) => {
>("mesh/subscribe-record", async (ctx) => {
const { recordId, mode = "full" } = ctx.payload;
const connectionId = ctx.connection.id;
@ -754,7 +754,7 @@ export class MeshServer extends WebSocketServer {
});
this.registerCommand<{ recordId: string }, boolean>(
"unsubscribe-record",
"mesh/unsubscribe-record",
async (ctx) => {
const { recordId } = ctx.payload;
const connectionId = ctx.connection.id;
@ -775,7 +775,7 @@ export class MeshServer extends WebSocketServer {
this.registerCommand<
{ recordId: string; newValue: any },
{ success: boolean }
>("publish-record-update", async (ctx) => {
>("mesh/publish-record-update", async (ctx) => {
const { recordId, newValue } = ctx.payload;
if (!(await this.isRecordWritable(recordId, ctx.connection))) {
@ -801,7 +801,7 @@ export class MeshServer extends WebSocketServer {
this.registerCommand<
{ roomName: string },
{ success: boolean; present: string[] }
>("subscribe-presence", async (ctx) => {
>("mesh/subscribe-presence", async (ctx) => {
const { roomName } = ctx.payload;
const connectionId = ctx.connection.id;
@ -841,7 +841,7 @@ export class MeshServer extends WebSocketServer {
});
this.registerCommand<{ roomName: string }, boolean>(
"unsubscribe-presence",
"mesh/unsubscribe-presence",
async (ctx) => {
const { roomName } = ctx.payload;
const presenceChannel = `mesh:presence:updates:${roomName}`;

View File

@ -310,12 +310,12 @@ describe("Record Subscription", () => {
// verify unsubscribe and subscribe were called for resync
expect(commandSpy).toHaveBeenCalledWith(
"unsubscribe-record",
"mesh/unsubscribe-record",
{ recordId },
30000
);
expect(commandSpy).toHaveBeenCalledWith(
"subscribe-record",
"mesh/subscribe-record",
{
recordId,
mode: "patch",