remix-utils
Advanced tools
Comparing version 4.2.0 to 4.2.1
declare type EventSourceOptions = { | ||
init?: EventSourceInit; | ||
event: string; | ||
event?: string; | ||
}; | ||
@@ -11,3 +11,3 @@ /** | ||
*/ | ||
export declare function useEventSource(url: string | URL, options?: EventSourceOptions): string | null; | ||
export declare function useEventSource(url: string | URL, { event, init }?: EventSourceOptions): string | null; | ||
export {}; |
@@ -8,8 +8,7 @@ import { useEffect, useState } from "react"; | ||
*/ | ||
export function useEventSource(url, options) { | ||
export function useEventSource(url, { event = "message", init } = {}) { | ||
const [data, setData] = useState(null); | ||
useEffect(() => { | ||
var _a; | ||
const eventSource = new EventSource(url, options === null || options === void 0 ? void 0 : options.init); | ||
eventSource.addEventListener((_a = options === null || options === void 0 ? void 0 : options.event) !== null && _a !== void 0 ? _a : "message", handler); | ||
const eventSource = new EventSource(url, init); | ||
eventSource.addEventListener(event !== null && event !== void 0 ? event : "message", handler); | ||
function handler(event) { | ||
@@ -22,4 +21,4 @@ setData(event.data || "UNKNOWN_EVENT_DATA"); | ||
}; | ||
}, [url, options]); | ||
}, [url, event, init]); | ||
return data; | ||
} |
declare type EventSourceOptions = { | ||
init?: EventSourceInit; | ||
event: string; | ||
event?: string; | ||
}; | ||
@@ -11,3 +11,3 @@ /** | ||
*/ | ||
export declare function useEventSource(url: string | URL, options?: EventSourceOptions): string | null; | ||
export declare function useEventSource(url: string | URL, { event, init }?: EventSourceOptions): string | null; | ||
export {}; |
@@ -11,8 +11,7 @@ "use strict"; | ||
*/ | ||
function useEventSource(url, options) { | ||
function useEventSource(url, { event = "message", init } = {}) { | ||
const [data, setData] = (0, react_1.useState)(null); | ||
(0, react_1.useEffect)(() => { | ||
var _a; | ||
const eventSource = new EventSource(url, options === null || options === void 0 ? void 0 : options.init); | ||
eventSource.addEventListener((_a = options === null || options === void 0 ? void 0 : options.event) !== null && _a !== void 0 ? _a : "message", handler); | ||
const eventSource = new EventSource(url, init); | ||
eventSource.addEventListener(event !== null && event !== void 0 ? event : "message", handler); | ||
function handler(event) { | ||
@@ -25,5 +24,5 @@ setData(event.data || "UNKNOWN_EVENT_DATA"); | ||
}; | ||
}, [url, options]); | ||
}, [url, event, init]); | ||
return data; | ||
} | ||
exports.useEventSource = useEventSource; |
{ | ||
"name": "remix-utils", | ||
"version": "4.2.0", | ||
"version": "4.2.1", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "engines": { |
@@ -1101,6 +1101,6 @@ # Remix Utils | ||
const options = { event: "time" } | ||
function Counter() { | ||
let time = useEventSource("/sse/time", { | ||
event: "time", | ||
}); | ||
let time = useEventSource("/sse/time", options); | ||
@@ -1107,0 +1107,0 @@ if (!time) return null; |
198278
4340