Skip to content

Commit

Permalink
Add web component
Browse files Browse the repository at this point in the history
  • Loading branch information
Bitaru committed Dec 8, 2022
1 parent da76a70 commit 6555acd
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@
/spec/reports/
/tmp/

node_modules

# rspec failure tracking
.rspec_status
3 changes: 3 additions & 0 deletions web/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
npm-debug.log
node_modules
.DS_Store
39 changes: 39 additions & 0 deletions web/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { connectStreamSource, disconnectStreamSource } from "@hotwired/turbo";

export default class TurboTrain extends HTMLElement {
static get observedAttributes() {
return [ 'href', 'session', 'name' ];
}

constructor() {
super();
}

connectedCallback() {
this.eventSource = new EventSource(`${this.href}/mercure?topic=${this.name}&authorization=${this.session}`);
connectStreamSource(this.eventSource);
}

disconnectedCallback() {
disconnectStreamSource(this.eventSource);
}

get href() {
return this.getAttribute('href');
}

get session() {
return this.getAttribute('session');
}

get name() {
return this.getAttribute('name');
}
}

if (
typeof window !== 'undefined' &&
!window.customElements.get('turbo-train-stream-source')
) {
window.customElements.define('turbo-train-stream-source', TurboTrain)
}
33 changes: 33 additions & 0 deletions web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions web/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@uscreentv/turbo-train",
"version": "0.0.1",
"description": "",
"main": "index.js",
"type": "module",
"exports": {
".": {
"import": "./index.js"
}
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Uscreen-video/turbo-train.git"
},
"author": "",
"license": "MIT",
"bugs": {
"url": "https://github.com/Uscreen-video/turbo-train/issues"
},
"homepage": "https://github.com/Uscreen-video/turbo-train#readme",
"peerDependencies": {
"@hotwired/turbo": "^7.2.4"
}
}

0 comments on commit 6555acd

Please sign in to comment.