Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
adapter: ["laravel-echo", "react", "vue"]
adapter: ["react"]
steps:
- name: Checkout
uses: actions/checkout@v4
Expand All @@ -26,7 +26,7 @@ jobs:
- uses: actions/setup-node@v4
with:
node-version: "20"
registry-url: "https://registry.npmjs.org"
registry-url: "https://npm.pkg.github.com"
cache: pnpm

# Ensure npm 11.5.1 or later is installed
Expand All @@ -38,3 +38,6 @@ jobs:

- name: "Publish ${{ matrix.adapter }} to npm"
run: pnpm -r --filter ./packages/laravel-echo --filter ./packages/${{ matrix.adapter }} build && cd ./packages/${{ matrix.adapter }} && pnpm run release
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7 changes: 5 additions & 2 deletions packages/react/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@laravel/echo-react",
"name": "@xcoorp/echo-react",
"version": "2.2.4",
"description": "React hooks for seamless integration with Laravel Echo.",
"keywords": [
Expand All @@ -11,13 +11,16 @@
"homepage": "https://github.com/laravel/echo/tree/2.x/packages/react",
"repository": {
"type": "git",
"url": "https://github.com/laravel/echo"
"url": "https://github.com/xcoorp/echo"
},
"license": "MIT",
"author": {
"name": "Taylor Otwell"
},
"type": "module",
"publishConfig": {
"registry": "https://npm.pkg.github.com"
},
"main": "dist/index.common.js",
"module": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
10 changes: 7 additions & 3 deletions packages/react/src/hooks/use-echo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const useEcho = <
>(
channelName: string,
event: string | string[] = [],
callback: (payload: TPayload) => void = () => {},
callback: (payload: TPayload, event: string) => void = () => {},
dependencies: any[] = [],
visibility: TVisibility = "private" as TVisibility,
) => {
Expand All @@ -103,7 +103,9 @@ export const useEcho = <
}

events.forEach((e) => {
subscription.current.stopListening(e, callbackFunc);
subscription.current.stopListening(e, (payload: TPayload) =>
callbackFunc(payload, e),
);
});

listening.current = false;
Expand All @@ -115,7 +117,9 @@ export const useEcho = <
}

events.forEach((e) => {
subscription.current.listen(e, callbackFunc);
subscription.current.listen(e, (payload: TPayload) =>
callbackFunc(payload, e),
);
});

listening.current = true;
Expand Down
77 changes: 55 additions & 22 deletions packages/react/tests/use-echo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,24 @@ describe("useEcho hook", async () => {

const channel = echoInstance.private(channelName);

expect(channel.listen).toHaveBeenCalledWith(events[0], mockCallback);
expect(channel.listen).toHaveBeenCalledWith(events[1], mockCallback);
expect(channel.listen).toHaveBeenCalledWith(
events[0],
expect.any(Function),
);
expect(channel.listen).toHaveBeenCalledWith(
events[1],
expect.any(Function),
);

expect(() => unmount()).not.toThrow();

expect(channel.stopListening).toHaveBeenCalledWith(
events[0],
mockCallback,
expect.any(Function),
);
expect(channel.stopListening).toHaveBeenCalledWith(
events[1],
mockCallback,
expect.any(Function),
);
});

Expand Down Expand Up @@ -192,7 +198,7 @@ describe("useEcho hook", async () => {

expect(echoInstance.private(channelName).listen).toHaveBeenCalledWith(
event,
mockCallback,
expect.any(Function),
);
});

Expand Down Expand Up @@ -253,15 +259,24 @@ describe("useEcho hook", async () => {

const channel = echoInstance.private(channelName);

expect(channel.listen).toHaveBeenCalledWith(event, mockCallback);
expect(channel.listen).toHaveBeenCalledWith(
event,
expect.any(Function),
);

result.current.stopListening();

expect(channel.stopListening).toHaveBeenCalledWith(event, mockCallback);
expect(channel.stopListening).toHaveBeenCalledWith(
event,
expect.any(Function),
);

result.current.listen();

expect(channel.listen).toHaveBeenCalledWith(event, mockCallback);
expect(channel.listen).toHaveBeenCalledWith(
event,
expect.any(Function),
);
});

it("can manually stop listening to events", async () => {
Expand All @@ -276,7 +291,10 @@ describe("useEcho hook", async () => {
result.current.stopListening();

const channel = echoInstance.private(channelName);
expect(channel.stopListening).toHaveBeenCalledWith(event, mockCallback);
expect(channel.stopListening).toHaveBeenCalledWith(
event,
expect.any(Function),
);
});

it("stopListening is a no-op when not listening", async () => {
Expand Down Expand Up @@ -389,22 +407,22 @@ describe("useEchoModel hook", async () => {

expect(channel.listen).toHaveBeenCalledWith(
`.${events[0]}`,
mockCallback,
expect.any(Function),
);
expect(channel.listen).toHaveBeenCalledWith(
`.${events[1]}`,
mockCallback,
expect.any(Function),
);

expect(() => unmount()).not.toThrow();

expect(channel.stopListening).toHaveBeenCalledWith(
`.${events[0]}`,
mockCallback,
expect.any(Function),
);
expect(channel.stopListening).toHaveBeenCalledWith(
`.${events[1]}`,
mockCallback,
expect.any(Function),
);
});

Expand Down Expand Up @@ -533,7 +551,10 @@ describe("useEchoModel hook", async () => {
expect(echoInstance.private).toHaveBeenCalledWith(expectedChannelName);

const channel = echoInstance.private(expectedChannelName);
expect(channel.listen).toHaveBeenCalledWith(`.${event}`, mockCallback);
expect(channel.listen).toHaveBeenCalledWith(
`.${event}`,
expect.any(Function),
);
});

it("events and listeners are optional", async () => {
Expand Down Expand Up @@ -603,18 +624,24 @@ describe("useEchoPublic hook", async () => {

const channel = echoInstance.channel(channelName);

expect(channel.listen).toHaveBeenCalledWith(events[0], mockCallback);
expect(channel.listen).toHaveBeenCalledWith(events[1], mockCallback);
expect(channel.listen).toHaveBeenCalledWith(
events[0],
expect.any(Function),
);
expect(channel.listen).toHaveBeenCalledWith(
events[1],
expect.any(Function),
);

expect(() => unmount()).not.toThrow();

expect(channel.stopListening).toHaveBeenCalledWith(
events[0],
mockCallback,
expect.any(Function),
);
expect(channel.stopListening).toHaveBeenCalledWith(
events[1],
mockCallback,
expect.any(Function),
);
});

Expand Down Expand Up @@ -757,18 +784,24 @@ describe("useEchoPresence hook", async () => {

const channel = echoInstance.join(channelName);

expect(channel.listen).toHaveBeenCalledWith(events[0], mockCallback);
expect(channel.listen).toHaveBeenCalledWith(events[1], mockCallback);
expect(channel.listen).toHaveBeenCalledWith(
events[0],
expect.any(Function),
);
expect(channel.listen).toHaveBeenCalledWith(
events[1],
expect.any(Function),
);

expect(() => unmount()).not.toThrow();

expect(channel.stopListening).toHaveBeenCalledWith(
events[0],
mockCallback,
expect.any(Function),
);
expect(channel.stopListening).toHaveBeenCalledWith(
events[1],
mockCallback,
expect.any(Function),
);
});

Expand Down