diff --git a/src/components/micro-frame-slot/component/node.marko b/src/components/micro-frame-slot/component/node.marko index b32713a..eb11a61 100644 --- a/src/components/micro-frame-slot/component/node.marko +++ b/src/components/micro-frame-slot/component/node.marko @@ -1,51 +1,39 @@ -import StreamSource from "../../stream-source/component/StreamSource"; -$ const sourceName = input.from; -$ let err; +import { getOrCreateStreamSource } from "../../stream-source/component/helper"; + $ { - 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.`); + const streamSource = getOrCreateStreamSource(input.from, out); + const stream = streamSource.slot(input.slot); + let finishLoading; + const loadingPromise = + input.loading && new Promise((res) => (finishLoading = res)); } - - - $ if (!input.catch) throw err; - <${input.catch}(err)/> - - - $ const streamSource = out.global.STREAM_SOURCE_MAP_SERVER.get(sourceName); - $ const stream = streamSource.slot(input.slot); - $ let finishLoading; - $ { - const loadingPromise = - input.loading && new Promise((res) => (finishLoading = res)); - } -
- - - <@then|{ value, done }|> - - $!{value} - - - - $ finishLoading && finishLoading(); - - - <@catch|e|> +
+ + + <@then|{ value, done }|> + + $!{value} + + + $ finishLoading && finishLoading(); - <${input.catch}(e)/> - - - - $ out.bf("@_", component, true); - - - - - + + + <@catch|e|> + $ finishLoading && finishLoading(); + <${input.catch}(e)/> + + + + $ out.bf("@_", component, true); + + + - $ out.ef(); -
- + + + $ out.ef(); +
diff --git a/src/components/micro-frame-slot/component/web.component.ts b/src/components/micro-frame-slot/component/web.component.ts index d4e5c9a..f6fb144 100644 --- a/src/components/micro-frame-slot/component/web.component.ts +++ b/src/components/micro-frame-slot/component/web.component.ts @@ -1,6 +1,7 @@ import { StreamWritable } from "../../stream-source/component/StreamWritable"; -import { STREAM_SOURCE_MAP_CLIENT } from "../../stream-source/component/StreamSource"; +import StreamSource from "../../stream-source/component/StreamSource"; import getWritableDOM from "writable-dom"; +import { getOrCreateStreamSource } from "../../stream-source/component/helper"; interface Input { slot: string; @@ -36,34 +37,38 @@ export = { }; }, onMount() { - // Only trigger a new load if this wasn't ssr'd, or the src has changed. + this.streamSource = getOrCreateStreamSource(this.input.from); + this.handleSrcChange = this.handleSrcChange.bind(this); + this.streamSource.onInvalidate(this.handleSrcChange); this.onUpdate(); }, + handleSrcChange(src: string) { + this.curSrc = src; + this.forceUpdate(); + }, onDestroy() { this.slot?.end(); + this.streamSource.offInvalidate(this.handleSrcChange); }, async onUpdate() { - if (this.slotId === this.input.slot && this.from === this.input.from) + if ( + this.slotId === this.input.slot && + this.from === this.input.from && + this.prevSrc === this.curSrc + ) return; this.state.loading = true; this.state.err = undefined; this.slotId = this.input.slot; this.from = this.input.from; + this.prevSrc = this.curSrc; let writable: ReturnType | undefined; let err: Error | undefined; try { - 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) - throw new Error( - `micro-frame-sse ${this.from} is not defined or server-side rendered.` - ); - - this.slot = streamSource.slot(this.slotId); + this.slot = this.streamSource.slot(this.slotId); if (!this.slot) { return; @@ -98,9 +103,14 @@ export = { el: HTMLDivElement; slotId: string | undefined; from: string | undefined; + prevSrc: string | undefined; + curSrc: string | undefined; slot: StreamWritable | undefined; + streamSource: StreamSource; onUpdate(): unknown; onCreate(): unknown; onMount(): unknown; onDestroy(): unknown; + forceUpdate(): unknown; + handleSrcChange(src: string): unknown; }; diff --git a/src/components/micro-frame-slot/component/web.marko b/src/components/micro-frame-slot/component/web.marko index fe0aab2..2a0c1cb 100644 --- a/src/components/micro-frame-slot/component/web.marko +++ b/src/components/micro-frame-slot/component/web.marko @@ -12,5 +12,5 @@ static function noop() {} We put the streamed html in a preserved fragment. This allows Marko to avoid diffing that section. --> - $ out.bf("@_", component, !state.err && component.slotId === input.slot && component.from === input.from).ef(); + $ out.bf("@_", component, !state.err && component.slotId === input.slot && component.from === input.from && component.prevSrc === component.curSrc).ef(); diff --git a/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-invalid-sourcename/renders.expected/loading.0.html b/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-invalid-sourcename/renders.expected/loading.0.html deleted file mode 100644 index a760d8e..0000000 --- a/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-invalid-sourcename/renders.expected/loading.0.html +++ /dev/null @@ -1,6 +0,0 @@ -
- Host app -
- \ No newline at end of file diff --git a/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-invalid-sourcename/renders.expected/loading.1.html b/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-invalid-sourcename/renders.expected/loading.1.html deleted file mode 100644 index d4da446..0000000 --- a/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-invalid-sourcename/renders.expected/loading.1.html +++ /dev/null @@ -1,11 +0,0 @@ -
- Host app -
-
-
- Slot_1 Error: micro-frame-sse test_error is not defined or server-side rendered. -
-
- \ No newline at end of file diff --git a/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-invalid-sourcename/renders.expected/loading.2.html b/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-invalid-sourcename/renders.expected/loading.2.html deleted file mode 100644 index 9aa28c6..0000000 --- a/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-invalid-sourcename/renders.expected/loading.2.html +++ /dev/null @@ -1,15 +0,0 @@ -
- Host app -
-
-
- Slot_1 Error: micro-frame-sse test_error is not defined or server-side rendered. -
-
-

- test_html for slot_2 -

-
- \ No newline at end of file diff --git a/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-then-change-src/renders.expected/loading.0.html b/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-then-change-src/renders.expected/loading.0.html new file mode 100644 index 0000000..0fa1059 --- /dev/null +++ b/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-then-change-src/renders.expected/loading.0.html @@ -0,0 +1,9 @@ +
+ Host app +
+ + \ No newline at end of file diff --git a/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-then-change-src/renders.expected/loading.1.html b/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-then-change-src/renders.expected/loading.1.html new file mode 100644 index 0000000..b3a2eb1 --- /dev/null +++ b/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-then-change-src/renders.expected/loading.1.html @@ -0,0 +1,12 @@ +
+ Host app +
+ +
+
+
+ \ No newline at end of file diff --git a/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-then-change-src/renders.expected/loading.2.html b/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-then-change-src/renders.expected/loading.2.html new file mode 100644 index 0000000..5cdcd83 --- /dev/null +++ b/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-then-change-src/renders.expected/loading.2.html @@ -0,0 +1,16 @@ +
+ Host app +
+ +
+
+

+ test_html for slot_1 +

+
+
+ \ No newline at end of file diff --git a/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-then-change-src/renders.expected/loading.3.html b/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-then-change-src/renders.expected/loading.3.html new file mode 100644 index 0000000..d1886fa --- /dev/null +++ b/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-then-change-src/renders.expected/loading.3.html @@ -0,0 +1,20 @@ +
+ Host app +
+ +
+
+

+ test_html for slot_1 +

+
+
+

+ test_html for slot_2 +

+
+ \ No newline at end of file diff --git a/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-then-change-src/renders.expected/loading.4.html b/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-then-change-src/renders.expected/loading.4.html new file mode 100644 index 0000000..cf6eac2 --- /dev/null +++ b/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-then-change-src/renders.expected/loading.4.html @@ -0,0 +1,21 @@ +
+ Host app +
+ +
+
+

+ test_html for slot_1 +

+ next chunk for slot_1 +
+
+

+ test_html for slot_2 +

+
+ \ No newline at end of file diff --git a/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-then-change-src/renders.expected/step-0.0.html b/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-then-change-src/renders.expected/step-0.0.html new file mode 100644 index 0000000..b3a2eb1 --- /dev/null +++ b/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-then-change-src/renders.expected/step-0.0.html @@ -0,0 +1,12 @@ +
+ Host app +
+ +
+
+
+ \ No newline at end of file diff --git a/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-then-change-src/renders.expected/step-0.1.html b/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-then-change-src/renders.expected/step-0.1.html new file mode 100644 index 0000000..5cdcd83 --- /dev/null +++ b/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-then-change-src/renders.expected/step-0.1.html @@ -0,0 +1,16 @@ +
+ Host app +
+ +
+
+

+ test_html for slot_1 +

+
+
+ \ No newline at end of file diff --git a/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-then-change-src/renders.expected/step-0.2.html b/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-then-change-src/renders.expected/step-0.2.html new file mode 100644 index 0000000..d1886fa --- /dev/null +++ b/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-then-change-src/renders.expected/step-0.2.html @@ -0,0 +1,20 @@ +
+ Host app +
+ +
+
+

+ test_html for slot_1 +

+
+
+

+ test_html for slot_2 +

+
+ \ No newline at end of file diff --git a/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-then-change-src/renders.expected/step-0.3.html b/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-then-change-src/renders.expected/step-0.3.html new file mode 100644 index 0000000..9487d38 --- /dev/null +++ b/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-then-change-src/renders.expected/step-0.3.html @@ -0,0 +1,21 @@ +
+ Host app +
+ +
+
+

+ test_html for slot_1 +

+ after src change +
+
+

+ test_html for slot_2 +

+
+ \ No newline at end of file diff --git a/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/ssr-only/renders.expected/loading.2.html b/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/ssr-only/renders.expected/loading.2.html index c00b3f5..b4ebcac 100644 --- a/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/ssr-only/renders.expected/loading.2.html +++ b/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/ssr-only/renders.expected/loading.2.html @@ -29,19 +29,17 @@

-

- test_2_html for slot_1 -

-
\ No newline at end of file + style="display:none" +/> + \ No newline at end of file diff --git a/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/ssr-only/renders.expected/loading.3.html b/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/ssr-only/renders.expected/loading.3.html index 30cba39..ce474d4 100644 --- a/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/ssr-only/renders.expected/loading.3.html +++ b/src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/ssr-only/renders.expected/loading.3.html @@ -4,15 +4,11 @@
-
+/>

test_html for slot_1 @@ -22,63 +18,20 @@

test_html for slot_2

-
-
-

- test_2_html for slot_1 -

- next chunk for slot_1 -
-
-

- test_2_html for slot_2 -

-
- - -