From 5244fb7273b55ebe3312b0f9e47c21a92d82e634 Mon Sep 17 00:00:00 2001 From: Matt Copperwaite Date: Fri, 17 Nov 2023 15:03:13 +0000 Subject: [PATCH] adds ability to detect new releases --- src/js/static.js | 12 ++++++++++++ src/js/updater.js | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 src/js/updater.js diff --git a/src/js/static.js b/src/js/static.js index 7d3e510..c4d9cda 100644 --- a/src/js/static.js +++ b/src/js/static.js @@ -4,6 +4,7 @@ import Shortcuts from "./shortcuts.js"; import Info from "./info.js"; import Media from "./media.js"; import Gestures from "./gestures.js"; +import Updater from "./updater.js"; export default class StaticPlayer { @@ -22,6 +23,8 @@ export default class StaticPlayer { GESTURES = Gestures; + UPDATER = Updater; + #background_video; #background; #player_el; @@ -31,6 +34,7 @@ export default class StaticPlayer { #info; #media; #gestures; + #updater; get background_video_el (){ @@ -96,11 +100,19 @@ export default class StaticPlayer { return this.#gestures; } + get updater () { + if(!this.#updater) { + this.#updater = new this.UPDATER(this); + } + return this.#updater; + } + run() { this.shortcuts.setup(); this.player.start(); this.background.start(); this.media.start(); this.gestures.start(); + this.updater.start(); } } diff --git a/src/js/updater.js b/src/js/updater.js new file mode 100644 index 0000000..ef15acd --- /dev/null +++ b/src/js/updater.js @@ -0,0 +1,50 @@ +export default class Updater { + RELEASE_URL = "release" + RELESE_CHECK_SECONDS = 60 // low for testing + + constructor (parent) { + this.parent = parent; + + this.this_release = null; + this.check_loop = null; + } + + get_release(url, callback) { + return fetch(url) + .then(function(response) { + if (!response.ok) { + throw new Error("HTTP error, status = " + response.status); + } + return response.text(); + }) + .then(callback.bind(this)) + } + + update_release(release) { + this.release = release; + } + + start_check_loop() { + const delay = this.RELESE_CHECK_SECONDS * 1000 // in milliseconds + + this.check_loop = setInterval(this.check_release.bind(this), delay); + } + + check_release() { + let that = this; + this.get_release(this.RELEASE_URL, (latest_release) => { + if (latest_release != that.release) { + that.refresh_page() + } + }) + } + + refresh_page() { + window.location.reload(); + } + + start() { + this.get_release(this.RELEASE_URL, this.update_release) + .then(this.start_check_loop.bind(this)); + } +}