From e2bd2a3d420841803b972035dedbc86d28a84f0a Mon Sep 17 00:00:00 2001 From: Slava Kim Date: Thu, 24 Jul 2014 17:41:20 -0700 Subject: [PATCH] wip on custom autoupdate for cordova --- packages/autoupdate/autoupdate_cordova.js | 64 +++++++++++++++++++++++ packages/autoupdate/autoupdate_server.js | 4 +- packages/autoupdate/package.js | 6 +++ 3 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 packages/autoupdate/autoupdate_cordova.js diff --git a/packages/autoupdate/autoupdate_cordova.js b/packages/autoupdate/autoupdate_cordova.js new file mode 100644 index 0000000000..43a43fa3c0 --- /dev/null +++ b/packages/autoupdate/autoupdate_cordova.js @@ -0,0 +1,64 @@ +var autoupdateVersion = __meteor_runtime_config__.autoupdateVersion || "unknown"; +var autoupdateVersionRefreshable = + __meteor_runtime_config__.autoupdateVersionRefreshable || "unknown"; + +// The collection of acceptable client versions. +ClientVersions = new Meteor.Collection("meteor_autoupdate_clientVersions"); + +Autoupdate = {}; + +Autoupdate.newClientAvailable = function () { + return !! ClientVersions.findOne( + {$and: [ + {current: true}, + {_id: {$ne: autoupdateVersion}} + ]} + ); +}; + +var retry = new Retry({ + minCount: 0, // don't do any immediate retries + baseTimeout: 30*1000 // start with 30s +}); +var failures = 0; + +Autoupdate._retrySubscription = function () { + Meteor.subscribe("meteor_autoupdate_clientVersions", { + onError: function (error) { + Meteor._debug("autoupdate subscription failed:", error); + failures++; + retry.retryLater(failures, function () { + Autoupdate._retrySubscription(); + }); + }, + onReady: function () { + if (Package.reload) { + var handle = ClientVersions.find().observeChanges({ + added: function (id, fields) { + var self = this; + if (fields.refreshable && id !== autoupdateVersionRefreshable) { + autoupdateVersionRefreshable = id; + onNewVersion(); + } else if (! fields.refreshable && id !== autoupdateVersion) { + autoupdateVersion = id; + onNewVersion(); + } + } + }); + + function onNewVersion () { + if (handle) { + handle.stop(); + } + + HTTP.get(Meteor.absoluteUrl() + 'cordova_manifest.json', function (err, res) { + console.log(res); + //Package.reload.Reload._reload(); + }); + } + } + } + }); +}; +Autoupdate._retrySubscription(); + diff --git a/packages/autoupdate/autoupdate_server.js b/packages/autoupdate/autoupdate_server.js index 5a900138a8..ac40dda3a1 100644 --- a/packages/autoupdate/autoupdate_server.js +++ b/packages/autoupdate/autoupdate_server.js @@ -64,7 +64,7 @@ var updateVersions = function (shouldReloadClientProgram) { // Step 1: load the current client program on the server and update the // hash values in __meteor_runtime_config__. if (shouldReloadClientProgram) { - WebAppInternals.reloadClientProgram(); + WebAppInternals.reloadClientPrograms(); } if (startupVersion === null) { @@ -132,4 +132,4 @@ Meteor.publish( // Listen for SIGUSR2, which signals that a client asset has changed. process.on('SIGUSR2', Meteor.bindEnvironment(function () { updateVersions(true); -})); \ No newline at end of file +})); diff --git a/packages/autoupdate/package.js b/packages/autoupdate/package.js index 77c355456b..d7f9f490b6 100644 --- a/packages/autoupdate/package.js +++ b/packages/autoupdate/package.js @@ -3,14 +3,20 @@ Package.describe({ version: '1.0.0' }); +Cordova.depends({ + 'org.apache.cordova.file': '1.2.0' +}); + Package.on_use(function (api) { api.use('webapp', 'server'); api.use(['deps', 'retry'], 'client'); api.use(['livedata', 'mongo-livedata'], ['client', 'server']); api.use('deps', 'client'); api.use('reload', 'client', {weak: true}); + api.use('http', 'client.cordova'); api.export('Autoupdate'); api.add_files('autoupdate_server.js', 'server'); api.add_files('autoupdate_client.js', 'client.browser'); + api.add_files('autoupdate_cordova.js', 'client.cordova'); });