Skip to content

Commit

Permalink
Event value can be a non-object, so fix that and add debug hook while…
Browse files Browse the repository at this point in the history
… we are at it.

- debug hook to WsHadlerConfig
- check if value is string or number in WsEventHandler
  • Loading branch information
floodfx committed Jan 13, 2023
1 parent f2ba76d commit c434210
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 180 deletions.
360 changes: 183 additions & 177 deletions packages/core/coverage/clover.xml

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/core/coverage/coverage-final.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/core/dist/liveview.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,7 @@ interface WsHandlerConfig {
flashAdaptor: FlashAdaptor;
pubSub: PubSub;
onError?: (err: any) => void;
debug?(msg: string): void;
}
declare class WsHandlerContext {
#private;
Expand Down
11 changes: 11 additions & 0 deletions packages/core/dist/liveview.js
Original file line number Diff line number Diff line change
Expand Up @@ -2973,6 +2973,9 @@ async function handleEvent(ctx, payload) {
ctx.clearFlash(key);
}
else {
if (typeof value === "string" || typeof value === "number") {
value = { value };
}
await ctx.liveView.handleEvent({ type: event, ...value }, ctx.socket);
}
return await ctx.liveView.render(ctx.socket.context, ctx.defaultLiveViewMeta());
Expand Down Expand Up @@ -3190,6 +3193,14 @@ class WsHandler {
this.#ws.subscribeToClose(() => this.close);
}
async handleMsg(msg) {
if (this.#config.debug) {
try {
this.#config.debug(JSON.stringify(msg));
}
catch (e) {
console.error("error debugging message", e);
}
}
try {
// attempt to prevent race conditions by queuing messages
// if we are already processing a message
Expand Down
11 changes: 11 additions & 0 deletions packages/core/dist/liveview.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2966,6 +2966,9 @@ async function handleEvent(ctx, payload) {
ctx.clearFlash(key);
}
else {
if (typeof value === "string" || typeof value === "number") {
value = { value };
}
await ctx.liveView.handleEvent({ type: event, ...value }, ctx.socket);
}
return await ctx.liveView.render(ctx.socket.context, ctx.defaultLiveViewMeta());
Expand Down Expand Up @@ -3183,6 +3186,14 @@ class WsHandler {
this.#ws.subscribeToClose(() => this.close);
}
async handleMsg(msg) {
if (this.#config.debug) {
try {
this.#config.debug(JSON.stringify(msg));
}
catch (e) {
console.error("error debugging message", e);
}
}
try {
// attempt to prevent race conditions by queuing messages
// if we are already processing a message
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/server/socket/ws/wsEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type HookPayload = Phx.EventPayload<"hook", Record<string, string>>;
export async function handleEvent(ctx: WsHandlerContext, payload: Phx.EventPayload): Promise<LiveViewTemplate> {
const { type, event, cid } = payload;

let value: { [key: string]: unknown } = {};
let value: { [key: string]: unknown } | string = {};
switch (type) {
case "click":
case "keyup":
Expand Down Expand Up @@ -106,6 +106,9 @@ export async function handleEvent(ctx: WsHandlerContext, payload: Phx.EventPaylo
const key = clearFlashPayload.value.key;
ctx.clearFlash(key);
} else {
if (typeof value === "string" || typeof value === "number") {
value = { value };
}
await ctx.liveView.handleEvent({ type: event, ...value }, ctx.socket);
}

Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/server/socket/ws/wsHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface WsHandlerConfig {
flashAdaptor: FlashAdaptor;
pubSub: PubSub;
onError?: (err: any) => void;
debug?(msg: string): void;
}

export class WsHandlerContext {
Expand Down Expand Up @@ -152,6 +153,13 @@ export class WsHandler {
}

async handleMsg(msg: Phx.Msg<unknown>) {
if (this.#config.debug) {
try {
this.#config.debug(JSON.stringify(msg));
} catch (e) {
console.error("error debugging message", e);
}
}
try {
// attempt to prevent race conditions by queuing messages
// if we are already processing a message
Expand Down

0 comments on commit c434210

Please sign in to comment.