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
24 changes: 24 additions & 0 deletions packages/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,30 @@ configureEcho({
});
```

### Without `pusher-js`

The default entry point imports `pusher-js` and uses it as the Pusher client for the Pusher-based broadcasters. If your application uses another broadcaster, such as Mercure, Socket.IO or the null broadcaster, import from `@laravel/echo-react/core` instead, so `pusher-js` doesn't need to be installed:

```ts
import { configureEcho, useEcho } from "@laravel/echo-react/core";

configureEcho({
broadcaster: "mercure",
});
```

The `core` entry point exports the same API. To use a Pusher-based broadcaster with it, pass the client yourself:

```ts
import Pusher from "pusher-js";
import { configureEcho } from "@laravel/echo-react/core";

configureEcho({
broadcaster: "reverb",
Pusher,
});
```

## Connection Status

You can get the current WebSocket connection status using the `useConnectionStatus` hook or the `echo().connectionStatus()` utility function.
Expand Down
6 changes: 6 additions & 0 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@
"react-native": "./dist/index.common.js",
"import": "./dist/index.js",
"require": "./dist/index.common.js"
},
"./core": {
"types": "./dist/core.d.ts",
"react-native": "./dist/core.common.js",
"import": "./dist/core.js",
"require": "./dist/core.common.js"
}
},
"files": [
Expand Down
20 changes: 18 additions & 2 deletions packages/react/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,30 @@ import Echo, {
type Broadcaster,
type EchoOptions,
} from "laravel-echo";
import Pusher from "pusher-js";
import type { ConfigDefaults } from "../types";

/**
* The Echo constructor argument, which allows a custom connector class as the broadcaster.
*/
type EchoConfig = ConstructorParameters<typeof Echo<keyof Broadcaster>>[0];

/**
* The Pusher client constructor accepted by the Pusher-based broadcasters.
*/
type PusherClient = NonNullable<EchoOptions<"pusher">["Pusher"]>;

let echoInstance: Echo<keyof Broadcaster> | null = null;
let echoConfig: EchoConfig | null = null;
let defaultPusher: PusherClient | null = null;

/**
* Register the Pusher client used when the configuration does not provide one.
*
* The default entry point registers `pusher-js`; the `core` entry point does not.
*/
export const registerDefaultPusher = (Pusher: PusherClient): void => {
defaultPusher = Pusher;
};

const getEchoInstance = <T extends keyof Broadcaster>(): Echo<T> => {
if (echoInstance) {
Expand All @@ -25,7 +39,9 @@ const getEchoInstance = <T extends keyof Broadcaster>(): Echo<T> => {
);
}

echoConfig.Pusher ??= Pusher;
if (defaultPusher) {
echoConfig.Pusher ??= defaultPusher;
}

echoInstance = new Echo<keyof Broadcaster>(echoConfig);

Expand Down
4 changes: 4 additions & 0 deletions packages/react/src/config/pusher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Pusher from "pusher-js";
import { registerDefaultPusher } from "./index";

registerDefaultPusher(Pusher);
14 changes: 14 additions & 0 deletions packages/react/src/core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export type { ConnectionStatus } from "laravel-echo";
export { configureEcho, echo, echoIsConfigured } from "./config/index";
export {
useChannel,
useConnectionStatus,
useEcho,
useEchoModel,
useEchoNotification,
useEchoPresence,
useEchoPublic,
usePresenceChannel,
usePublicChannel,
useSocketId,
} from "./hooks/use-echo";
2 changes: 2 additions & 0 deletions packages/react/src/index.iife.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import "./config/pusher";

export { configureEcho, echo } from "./config/index";
export {
useEcho,
Expand Down
17 changes: 3 additions & 14 deletions packages/react/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
export type { ConnectionStatus } from "laravel-echo";
export { configureEcho, echo, echoIsConfigured } from "./config/index";
export {
useChannel,
useConnectionStatus,
useEcho,
useEchoModel,
useEchoNotification,
useEchoPresence,
useEchoPublic,
usePresenceChannel,
usePublicChannel,
useSocketId,
} from "./hooks/use-echo";
import "./config/pusher";

export * from "./core";
49 changes: 49 additions & 0 deletions packages/react/tests/core.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";

describe("entry points", () => {
beforeEach(() => {
vi.resetModules();
vi.doUnmock("pusher-js");
});

it("loads the core entry point without pusher-js", async () => {
vi.doMock("pusher-js", () => {
throw new Error("pusher-js must not be imported");
});

const { configureEcho, echo } = await import("../src/core");

configureEcho({ broadcaster: "null" });

expect(echo().options.broadcaster).toBe("null");
expect(echo().options.Pusher).toBeUndefined();
});

it("keeps pusher-js as the default client in the default entry point", async () => {
const { default: Pusher } = await import("pusher-js");
const { configureEcho, echo } = await import("../src/index");

configureEcho({ broadcaster: "reverb", key: "app-key" });

expect(echo().options.Pusher).toBe(Pusher);
});

it("prefers the Pusher client passed to configureEcho", async () => {
class CustomPusher {}
const { configureEcho, echo } = await import("../src/index");

configureEcho({ broadcaster: "null", Pusher: CustomPusher as never });

expect(echo().options.Pusher).toBe(CustomPusher);
});

it("shares the Echo instance between both entry points", async () => {
const defaultEntry = await import("../src/index");
const coreEntry = await import("../src/core");

defaultEntry.configureEcho({ broadcaster: "null" });

expect(coreEntry.echoIsConfigured()).toBe(true);
expect(coreEntry.echo()).toBe(defaultEntry.echo());
});
});
7 changes: 6 additions & 1 deletion packages/react/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const config: UserConfig = (() => {
rollupOptions: {
external: ["react", "pusher-js"],
output: {
// Both entry points share this chunk, and with it one Echo instance.
chunkFileNames: "[name]-[hash].[format].js",
globals: {
react: "React",
"pusher-js": "Pusher",
Expand Down Expand Up @@ -68,7 +70,10 @@ const config: UserConfig = (() => {
]),
build: {
lib: {
entry: resolve(import.meta.dirname, "src/index.ts"),
entry: {
index: resolve(import.meta.dirname, "src/index.ts"),
core: resolve(import.meta.dirname, "src/core.ts"),
},
formats: ["es", "cjs"],
fileName: (format, entryName) => {
return `${entryName}.${format === "es" ? "js" : "common.js"}`;
Expand Down
24 changes: 24 additions & 0 deletions packages/svelte/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,30 @@ configureEcho({
});
```

### Without `pusher-js`

The default entry point imports `pusher-js` and uses it as the Pusher client for the Pusher-based broadcasters. If your application uses another broadcaster, such as Mercure, Socket.IO or the null broadcaster, import from `@laravel/echo-svelte/core` instead, so `pusher-js` doesn't need to be installed:

```ts
import { configureEcho, useEcho } from "@laravel/echo-svelte/core";

configureEcho({
broadcaster: "mercure",
});
```

The `core` entry point exports the same API. To use a Pusher-based broadcaster with it, pass the client yourself:

```ts
import Pusher from "pusher-js";
import { configureEcho } from "@laravel/echo-svelte/core";

configureEcho({
broadcaster: "reverb",
Pusher,
});
```

## Connection Status

You can get the current WebSocket connection status using the `useConnectionStatus` function or the `echo().connectionStatus()` utility function.
Expand Down
6 changes: 6 additions & 0 deletions packages/svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
"svelte": "./dist/index.js",
"import": "./dist/index.js",
"require": "./dist/index.common.js"
},
"./core": {
"types": "./dist/core.d.ts",
"svelte": "./dist/core.js",
"import": "./dist/core.js",
"require": "./dist/core.common.js"
}
},
"svelte": "./dist/index.js",
Expand Down
20 changes: 18 additions & 2 deletions packages/svelte/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,30 @@ import Echo, {
type Broadcaster,
type EchoOptions,
} from "laravel-echo";
import Pusher from "pusher-js";
import type { ConfigDefaults } from "../types";

/**
* The Echo constructor argument, which allows a custom connector class as the broadcaster.
*/
type EchoConfig = ConstructorParameters<typeof Echo<keyof Broadcaster>>[0];

/**
* The Pusher client constructor accepted by the Pusher-based broadcasters.
*/
type PusherClient = NonNullable<EchoOptions<"pusher">["Pusher"]>;

let echoInstance: Echo<keyof Broadcaster> | null = null;
let echoConfig: EchoConfig | null = null;
let defaultPusher: PusherClient | null = null;

/**
* Register the Pusher client used when the configuration does not provide one.
*
* The default entry point registers `pusher-js`; the `core` entry point does not.
*/
export const registerDefaultPusher = (Pusher: PusherClient): void => {
defaultPusher = Pusher;
};

const getEchoInstance = <T extends keyof Broadcaster>(): Echo<T> => {
if (echoInstance) {
Expand All @@ -25,7 +39,9 @@ const getEchoInstance = <T extends keyof Broadcaster>(): Echo<T> => {
);
}

echoConfig.Pusher ??= Pusher;
if (defaultPusher) {
echoConfig.Pusher ??= defaultPusher;
}

echoInstance = new Echo<keyof Broadcaster>(echoConfig);

Expand Down
4 changes: 4 additions & 0 deletions packages/svelte/src/config/pusher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Pusher from "pusher-js";
import { registerDefaultPusher } from "./index";

registerDefaultPusher(Pusher);
14 changes: 14 additions & 0 deletions packages/svelte/src/core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export type { ConnectionStatus } from "laravel-echo";
export { configureEcho, echo, echoIsConfigured } from "./config/index";
export {
useChannel,
useConnectionStatus,
useEcho,
useEchoModel,
useEchoNotification,
useEchoPresence,
useEchoPublic,
usePresenceChannel,
usePublicChannel,
useSocketId,
} from "./runes/useEcho";
2 changes: 2 additions & 0 deletions packages/svelte/src/index.iife.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import "./config/pusher";

export { configureEcho, echo, echoIsConfigured } from "./config/index";
export {
useConnectionStatus,
Expand Down
17 changes: 3 additions & 14 deletions packages/svelte/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
export type { ConnectionStatus } from "laravel-echo";
export { configureEcho, echo, echoIsConfigured } from "./config/index";
export {
useChannel,
useConnectionStatus,
useEcho,
useEchoModel,
useEchoNotification,
useEchoPresence,
useEchoPublic,
usePresenceChannel,
usePublicChannel,
useSocketId,
} from "./runes/useEcho";
import "./config/pusher";

export * from "./core";
49 changes: 49 additions & 0 deletions packages/svelte/tests/core.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";

describe("entry points", () => {
beforeEach(() => {
vi.resetModules();
vi.doUnmock("pusher-js");
});

it("loads the core entry point without pusher-js", async () => {
vi.doMock("pusher-js", () => {
throw new Error("pusher-js must not be imported");
});

const { configureEcho, echo } = await import("../src/core");

configureEcho({ broadcaster: "null" });

expect(echo().options.broadcaster).toBe("null");
expect(echo().options.Pusher).toBeUndefined();
});

it("keeps pusher-js as the default client in the default entry point", async () => {
const { default: Pusher } = await import("pusher-js");
const { configureEcho, echo } = await import("../src/index");

configureEcho({ broadcaster: "reverb", key: "app-key" });

expect(echo().options.Pusher).toBe(Pusher);
});

it("prefers the Pusher client passed to configureEcho", async () => {
class CustomPusher {}
const { configureEcho, echo } = await import("../src/index");

configureEcho({ broadcaster: "null", Pusher: CustomPusher as never });

expect(echo().options.Pusher).toBe(CustomPusher);
});

it("shares the Echo instance between both entry points", async () => {
const defaultEntry = await import("../src/index");
const coreEntry = await import("../src/core");

defaultEntry.configureEcho({ broadcaster: "null" });

expect(coreEntry.echoIsConfigured()).toBe(true);
expect(coreEntry.echo()).toBe(defaultEntry.echo());
});
});
Loading
Loading