-
Notifications
You must be signed in to change notification settings - Fork 415
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #413 The `<turbo-stream-source>` element accepts a `[src]` attribute, and uses that to connect Turbo to poll for streams published on the server side. When the element is connected to the document, the stream source is connected. When the element is disconnected, the stream is disconnected. When declared with an `ws://` or `wss://` URL, the underlying Stream Source will be a `WebSocket` instance. Otherwise, the connection is through an `EventSource`. Since the document's `<head>` is persistent across navigations, the `<turbo-stream-source>` is meant to be mounted within the `<body>` element. Typical full page navigations driven by Turbo will result in the `<body>` being discarded and replaced with the resulting document. It's the server's responsibility to ensure that the element is present on each page that requires streaming.
- Loading branch information
1 parent
8f94b5c
commit e015320
Showing
4 changed files
with
40 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { StreamSource } from "../core/types" | ||
import { connectStreamSource, disconnectStreamSource } from "../index" | ||
|
||
export class StreamSourceElement extends HTMLElement { | ||
streamSource: StreamSource | null = null | ||
|
||
connectedCallback() { | ||
this.streamSource = this.src.match(/^ws{1,2}:/) ? | ||
new WebSocket(this.src) : | ||
new EventSource(this.src) | ||
|
||
connectStreamSource(this.streamSource) | ||
} | ||
|
||
disconnectedCallback() { | ||
if (this.streamSource) { | ||
disconnectStreamSource(this.streamSource) | ||
} | ||
} | ||
|
||
get src(): string { | ||
return this.getAttribute("src") || "" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters