Skip to content

Commit

Permalink
Added support for Meteor 1.8.1 because of meteor/cordova-plugin-meteo…
Browse files Browse the repository at this point in the history
…r-webapp#62.

This version of package requires you to call Reloader.configure in order to activate this package.
  • Loading branch information
mslobodan committed Aug 29, 2019
1 parent 6380bd5 commit 8bf900e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 86 deletions.
4 changes: 2 additions & 2 deletions .versions
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 26 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# About this fork

This fork adds support for Meteor 1.8.1 because of:

<i>
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)
</i>

### Usage

<b>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.</b>

# 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.
Expand Down Expand Up @@ -27,15 +41,22 @@ 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
```

If you have any calls to `location.reload()` or `location.replace(location.href)` in your app, replace them with `Reloader.reload()`.

## 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({
Expand All @@ -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:

Expand Down Expand Up @@ -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
Expand All @@ -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! 👏
28 changes: 14 additions & 14 deletions package.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
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'
});

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');
Expand All @@ -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');
});
100 changes: 35 additions & 65 deletions reloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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', []);

},


Expand All @@ -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
Expand All @@ -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 &&
Expand All @@ -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
Expand All @@ -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()

Expand All @@ -113,121 +112,92 @@ Reloader = {

if (this.updateAvailable.get()) {
this.reload();
return;
}

this._waitForUpdate(c)

});

}
},

_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);


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();
});

0 comments on commit 8bf900e

Please sign in to comment.