some stuff

This commit is contained in:
martin 2018-05-25 15:21:42 +01:00
parent d8d66f4ec8
commit b19a1217ff
8 changed files with 143 additions and 97 deletions

View File

@ -35,7 +35,8 @@ gulp.task('bundleBackbone', function () {
gulp.task('bump', function() { gulp.task('bump', function() {
gulp.src('src/service-worker.js') gulp.src('src/service-worker.js')
.pipe(bump({ 'key': 'version' })) .pipe(bump({ 'key': 'version' }))
.pipe(gulp.dest('src')); .pipe(gulp.dest('src'))
.pipe(gulp.dest('live'));
}); });
gulp.task('buildBackbone', ['bump', 'bundleBackbone'], function() { gulp.task('buildBackbone', ['bump', 'bundleBackbone'], function() {

8
package-lock.json generated
View File

@ -52,7 +52,7 @@
}, },
"@sinonjs/formatio": { "@sinonjs/formatio": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/@sinonjs/formatio/-/formatio-2.0.0.tgz", "resolved": "http://registry.npmjs.org/@sinonjs/formatio/-/formatio-2.0.0.tgz",
"integrity": "sha512-ls6CAMA6/5gG+O/IdsBcblvnd8qcO/l1TYoNeAzp3wcISOxlPXQEus0mLcdwazEkWjaBdaJ3TaxmNgCLWwvWzg==", "integrity": "sha512-ls6CAMA6/5gG+O/IdsBcblvnd8qcO/l1TYoNeAzp3wcISOxlPXQEus0mLcdwazEkWjaBdaJ3TaxmNgCLWwvWzg==",
"dev": true, "dev": true,
"requires": { "requires": {
@ -11959,9 +11959,9 @@
} }
}, },
"whatwg-fetch": { "whatwg-fetch": {
"version": "2.0.3", "version": "2.0.4",
"resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-2.0.3.tgz", "resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-2.0.4.tgz",
"integrity": "sha1-nITsLc9oGH/wC8ZOEnS0QhduHIQ=" "integrity": "sha512-dcQ1GWpOD/eEQ97k66aiEVpNnapVj90/+R+SXTPYGHpYBBypfKJEQjLrvMZ7YXbKm21gXd4NcuxUTjiv1YtLng=="
}, },
"when": { "when": {
"version": "3.7.8", "version": "3.7.8",

View File

@ -74,6 +74,6 @@
"vinyl-buffer": "^1.0.1", "vinyl-buffer": "^1.0.1",
"vinyl-source-stream": "^2.0.0", "vinyl-source-stream": "^2.0.0",
"watchify": "^3.11.0", "watchify": "^3.11.0",
"whatwg-fetch": "^2.0.3" "whatwg-fetch": "^2.0.4"
} }
} }

19
serverHTTP2.js Normal file
View File

@ -0,0 +1,19 @@
const http2 = require('http2');
const fs = require('fs');
const server = http2.createSecureServer({
key: fs.readFileSync('localhost-privkey.pem'),
cert: fs.readFileSync('localhost-cert.pem')
});
server.on('error', (err) => console.error(err));
server.on('stream', (stream, headers) => {
// stream is a Duplex
stream.respond({
'content-type': 'text/html',
':status': 200
});
stream.end('<h1>Hello World</h1>');
});
server.listen(8110);

View File

@ -11,10 +11,11 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
const CACHE_VERSION = { 'version': '0.0.707' }; const CACHE_VERSION = { 'version': '0.0.771' };
const dataCacheName = 'jubileeData-v1'; const PRECACHE = `jubileeData-${CACHE_VERSION.version}`;
const cacheName = 'jubilee-final-1'; const RUNTIME = 'runtime';
const filesToCache = [
const PRECACHE_URLS = [
'/', '/',
'/index.html', '/index.html',
'/service-worker.js', '/service-worker.js',
@ -66,77 +67,76 @@ const filesToCache = [
]; ];
self.addEventListener('install', function(e) { const liveData = ['news', 'agenda'];
console.log('[ServiceWorker] Install');
e.waitUntil(
caches.open(cacheName).then(function(cache) {
console.log('[ServiceWorker] Caching app shell');
return cache.addAll(filesToCache); self.addEventListener('install', event => {
}) console.warn('Installing...');
event.waitUntil(
caches.open(PRECACHE)
.then(cache => cache.addAll(PRECACHE_URLS))
.then(self.skipWaiting())
); );
}); });
self.addEventListener('activate', function(e) { // The activate handler takes care of cleaning up old caches.
console.log('[ServiceWorker] Activate'); self.addEventListener('activate', event => {
e.waitUntil( console.warn('Activate...');
caches.keys().then(function(keyList) { const currentCaches = [PRECACHE, RUNTIME];
return Promise.all(keyList.map(function(key) { event.waitUntil(
if (key !== cacheName && key !== dataCacheName) { caches.keys().then(cacheNames => {
console.log('[ServiceWorker] Removing old cache', key); return cacheNames.filter(cacheName => !currentCaches.includes(cacheName));
}).then(cachesToDelete => {
return caches.delete(key); return Promise.all(cachesToDelete.map(cacheToDelete => {
} return caches.delete(cacheToDelete);
})); }));
}) }).then(() => self.clients.claim())
); );
/*
* Fixes a corner case in which the app wasn't returning the latest data.
* You can reproduce the corner case by commenting out the line below and
* then doing the following steps: 1) load app for first time so that the
* initial New York City data is shown 2) press the refresh button on the
* app 3) go offline 4) reload the app. You expect to see the newer NYC
* data, but you actually see the initial data. This happens because the
* service worker is not yet activated. The code below essentially lets
* you activate the service worker faster.
*/
return self.clients.claim();
}); });
self.addEventListener('fetch', function(e) { // The fetch handler serves responses for same-origin resources from a cache.
console.warn('[Service Worker] Fetch', e.request.url); // If no response is found, it populates the runtime cache with the response
const dataUrl = '/getnexttraintimes?'; // from the network before returning it to the page.
if (e.request.url.indexOf(dataUrl) > -1) { self.addEventListener('fetch', event => {
console.log('!'); console.warn('Fetch', event.request.url);
// Skip cross-origin requests, like those for Google Analytics.
if (event.request.url.startsWith(self.location.origin)) {
console.log('One of our requests..');
event.respondWith(
caches.match(event.request).then(cachedResponse => {
if (cachedResponse) {
return cachedResponse;
}
/* return caches.open(RUNTIME).then(cache => {
* When the request URL contains dataUrl, the app is asking for fresh return fetch(event.request).then(response => {
* weather data. In this case, the service worker always goes to the // Put a copy of the response in the runtime cache.
* network and then caches the response. This is called the "Cache then return cache.put(event.request, response.clone()).then(() => {
* network" strategy: return response;
* https://jakearchibald.com/2014/offline-cookbook/#cache-then-network });
*/ });
e.respondWith(
caches.open(dataCacheName).then(function(cache) {
return fetch(e.request).then(function(response) {
cache.put(e.request.url, response.clone());
return response;
}); });
}) })
); );
} }
else
/* if (event.request.url.startsWith('https://maps.googleapis.com')) {
* The app is asking for app shell files. In this scenario the app uses the const url = new URL(event.request.url);
* "Cache, falling back to the network" offline strategy: const locCache = `loc-${url.searchParams.get('latlng')}`;
* https://jakearchibald.com/2014/offline-cookbook/#cache-falling-back-to-network event.respondWith(
*/ caches.match(locCache).then(cachedResponse => {
e.respondWith( if (cachedResponse) {
caches.match(e.request).then(function(response) { return cachedResponse;
return response || fetch(e.request); }
return caches.open(RUNTIME).then(cache => {
return fetch(event.request).then(response => {
// Put a copy of the response in the runtime cache.
return cache.put(locCache, response.clone()).then(() => {
return response;
});
});
});
}) })
); );
}
}); });

View File

@ -24,6 +24,7 @@ const NearbyModel = Backbone.Model.extend({
this.tick(); this.tick();
this.set('totalResults', 0); this.set('totalResults', 0);
this.listenTo(this, 'change:llFixed change:atHome change:section change:update', this.onChange); this.listenTo(this, 'change:llFixed change:atHome change:section change:update', this.onChange);
this.throttledGetNearby = _.throttle(this.getNearby, 6000);
}, },
'tick': function() { 'tick': function() {
const hour = parseInt((new Date()).getHours().toString(), 10); const hour = parseInt((new Date()).getHours().toString(), 10);
@ -41,8 +42,8 @@ const NearbyModel = Backbone.Model.extend({
console.log('At home, so no fetching...'); console.log('At home, so no fetching...');
this.logUpdate(); this.logUpdate();
} }
else else
this.getNearby(); this.throttledGetNearby();
}, },
'getNearby': function() { 'getNearby': function() {
const llFixed = this.get('llFixed'); const llFixed = this.get('llFixed');
@ -54,22 +55,24 @@ const NearbyModel = Backbone.Model.extend({
const lastUpdate = time - (this.get('time') || 0); const lastUpdate = time - (this.get('time') || 0);
console.log('>> Nearby section:', hour, section); console.log('>> Nearby section:', hour, section);
console.info('>> Nearby:request'); console.info('>> Nearby:request V2');
console.log(`>> Nearby last fetch: ${TimeFormat.fromMs(lastUpdate, 'hh:mm')} ago`); console.log(`>> Nearby last fetch: ${TimeFormat.fromMs(lastUpdate, 'hh:mm')} ago`);
if (lastUpdate > 120000) if (lastUpdate > 120000) {
request({ const url = new URL(`${window.loc}/fsexplore`);
'url': `${window.loc}/fsexplore`,
'method': 'GET', url.searchParams.append('ll', llFixed);
'qs': { url.searchParams.append('section', section);
'll': llFixed,
'section': section fetch(url)
} .then(res => {
}, function(err, res, body) {
if (err)
console.error(err);
else {
console.log('statusCode', res.statusCode); console.log('statusCode', res.statusCode);
console.warn(res.status);
return res.text();
})
.then(function(body) {
const fsJSON = JSON.parse(body); const fsJSON = JSON.parse(body);
const groups = get(fsJSON, 'response.groups'); const groups = get(fsJSON, 'response.groups');
const items = groups[0].items; const items = groups[0].items;
@ -80,8 +83,11 @@ const NearbyModel = Backbone.Model.extend({
this.fsCollection.reset(newItems); this.fsCollection.reset(newItems);
this.logUpdate(); this.logUpdate();
} }.bind(this))
}.bind(this)); .catch(function(error) {
console.error(error);
});
}
}, 'logUpdate': function() { }, 'logUpdate': function() {
console.log('Nearby logging:'); console.log('Nearby logging:');
@ -184,7 +190,7 @@ const NearbyView = Backbone.View.extend({
const section = this.model.get('section'); const section = this.model.get('section');
const data = { llFixed, section, 'limit':20 }; const data = { llFixed, section, 'limit':20 };
// console.log('Data', data); // console.log('Data', data);
this.eventBus.trigger('showNearbyList', data); this.eventBus.trigger('showNearbyList', data);
} }

View File

@ -55,7 +55,7 @@ const TrafficModel = Backbone.Model.extend({
'getTraffic': function() { 'getTraffic': function() {
console.log('Get Traffic'); console.log('Get Traffic');
// olat, olon, dlat, dlon // olat, olon, dlat, dlon
const override = true; const override = false;
const time = new Date().getTime() ; const time = new Date().getTime() ;
const hour = (new Date()).getHours(); const hour = (new Date()).getHours();
const latlong = this.get('latlong'); const latlong = this.get('latlong');

View File

@ -21,23 +21,43 @@ const { NearbyListModel, NearbyListView } = require('./NearbyList');
const { NearbyPlacesView } = require('./NearbyPlaces'); const { NearbyPlacesView } = require('./NearbyPlaces');
var app = app || {}; var app = app || {};
const live = false; const live = true;
if (live) { if (live) {
window.loc = 'https://jubilee.silvrtree.co.uk'; window.loc = 'https://jubilee.silvrtree.co.uk';
if ('serviceWorker' in navigator)
navigator.serviceWorker
.register('./service-worker.js')
.then(function() {
console.log('Service Worker Registered');
});
}
else
window.loc = 'http://localhost:8110'; window.loc = 'http://localhost:8110';
if ('serviceWorker' in navigator) {
//
navigator.serviceWorker.ready.then(function(reg) {
console.warn('Ready??', reg);
main();
});
(function () { window.addEventListener('load', function() {
navigator.serviceWorker
.register('./service-worker.js')
.then(function(r) {
console.warn('Service Worker Registered', r.scope);
})
.catch(function(error) {
// registration failed
console.error(`Registration failed with ${ error}`);
});
});
//
}
}
else {
window.loc = 'http://localhost:8110';
main();
}
function main() {
const offline = false; const offline = false;
console.log('>>> START!!! <<<');
/* var myRouter = Backbone.Router.extend({ /* var myRouter = Backbone.Router.extend({
// //
@ -102,4 +122,4 @@ else
window.addEventListener('online', app.updateOnlineStatus); window.addEventListener('online', app.updateOnlineStatus);
window.addEventListener('offline', app.updateOnlineStatus); window.addEventListener('offline', app.updateOnlineStatus);
})(); }