From 8bf900e975ede1553b3f577fc2d41915826a14b9 Mon Sep 17 00:00:00 2001 From: Slobodan Milicevic Date: Thu, 29 Aug 2019 22:04:59 +0200 Subject: [PATCH] Added support for Meteor 1.8.1 because of https://github.com/meteor/cordova-plugin-meteor-webapp/pull/62. This version of package requires you to call Reloader.configure in order to activate this package. --- .versions | 4 +-- README.md | 31 +++++++++++++--- package.js | 28 +++++++-------- reloader.js | 100 ++++++++++++++++++---------------------------------- 4 files changed, 77 insertions(+), 86 deletions(-) diff --git a/.versions b/.versions index acf4085..60189d9 100644 --- a/.versions +++ b/.versions @@ -28,10 +28,10 @@ html-tools@1.0.11 htmljs@1.0.11 http@1.3.0 id-map@1.0.9 -jamielob:reloader@1.3.0 +mslobodan:reloader@1.3.0 jquery@1.11.10 launch-screen@1.1.1 -local-test:jamielob:reloader@1.3.0 +local-test:mslobodan:reloader@1.3.0 logging@1.1.19 meteor@1.8.0 minimongo@1.4.0 diff --git a/README.md b/README.md index deb3438..fc44dc6 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,17 @@ +# About this fork + +This fork adds support for Meteor 1.8.1 because of: + + +Cordova Hot Code Push mechanism is now switching versions explicitly with call to WebAppLocalServer.switchToPendingVersion instead of trying to switch every time a browser reload is detected. If you use any third party package or have your own HCP routines implemented be sure to call it before forcing a browser reload. If you use the automatic reload from the Reload meteor package you do not need to do anything. + +[cordova-plugin-meteor-webapp PR #62](#https://github.com/meteor/cordova-plugin-meteor-webapp/pull/62) + + +### Usage + +This package won't be activated automatically like `jamielob/reloader`. You must call `Reloader.configure(options)` and pass desired options in order to activate it. + # Reloader More control over hot code push reloading for your mobile apps. A replacement for [`mdg:reload-on-resume`](https://github.com/meteor/mobile-packages/blob/master/packages/mdg:reload-on-resume/README.md) with more options and better UX. @@ -27,7 +41,7 @@ As of Meteor 1.3, if you prevent instant reloading on updates, the newest versio ### Installation ```sh -meteor add jamielob:reloader +meteor add mslobodan:reloader meteor remove mdg:reload-on-resume ``` @@ -35,7 +49,14 @@ If you have any calls to `location.reload()` or `location.replace(location.href) ## Configure -The default options are shown below. You can override them anywhere in your `client/` folder. +This version of reloader doesn't have default options. In order to activate this package you must call +```js +Reloader.configure(options) +``` +and pass desired options as soon as possible in code. + + +Preferred options are: ```js Reloader.configure({ @@ -46,7 +67,7 @@ Reloader.configure({ }); ``` -These default options will make sure that your app is up to date every time a user starts your app, or comes back to it after 10 minutes of being idle. +These options will make sure that your app is up to date every time a user starts your app, or comes back to it after 10 minutes of being idle. Another popular configuration is: @@ -171,7 +192,7 @@ This package also provides an easy reload event that you can attach to a button ### Run tests ```bash -git clone git@github.com:jamielob/reloader.git +git clone git@github.com:mslobodan/reloader.git cd reloader # uncomment the noted line in package.js meteor test-packages ./ --driver-package practicalmeteor:mocha @@ -180,6 +201,6 @@ open localhost:3000 ### Credits -[Contributors](https://github.com/jamielob/reloader/graphs/contributors) +[Contributors](https://github.com/mslobodan/reloader/graphs/contributors) And thanks to @martijnwalraven for his help with this packages and superb work on Meteor Cordova! 👏 diff --git a/package.js b/package.js index b41ba72..d44073b 100644 --- a/package.js +++ b/package.js @@ -1,8 +1,8 @@ Package.describe({ - name: 'jamielob:reloader', + name: 'mslobodan:reloader', version: '1.3.0', summary: 'More control over hot code push reloading', - git: 'https://github.com/jamielob/reloader/', + git: 'https://github.com/mslobodan/reloader/', documentation: 'README.md' }); @@ -10,17 +10,17 @@ Cordova.depends({ 'cordova-plugin-splashscreen': '4.1.0' }); -Package.onUse(function(api) { +Package.onUse(function (api) { api.versionsFrom('1.3.1'); api.use(['ecmascript', - 'check', - 'underscore', - 'reload', - 'templating', - 'reactive-var', - 'tracker', - 'launch-screen'], 'client'); + 'check', + 'underscore', + 'reload', + 'templating', + 'reactive-var', + 'tracker', + 'launch-screen'], 'client'); api.mainModule('reloader.js', 'web.cordova'); api.mainModule('browser.js', 'web.browser'); @@ -35,12 +35,12 @@ Npm.depends({ sinon: '1.17.3' }); -Package.onTest(function(api) { - api.use('jamielob:reloader', 'client') +Package.onTest(function (api) { + api.use('mslobodan:reloader', 'client'); api.use(['ecmascript', - 'underscore', - 'practicalmeteor:mocha'], 'client'); + 'underscore', + 'practicalmeteor:mocha'], 'client'); api.mainModule('reloader-tests.js', 'client'); }); diff --git a/reloader.js b/reloader.js index 926076f..75f43cd 100644 --- a/reloader.js +++ b/reloader.js @@ -10,10 +10,12 @@ Reloader = { checkTimer: Match.Optional(Match.Integer), refresh: Match.Optional(Match.OneOf('startAndResume', 'start', 'instantly')), idleCutoff: Match.Optional(Match.Integer), - launchScreenDelay: Match.Optional(Match.Integer), + launchScreenDelay: Match.Optional(Match.Integer) }); _.extend(this._options, options); + + this._onPageLoad(); }, updateAvailable: new ReactiveVar(false), @@ -27,18 +29,21 @@ Reloader = { }, reload() { - this.prereload() - - // We'd like to make the browser reload the page using location.replace() - // instead of location.reload(), because this avoids validating assets - // with the server if we still have a valid cached copy. This doesn't work - // when the location contains a hash however, because that wouldn't reload - // the page and just scroll to the hash location instead. - if (window.location.hash || window.location.href.endsWith("#")) { - window.location.reload(); - } else { - window.location.replace(window.location.href); - } + this.prereload(); + + cordova.exec(() => { + // We'd like to make the browser reload the page using location.replace() + // instead of location.reload(), because this avoids validating assets + // with the server if we still have a valid cached copy. This doesn't work + // when the location contains a hash however, because that wouldn't reload + // the page and just scroll to the hash location instead. + if (window.location.hash || window.location.href.endsWith('#')) { + window.location.reload(); + } else { + window.location.replace(window.location.href); + } + }, console.error, 'WebAppLocalServer', 'switchPendingVersion', []); + }, @@ -47,13 +52,13 @@ Reloader = { _shouldCheckForUpdateOnStart() { const isColdStart = !localStorage.getItem('reloaderWasRefreshed'); return isColdStart && - ( - this._options.check === 'everyStart' || ( - this._options.check === 'firstStart' && - !localStorage.getItem('reloaderLastStart') - ) - ); + this._options.check === 'everyStart' || + ( + this._options.check === 'firstStart' && + !localStorage.getItem('reloaderLastStart') + ) + ); }, // Check if the idleCutoff is set AND we exceeded the idleCutOff limit AND the everyStart check is set @@ -67,7 +72,7 @@ Reloader = { const lastPause = Number(localStorage.getItem('reloaderLastPause')); // Calculate the cutoff timestamp - const idleCutoffAt = Number( Date.now() - this._options.idleCutoff ); + const idleCutoffAt = Number(Date.now() - this._options.idleCutoff); return ( this._options.idleCutoff && @@ -79,12 +84,9 @@ Reloader = { _waitForUpdate(computation) { // Check if we have a HCP after the check timer is up Meteor.setTimeout(() => { - // If there is a new version available if (this.updateAvailable.get()) { - this.reload(); - } else { // Stop waiting for update @@ -94,15 +96,12 @@ Reloader = { launchScreen.release(); navigator.splashscreen.hide(); - } - - }, this._options.checkTimer ); + }, this._options.checkTimer); }, _checkForUpdate() { if (this.updateAvailable.get()) { - // Check for an even newer update this._waitForUpdate() @@ -113,10 +112,10 @@ Reloader = { if (this.updateAvailable.get()) { this.reload(); + return; } this._waitForUpdate(c) - }); } @@ -124,85 +123,56 @@ Reloader = { _onPageLoad() { if (this._shouldCheckForUpdateOnStart()) { - this._checkForUpdate(); } else { - - Meteor.setTimeout(function() { - + Meteor.setTimeout(function () { launchScreen.release(); - // Reset the reloaderWasRefreshed flag localStorage.removeItem('reloaderWasRefreshed'); - }, this._options.launchScreenDelay); // Short delay helps with white flash - } }, _onResume() { const shouldCheck = this._shouldCheckForUpdateOnResume(); - localStorage.removeItem('reloaderLastPause'); if (shouldCheck) { - navigator.splashscreen.show(); - this._checkForUpdate(); // If we don't need to do an additional check } else { - // Check if there's a new version available already AND we need to refresh on resume - if ( this.updateAvailable.get() && this._options.refresh === 'startAndResume' ) { - + if (this.updateAvailable.get() && this._options.refresh === 'startAndResume') { this.reload(); - } - } }, // https://github.com/meteor/meteor/blob/devel/packages/reload/reload.js#L104-L122 _onMigrate(retry) { if (this._options.refresh === 'instantly') { - - this.prereload() - + this.prereload(); return [true, {}]; } else { - // Set the flag this.updateAvailable.set(true); - // Don't refresh yet return [false]; - } } }; -// Set the defaults -Reloader.configure({ - check: 'everyStart', - checkTimer: 3000, - refresh: 'startAndResume', - idleCutoff: 1000 * 60 * 10, // 10 minutes - launchScreenDelay: 100 -}); - - -Reloader._onPageLoad(); // Set the last start flag localStorage.setItem('reloaderLastStart', Date.now()); // Watch for the app resuming -document.addEventListener("resume", function () { +document.addEventListener('resume', function () { Reloader._onResume(); }, false); @@ -210,24 +180,24 @@ document.addEventListener("resume", function () { localStorage.removeItem('reloaderLastPause'); // Watch for the device pausing -document.addEventListener("pause", function() { +document.addEventListener('pause', function () { // Save to localStorage localStorage.setItem('reloaderLastPause', Date.now()); }, false); // Capture the reload -Reload._onMigrate('jamielob:reloader', function (retry) { +Reload._onMigrate('mslobodan:reloader', function (retry) { return Reloader._onMigrate(retry); }); // Update available template helper -Template.registerHelper("updateAvailable", function() { +Template.registerHelper('updateAvailable', function () { return Reloader.updateAvailable.get(); }); // Update available event -$(document).on('click', '[reloader-update]', function(event) { +$(document).on('click', '[reloader-update]', function (event) { Reloader.reload(); });