Skip to content

Commit 19280c4

Browse files
chore: fix review
1 parent 4d1d47b commit 19280c4

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

templates/next/utils/dataAccess.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface Violation {
1111
propertyPath: string;
1212
}
1313

14-
const extractHubURL = function (response) {
14+
const extractHubURL = (response: Response): ?URL => {
1515
const linkHeader = response.headers.get('Link');
1616
if (!linkHeader) return null;
1717

templates/next/utils/mercure.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import has from "lodash/has";
2+
import { useEffect, useState } from "react";
23
import { PagedCollection } from "../types/Collection";
34
import { normalize } from "./dataAccess";
4-
import { useEffect, useState } from "react";
55

6-
const mercureSubscribe = (hubURL: string, data: any | PagedCollection<any>, setData: Function) => {
6+
const mercureSubscribe = (hubURL: string, data: any | PagedCollection<any>, setData: (data: any) => void) => {
77
const url = new URL(hubURL, window.origin);
88
url.searchParams.append("topic", (new URL(data["@id"], window.origin)).toString());
99
const eventSource = new EventSource(url.toString());
@@ -30,22 +30,28 @@ export const useMercure = (deps: any | PagedCollection<any>, hubURL: string) =>
3030
}
3131

3232
useEffect(() => {
33-
if (has(data, "{{{hydraPrefix}}}member") && typeof data["{{{hydraPrefix}}}member"] !== "undefined" && data["{{{hydraPrefix}}}member"].length !== 0) {
33+
if (has(data, "{{{hydraPrefix}}}member") && Array.isArray(data["{{{hydraPrefix}}}member"]) && data["{{{hydraPrefix}}}member"].length !== 0) {
3434
// It's a PagedCollection
3535
data["{{{hydraPrefix}}}member"].forEach((obj, pos) => mercureSubscribe(hubURL, obj, (datum) => {
3636
data["{{{hydraPrefix}}}member"][pos] = datum;
3737
setData(data);
3838
}));
39-
} else {
40-
// It's a single object
41-
const eventSource = mercureSubscribe(hubURL, data, setData);
4239

4340
return () => {
4441
eventSource.removeEventListener("message", setData);
4542

4643
return data;
47-
}
44+
};
4845
}
46+
47+
// It's a single object
48+
const eventSource = mercureSubscribe(hubURL, data, setData);
49+
50+
return () => {
51+
eventSource.removeEventListener("message", setData);
52+
53+
return data;
54+
};
4955
}, [data]);
5056

5157
return data;

0 commit comments

Comments
 (0)