react-adonis-transmit
Version:
React context and hooks for Adonis Transmit
133 lines (130 loc) • 4.56 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
TransmitProvider: () => TransmitProvider,
useTransmit: () => useTransmit
});
module.exports = __toCommonJS(index_exports);
// src/TransmitContext.tsx
var import_react = __toESM(require("react"));
var import_transmit_client = require("@adonisjs/transmit-client");
var TransmitContext = (0, import_react.createContext)(null);
function TransmitProvider({
children,
baseUrl,
onMessage,
authHeader,
enableLogging = false
}) {
const transmit = (0, import_react.useRef)(
new import_transmit_client.Transmit({
baseUrl,
beforeSubscribe: (requestInit) => {
if (authHeader) {
requestInit.headers.append("Authorization", authHeader);
}
}
})
).current;
const subscriptions = (0, import_react.useRef)(/* @__PURE__ */ new Map());
(0, import_react.useEffect)(() => {
const onUnmount = () => {
if (enableLogging) {
console.log("[Transmit] - Closing connection");
}
};
if (enableLogging) {
transmit.on("connected", () => console.log("[SSE] connected"));
transmit.on("disconnected", () => console.log("[SSE] disconnected"));
transmit.on("initializing", () => console.log("[SSE] initializing..."));
transmit.on("reconnecting", () => console.log("[SSE] reconnecting..."));
}
return () => {
onUnmount();
};
}, []);
const handleMessage = (channel, event) => {
if (enableLogging) {
console.log(`[Transmit] ${channel} message:`, event);
}
onMessage == null ? void 0 : onMessage(channel, event);
};
const subscribe = (0, import_react.useCallback)((channel, callback) => {
if (enableLogging) {
console.log("[Transmit] - Subscribing to", channel);
}
let sub = subscriptions.current.get(channel);
if (!sub) {
sub = {
count: 0,
subscription: transmit.subscription(channel)
};
subscriptions.current.set(channel, sub);
sub.subscription.onMessage((event) => {
handleMessage(channel, event);
callback(event);
});
(async () => {
try {
await sub.subscription.create();
} catch (error) {
console.error("Failed to create subscription:", error);
}
})();
}
sub.count++;
if (enableLogging) {
console.log("[Transmit] - Subscribed to", channel);
}
return () => {
if (!subscriptions.current.has(channel)) return;
const sub2 = subscriptions.current.get(channel);
sub2.count--;
if (sub2.count === 0) {
sub2.subscription.delete();
subscriptions.current.delete(channel);
}
};
}, []);
return /* @__PURE__ */ import_react.default.createElement(TransmitContext.Provider, { value: { transmit, subscribe } }, children);
}
function useTransmit() {
const context = (0, import_react.useContext)(TransmitContext);
if (!context) {
throw new Error("useTransmit must be used within a TransmitProvider");
}
return context;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
TransmitProvider,
useTransmit
});
;