Skip to content

Commit

Permalink
fix: moving stream map to out.global (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
bill-min committed Jan 3, 2023
1 parent 577ecec commit fc36c57
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/components/micro-frame-slot/component/node.marko
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import StreamSource, { STREAM_SOURCE_MAP } from "../../stream-source/component/StreamSource";
import StreamSource from "../../stream-source/component/StreamSource";
$ const sourceName = input.from;
$ let err;
$ {
if (!STREAM_SOURCE_MAP.has(sourceName))
if (!out.global.STREAM_SOURCE_MAP_SERVER || !out.global.STREAM_SOURCE_MAP_SERVER.has(sourceName))
err = new Error(`micro-frame-sse ${sourceName} is not defined.`);
}

Expand All @@ -11,7 +11,7 @@ $ {
<${input.catch}(err)/>
</if>
<else>
$ const streamSource = STREAM_SOURCE_MAP.get(sourceName);
$ const streamSource = out.global.STREAM_SOURCE_MAP_SERVER.get(sourceName);
$ const stream = streamSource.slot(input.slot);
$ let finishLoading;
$ {
Expand Down
4 changes: 2 additions & 2 deletions src/components/micro-frame-slot/component/web.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { StreamWritable } from "../../stream-source/component/StreamWritable";
import { STREAM_SOURCE_MAP } from "../../stream-source/component/StreamSource";
import { STREAM_SOURCE_MAP_CLIENT } from "../../stream-source/component/StreamSource";
import getWritableDOM from "writable-dom";

interface Input {
Expand Down Expand Up @@ -55,7 +55,7 @@ export = {
let err: Error | undefined;

try {
const streamSource = STREAM_SOURCE_MAP.get(this.from);
const streamSource = STREAM_SOURCE_MAP_CLIENT.get(this.from);
// In case of micro-frame-sse pure server-side rendered,
// throw error when the slot trying to connect to the stream
if (!streamSource)
Expand Down
2 changes: 1 addition & 1 deletion src/components/stream-source/component/StreamSource.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import createWritable, { StreamWritable } from "./StreamWritable";

export const STREAM_SOURCE_MAP: Map<string, StreamSource> = new Map();
export const STREAM_SOURCE_MAP_CLIENT: Map<string, StreamSource> = new Map();
class StreamSource {
private readonly _slots: Map<string, StreamWritable>;
private _closed: boolean;
Expand Down
11 changes: 8 additions & 3 deletions src/components/stream-source/component/node.marko
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fetch from "make-fetch-happen";
import path from "path";
import StreamSource, { STREAM_SOURCE_MAP } from "./StreamSource";
import StreamSource from "./StreamSource";

static const cachePath = path.resolve("node_modules/.cache/fetch");

Expand All @@ -27,8 +27,13 @@ $ const request = async () => {
return res;
}
$ const streamSource = new StreamSource();
$ STREAM_SOURCE_MAP.set(input.name, streamSource);
$ {
const streamSource = new StreamSource();
if (out.global.STREAM_SOURCE_MAP_SERVER === undefined) {
out.global.STREAM_SOURCE_MAP_SERVER = new Map();
}
out.global.STREAM_SOURCE_MAP_SERVER.set(input.name, streamSource);
}
<div id=component.id data-src=input.src>
$ out.bf("@_", component, true);
Expand Down
4 changes: 2 additions & 2 deletions src/components/stream-source/component/web.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import StreamSource, { STREAM_SOURCE_MAP } from "./StreamSource";
import StreamSource, { STREAM_SOURCE_MAP_CLIENT } from "./StreamSource";

interface Input {
src: string;
Expand Down Expand Up @@ -43,7 +43,7 @@ export = {
ssrEl.removeAttribute("id");
} else {
const streamSource = new StreamSource();
STREAM_SOURCE_MAP.set(input.name, streamSource);
STREAM_SOURCE_MAP_CLIENT.set(input.name, streamSource);
this.streamSource = streamSource;
}
},
Expand Down

0 comments on commit fc36c57

Please sign in to comment.