Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show a notice when an update is available #17

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ img.shadow{
h1 img{
vertical-align: middle;
}

#updater{
background-color: khaki;
border: 1px solid gray;
}
/* for Alpine.js */
[x-cloak] {
display: none !important;
}

13 changes: 11 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
<script type="text/javascript" src="js/webmidi.iife.min.js"></script>
<script type="text/javascript" src="js/app.js"></script>

<script>navigator.serviceWorker.register("service-worker.js")</script>

</head>
<body>
<div class="container text-center" x-data="appData()" x-init="appInit()">
Expand All @@ -39,6 +37,17 @@ <h1>
<img src="images/192x192.png" alt="Push Push icon" height="80px" width="80px"/>
Push Push Editor
</h1>
<div id="updater" x-show="$store.pp.show_update" >
There is a new version of the editor available. Load it now?
<div class="row">
<!-- <div class="six columns"> -->
<button class="button-primary" @click.prevent="postSkipWaiting()" >Load new version</button>
<!-- </div> -->
<!-- <div class="six columns"> -->
<button class="" @click.prevent="$store.pp.show_update = false">Load later</button>
<!-- </div> -->
</div>
</div>
[<a href="" x-data @click.prevent="$store.pp.show_page == 'help'? $store.pp.show_page = 'editor' : $store.pp.show_page = 'help' " x-text="$store.pp.show_page == 'help'?'Go to editor' : 'Show user guide'" ></a>]
</div>
<hr>
Expand Down
40 changes: 38 additions & 2 deletions docs/js/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var editor_version = '1.2.1';
var editor_version = '1.3.0';

var PP_MIDI_MANUF_ID_1 = 0x37;
var PP_MIDI_MANUF_ID_2 = 0x72;
Expand All @@ -16,6 +16,41 @@ var X_ERROR = 0x7F; // Something went wrong
var X_OK = 0x01;
var X_FAILED = 0x7F;

let newWorker;
// navigator.serviceWorker.register("service-worker.js")
navigator.serviceWorker.register('service-worker.js').then(reg => {
reg.addEventListener('updatefound', () => {
// An updated service worker has appeared in reg.installing!
newWorker = reg.installing;
newWorker.addEventListener('statechange', () => {
// Has service worker state changed?
switch (newWorker.state) {
case 'installed':
// There is a new service worker available, show the notification
if (navigator.serviceWorker.controller) {
showUpdateNotification();
}
break;
case 'activated':
// The new Service Worker was installed and activated. Hide the notification
hideUpdateNotification()
break;
}
});
});
});

function showUpdateNotification(){
Alpine.store('pp').show_update = true;
}
function hideUpdateNotification(){
Alpine.store('pp').show_update = false;
location.reload()
}
function postSkipWaiting(){
newWorker.postMessage( 'SKIP_WAITING' );
}

document.addEventListener('alpine:init', () => {
Alpine.store('pp', {
editor_version: editor_version,
Expand All @@ -33,7 +68,8 @@ document.addEventListener('alpine:init', () => {
pp_stored: false,
pp_errors: false,
pp_import_error: false,
show_page: 'editor',
show_page: 'editor',
show_update: false
});


Expand Down
12 changes: 7 additions & 5 deletions docs/service-worker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const VERSION = "1.2.1";
const CACHE_NAME = `PushPushEditor-${VERSION}`;
const BUILD = "202402141747";

const CACHE_NAME = `PushPushEditor-${BUILD}`;
const PRE_CACHED_RESOURCES = [
"index.html",
"manifest.json",
Expand Down Expand Up @@ -30,7 +31,7 @@ const expectedCaches = [CACHE_NAME]
// Add a cache-busting query string to the pre-cached resources.
// This is to avoid loading these resources from the disk cache.
const PRE_CACHED_RESOURCES_WITH_VERSIONS = PRE_CACHED_RESOURCES.map(path => {
return `${path}?v=${VERSION}`;
return `${path}?v=${BUILD}`;
});

// Use the install event to pre-cache all initial resources.
Expand Down Expand Up @@ -79,7 +80,7 @@ self.addEventListener('fetch', event => {
const cache = await caches.open(CACHE_NAME);

// Get the resource from the cache.
const versionedUrl = `${event.request.url}?v=${VERSION}`;
const versionedUrl = `${event.request.url}?v=${BUILD}`;
const cachedResponse = await cache.match(versionedUrl);

if (cachedResponse) {
Expand All @@ -99,9 +100,10 @@ self.addEventListener('fetch', event => {
})());
});


/* force update Service Worker */
self.addEventListener('message', (event) => {
if (event.data === 'SKIP_WAITING') {
console.log('received PUSH SKIP_WAITING');
self.skipWaiting();
}
});