mirror of
https://github.com/nvms/prsm.git
synced 2025-12-16 08:00:53 +00:00
namespace messages
This commit is contained in:
parent
66803c1177
commit
bbd48020de
@ -110,11 +110,11 @@ export class MeshClient extends EventEmitter {
|
|||||||
this.connection.on("message", (data) => {
|
this.connection.on("message", (data) => {
|
||||||
this.emit("message", data);
|
this.emit("message", data);
|
||||||
|
|
||||||
if (data.command === "record-update") {
|
if (data.command === "mesh/record-update") {
|
||||||
this.handleRecordUpdate(data.payload);
|
this.handleRecordUpdate(data.payload);
|
||||||
} else if (data.command === "presence-update") {
|
} else if (data.command === "mesh/presence-update") {
|
||||||
this.handlePresenceUpdate(data.payload);
|
this.handlePresenceUpdate(data.payload);
|
||||||
} else if (data.command === "subscription-message") {
|
} else if (data.command === "mesh/subscription-message") {
|
||||||
this.emit(data.command, data.payload);
|
this.emit(data.command, data.payload);
|
||||||
} else {
|
} else {
|
||||||
const systemCommands = [
|
const systemCommands = [
|
||||||
@ -458,7 +458,7 @@ export class MeshClient extends EventEmitter {
|
|||||||
options?: { historyLimit?: number }
|
options?: { historyLimit?: number }
|
||||||
): Promise<{ success: boolean; history: string[] }> {
|
): Promise<{ success: boolean; history: string[] }> {
|
||||||
this.on(
|
this.on(
|
||||||
"subscription-message",
|
"mesh/subscription-message",
|
||||||
async (data: { channel: string; message: string }) => {
|
async (data: { channel: string; message: string }) => {
|
||||||
if (data.channel === channel) {
|
if (data.channel === channel) {
|
||||||
await callback(data.message);
|
await callback(data.message);
|
||||||
@ -468,7 +468,7 @@ export class MeshClient extends EventEmitter {
|
|||||||
|
|
||||||
const historyLimit = options?.historyLimit;
|
const historyLimit = options?.historyLimit;
|
||||||
|
|
||||||
return this.command("subscribe-channel", { channel, historyLimit }).then(
|
return this.command("mesh/subscribe-channel", { channel, historyLimit }).then(
|
||||||
(result) => {
|
(result) => {
|
||||||
if (result.success && result.history && result.history.length > 0) {
|
if (result.success && result.history && result.history.length > 0) {
|
||||||
result.history.forEach((message: string) => {
|
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.
|
* @returns {Promise<boolean>} A promise that resolves to true if the unsubscription is successful, or false otherwise.
|
||||||
*/
|
*/
|
||||||
unsubscribeChannel(channel: string): Promise<boolean> {
|
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";
|
const mode = options?.mode ?? "full";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await this.command("subscribe-record", { recordId, mode });
|
const result = await this.command("mesh/subscribe-record", { recordId, mode });
|
||||||
|
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
this.recordSubscriptions.set(recordId, {
|
this.recordSubscriptions.set(recordId, {
|
||||||
@ -553,7 +553,7 @@ export class MeshClient extends EventEmitter {
|
|||||||
*/
|
*/
|
||||||
async unsubscribeRecord(recordId: string): Promise<boolean> {
|
async unsubscribeRecord(recordId: string): Promise<boolean> {
|
||||||
try {
|
try {
|
||||||
const success = await this.command("unsubscribe-record", { recordId });
|
const success = await this.command("mesh/unsubscribe-record", { recordId });
|
||||||
if (success) {
|
if (success) {
|
||||||
this.recordSubscriptions.delete(recordId);
|
this.recordSubscriptions.delete(recordId);
|
||||||
}
|
}
|
||||||
@ -576,7 +576,7 @@ export class MeshClient extends EventEmitter {
|
|||||||
*/
|
*/
|
||||||
async publishRecordUpdate(recordId: string, newValue: any): Promise<boolean> {
|
async publishRecordUpdate(recordId: string, newValue: any): Promise<boolean> {
|
||||||
try {
|
try {
|
||||||
const result = await this.command("publish-record-update", {
|
const result = await this.command("mesh/publish-record-update", {
|
||||||
recordId,
|
recordId,
|
||||||
newValue,
|
newValue,
|
||||||
});
|
});
|
||||||
@ -608,7 +608,7 @@ export class MeshClient extends EventEmitter {
|
|||||||
}) => void | Promise<void>
|
}) => void | Promise<void>
|
||||||
): Promise<{ success: boolean; present: string[] }> {
|
): Promise<{ success: boolean; present: string[] }> {
|
||||||
try {
|
try {
|
||||||
const result = await this.command("subscribe-presence", { roomName });
|
const result = await this.command("mesh/subscribe-presence", { roomName });
|
||||||
|
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
this.presenceSubscriptions.set(roomName, callback);
|
this.presenceSubscriptions.set(roomName, callback);
|
||||||
@ -635,7 +635,7 @@ export class MeshClient extends EventEmitter {
|
|||||||
*/
|
*/
|
||||||
async unsubscribePresence(roomName: string): Promise<boolean> {
|
async unsubscribePresence(roomName: string): Promise<boolean> {
|
||||||
try {
|
try {
|
||||||
const success = await this.command("unsubscribe-presence", { roomName });
|
const success = await this.command("mesh/unsubscribe-presence", { roomName });
|
||||||
if (success) {
|
if (success) {
|
||||||
this.presenceSubscriptions.delete(roomName);
|
this.presenceSubscriptions.delete(roomName);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -237,7 +237,7 @@ export class MeshServer extends WebSocketServer {
|
|||||||
for (const connection of this.channelSubscriptions[channel]) {
|
for (const connection of this.channelSubscriptions[channel]) {
|
||||||
if (!connection.isDead) {
|
if (!connection.isDead) {
|
||||||
connection.send({
|
connection.send({
|
||||||
command: "presence-update",
|
command: "mesh/presence-update",
|
||||||
payload: JSON.parse(message),
|
payload: JSON.parse(message),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -247,7 +247,7 @@ export class MeshServer extends WebSocketServer {
|
|||||||
for (const connection of this.channelSubscriptions[channel]) {
|
for (const connection of this.channelSubscriptions[channel]) {
|
||||||
if (!connection.isDead) {
|
if (!connection.isDead) {
|
||||||
connection.send({
|
connection.send({
|
||||||
command: "subscription-message",
|
command: "mesh/subscription-message",
|
||||||
payload: { channel, message },
|
payload: { channel, message },
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -307,12 +307,12 @@ export class MeshServer extends WebSocketServer {
|
|||||||
if (connection && !connection.isDead) {
|
if (connection && !connection.isDead) {
|
||||||
if (mode === "patch" && patch) {
|
if (mode === "patch" && patch) {
|
||||||
connection.send({
|
connection.send({
|
||||||
command: "record-update",
|
command: "mesh/record-update",
|
||||||
payload: { recordId, patch, version },
|
payload: { recordId, patch, version },
|
||||||
});
|
});
|
||||||
} else if (mode === "full" && newValue !== undefined) {
|
} else if (mode === "full" && newValue !== undefined) {
|
||||||
connection.send({
|
connection.send({
|
||||||
command: "record-update",
|
command: "mesh/record-update",
|
||||||
payload: { recordId, full: newValue, version },
|
payload: { recordId, full: newValue, version },
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -669,7 +669,7 @@ export class MeshServer extends WebSocketServer {
|
|||||||
this.registerCommand<
|
this.registerCommand<
|
||||||
{ channel: string; historyLimit?: number },
|
{ channel: string; historyLimit?: number },
|
||||||
{ success: boolean; history?: string[] }
|
{ success: boolean; history?: string[] }
|
||||||
>("subscribe-channel", async (ctx) => {
|
>("mesh/subscribe-channel", async (ctx) => {
|
||||||
const { channel, historyLimit } = ctx.payload;
|
const { channel, historyLimit } = ctx.payload;
|
||||||
|
|
||||||
if (!(await this.isChannelExposed(channel, ctx.connection))) {
|
if (!(await this.isChannelExposed(channel, ctx.connection))) {
|
||||||
@ -704,7 +704,7 @@ export class MeshServer extends WebSocketServer {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.registerCommand<{ channel: string }, boolean>(
|
this.registerCommand<{ channel: string }, boolean>(
|
||||||
"unsubscribe-channel",
|
"mesh/unsubscribe-channel",
|
||||||
async (ctx) => {
|
async (ctx) => {
|
||||||
const { channel } = ctx.payload;
|
const { channel } = ctx.payload;
|
||||||
if (this.channelSubscriptions[channel]) {
|
if (this.channelSubscriptions[channel]) {
|
||||||
@ -729,7 +729,7 @@ export class MeshServer extends WebSocketServer {
|
|||||||
this.registerCommand<
|
this.registerCommand<
|
||||||
{ recordId: string; mode?: "patch" | "full" },
|
{ recordId: string; mode?: "patch" | "full" },
|
||||||
{ success: boolean; record?: any; version?: number }
|
{ success: boolean; record?: any; version?: number }
|
||||||
>("subscribe-record", async (ctx) => {
|
>("mesh/subscribe-record", async (ctx) => {
|
||||||
const { recordId, mode = "full" } = ctx.payload;
|
const { recordId, mode = "full" } = ctx.payload;
|
||||||
const connectionId = ctx.connection.id;
|
const connectionId = ctx.connection.id;
|
||||||
|
|
||||||
@ -754,7 +754,7 @@ export class MeshServer extends WebSocketServer {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.registerCommand<{ recordId: string }, boolean>(
|
this.registerCommand<{ recordId: string }, boolean>(
|
||||||
"unsubscribe-record",
|
"mesh/unsubscribe-record",
|
||||||
async (ctx) => {
|
async (ctx) => {
|
||||||
const { recordId } = ctx.payload;
|
const { recordId } = ctx.payload;
|
||||||
const connectionId = ctx.connection.id;
|
const connectionId = ctx.connection.id;
|
||||||
@ -775,7 +775,7 @@ export class MeshServer extends WebSocketServer {
|
|||||||
this.registerCommand<
|
this.registerCommand<
|
||||||
{ recordId: string; newValue: any },
|
{ recordId: string; newValue: any },
|
||||||
{ success: boolean }
|
{ success: boolean }
|
||||||
>("publish-record-update", async (ctx) => {
|
>("mesh/publish-record-update", async (ctx) => {
|
||||||
const { recordId, newValue } = ctx.payload;
|
const { recordId, newValue } = ctx.payload;
|
||||||
|
|
||||||
if (!(await this.isRecordWritable(recordId, ctx.connection))) {
|
if (!(await this.isRecordWritable(recordId, ctx.connection))) {
|
||||||
@ -801,7 +801,7 @@ export class MeshServer extends WebSocketServer {
|
|||||||
this.registerCommand<
|
this.registerCommand<
|
||||||
{ roomName: string },
|
{ roomName: string },
|
||||||
{ success: boolean; present: string[] }
|
{ success: boolean; present: string[] }
|
||||||
>("subscribe-presence", async (ctx) => {
|
>("mesh/subscribe-presence", async (ctx) => {
|
||||||
const { roomName } = ctx.payload;
|
const { roomName } = ctx.payload;
|
||||||
const connectionId = ctx.connection.id;
|
const connectionId = ctx.connection.id;
|
||||||
|
|
||||||
@ -841,7 +841,7 @@ export class MeshServer extends WebSocketServer {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.registerCommand<{ roomName: string }, boolean>(
|
this.registerCommand<{ roomName: string }, boolean>(
|
||||||
"unsubscribe-presence",
|
"mesh/unsubscribe-presence",
|
||||||
async (ctx) => {
|
async (ctx) => {
|
||||||
const { roomName } = ctx.payload;
|
const { roomName } = ctx.payload;
|
||||||
const presenceChannel = `mesh:presence:updates:${roomName}`;
|
const presenceChannel = `mesh:presence:updates:${roomName}`;
|
||||||
|
|||||||
@ -310,12 +310,12 @@ describe("Record Subscription", () => {
|
|||||||
|
|
||||||
// verify unsubscribe and subscribe were called for resync
|
// verify unsubscribe and subscribe were called for resync
|
||||||
expect(commandSpy).toHaveBeenCalledWith(
|
expect(commandSpy).toHaveBeenCalledWith(
|
||||||
"unsubscribe-record",
|
"mesh/unsubscribe-record",
|
||||||
{ recordId },
|
{ recordId },
|
||||||
30000
|
30000
|
||||||
);
|
);
|
||||||
expect(commandSpy).toHaveBeenCalledWith(
|
expect(commandSpy).toHaveBeenCalledWith(
|
||||||
"subscribe-record",
|
"mesh/subscribe-record",
|
||||||
{
|
{
|
||||||
recordId,
|
recordId,
|
||||||
mode: "patch",
|
mode: "patch",
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user