From 6555acdb14ea54d26b58f627be12cba8264b709c Mon Sep 17 00:00:00 2001 From: Bitaru Date: Thu, 8 Dec 2022 12:33:00 +0100 Subject: [PATCH] Add web component --- .gitignore | 2 ++ web/.npmignore | 3 +++ web/index.js | 39 +++++++++++++++++++++++++++++++++++++++ web/package-lock.json | 33 +++++++++++++++++++++++++++++++++ web/package.json | 28 ++++++++++++++++++++++++++++ 5 files changed, 105 insertions(+) create mode 100644 web/.npmignore create mode 100644 web/index.js create mode 100644 web/package-lock.json create mode 100644 web/package.json diff --git a/.gitignore b/.gitignore index b04a8c8..1a26b9f 100644 --- a/.gitignore +++ b/.gitignore @@ -7,5 +7,7 @@ /spec/reports/ /tmp/ +node_modules + # rspec failure tracking .rspec_status diff --git a/web/.npmignore b/web/.npmignore new file mode 100644 index 0000000..299aea9 --- /dev/null +++ b/web/.npmignore @@ -0,0 +1,3 @@ +npm-debug.log +node_modules +.DS_Store diff --git a/web/index.js b/web/index.js new file mode 100644 index 0000000..ec0b146 --- /dev/null +++ b/web/index.js @@ -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) +} diff --git a/web/package-lock.json b/web/package-lock.json new file mode 100644 index 0000000..5bba0a6 --- /dev/null +++ b/web/package-lock.json @@ -0,0 +1,33 @@ +{ + "name": "@uscreentv/turbo-train", + "version": "0.0.1", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "@uscreentv/turbo-train", + "version": "0.0.1", + "license": "MIT", + "peerDependencies": { + "@hotwired/turbo": "^7.2.4" + } + }, + "node_modules/@hotwired/turbo": { + "version": "7.2.4", + "resolved": "https://registry.npmjs.org/@hotwired/turbo/-/turbo-7.2.4.tgz", + "integrity": "sha512-c3xlOroHp/cCZHDOuLp6uzQYEbvXBUVaal0puXoGJ9M8L/KHwZ3hQozD4dVeSN9msHWLxxtmPT1TlCN7gFhj4w==", + "peer": true, + "engines": { + "node": ">= 14" + } + } + }, + "dependencies": { + "@hotwired/turbo": { + "version": "7.2.4", + "resolved": "https://registry.npmjs.org/@hotwired/turbo/-/turbo-7.2.4.tgz", + "integrity": "sha512-c3xlOroHp/cCZHDOuLp6uzQYEbvXBUVaal0puXoGJ9M8L/KHwZ3hQozD4dVeSN9msHWLxxtmPT1TlCN7gFhj4w==", + "peer": true + } + } +} diff --git a/web/package.json b/web/package.json new file mode 100644 index 0000000..ab92e5e --- /dev/null +++ b/web/package.json @@ -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" + } +}