-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc022bc
commit 9ba31f3
Showing
13 changed files
with
210 additions
and
140 deletions.
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 |
---|---|---|
@@ -1,31 +1,25 @@ | ||
const alertContainer = document.querySelector('#alert-container') | ||
const template = document.querySelector('#dinosaur-item-template') | ||
const dinosaurList = document.querySelector('#dinosaur-list') | ||
const dinosaurSection = document.querySelector('#dinosaur-section') | ||
const dinosaurId = dinosaurSection.dataset.id | ||
|
||
fetch(`/api/dinosaurs/${dinosaurId}`) | ||
fetch(window.location) | ||
.then(async response => { | ||
const bearer = response.headers.get('x-mercure-token') | ||
const topic = response.headers.get('x-mercure-topic') | ||
const hubUrl = response.headers.get('Link').match(/<([^>]+)>;\s+rel=(?:mercure|"[^"]*mercure[^"]*")/)[1] | ||
|
||
/* | ||
* If no JWT is provided, it means that the user won't | ||
* be able to subscribe to the updates. | ||
*/ | ||
if (!bearer) return | ||
const hub = new URL(hubUrl); | ||
|
||
const hubUrl = response.headers.get('Link').match(/<([^>]+)>;\s+rel=(?:mercure|"[^"]*mercure[^"]*")/)[1] | ||
hub.searchParams.append('topic', topic) | ||
|
||
const es = new EventSource(hub) | ||
|
||
const hub = new URL(hubUrl, window.origin); | ||
const body = await response.json() | ||
es.onmessage = e => { | ||
const item = document.createElement('div') | ||
|
||
hub.searchParams.append('topic', body.topic) | ||
item.setAttribute('class', 'alert alert-danger') | ||
item.setAttribute('role', 'alert') | ||
|
||
const es = new EventSourcePolyfill(hub, { | ||
headers: { | ||
'Authorization': bearer, | ||
} | ||
}) | ||
item.innerHTML = 'Dinosaur has changed !' | ||
|
||
es.onmessage = event => console.log(event) | ||
}); | ||
alertContainer.innerHTML = '' | ||
alertContainer.append(item) | ||
} | ||
}) |
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,35 @@ | ||
const alertContainer = document.querySelector('#alert-container') | ||
const template = document.querySelector('#dinosaur-item-template') | ||
const dinosaurList = document.querySelector('#dinosaur-list') | ||
|
||
fetch(window.location) | ||
.then(async response => { | ||
const topic = response.headers.get('x-mercure-topic') | ||
const hubUrl = response.headers.get('Link').match(/<([^>]+)>;\s+rel=(?:mercure|"[^"]*mercure[^"]*")/)[1] | ||
|
||
const hub = new URL(hubUrl); | ||
|
||
hub.searchParams.append('topic', topic) | ||
|
||
const es = new EventSource(hub) | ||
|
||
es.onmessage = e => { | ||
const dinosaur = JSON.parse(e.data) | ||
|
||
const clone = template.content.cloneNode(true) | ||
const dinosaurTemplateNameContainer = clone.querySelector('#dinosaur-item-template-name') | ||
const dinosaurTemplateLinkContainer = clone.querySelector('#dinosaur-item-template-link') | ||
|
||
dinosaurTemplateNameContainer.innerHTML = dinosaur.name | ||
dinosaurTemplateLinkContainer.href = dinosaur.link | ||
|
||
dinosaurList.append(clone) | ||
|
||
alertContainer.innerHTML =`<div class='alert alert-success'>Welcome to ${dinosaur.name}!</div>` | ||
|
||
window.setTimeout(() => { | ||
const alert = document.querySelector('.alert') | ||
alert.parentNode.removeChild(alert) | ||
}, 5000); | ||
} | ||
}) |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.