Refactor "reload" package: camelCase the API, and make the onMigrate name

optional.

Motivation: now that we don't use "url" as a key in _LivedataConnection, it's
easier to turn it into a function (for MeteorWildcard support).
This commit is contained in:
David Glasser
2012-09-28 00:19:54 -07:00
parent bee8bcf482
commit ac9f553d07
3 changed files with 24 additions and 19 deletions

View File

@@ -40,7 +40,7 @@ Meteor._LivedataConnection = function (url, restart_on_update) {
// name -> updates for (yet to be created) collection // name -> updates for (yet to be created) collection
self.queued = {}; self.queued = {};
// if we're blocking a migration, the retry func // if we're blocking a migration, the retry func
self.retry_migrate = null; self._retryMigrate = null;
// metadata for subscriptions // metadata for subscriptions
self.subs = new LocalCollection; self.subs = new LocalCollection;
@@ -53,12 +53,11 @@ Meteor._LivedataConnection = function (url, restart_on_update) {
// Setup auto-reload persistence. // Setup auto-reload persistence.
var reload_key = "Server-" + url; Meteor._reload.onMigrate(function (retry) {
Meteor._reload.on_migrate(reload_key, function (retry) {
if (!self._readyToMigrate()) { if (!self._readyToMigrate()) {
if (self.retry_migrate) if (self._retryMigrate)
throw new Error("Two migrations in progress?"); throw new Error("Two migrations in progress?");
self.retry_migrate = retry; self._retryMigrate = retry;
return false; return false;
} }
@@ -542,9 +541,9 @@ _.extend(Meteor._LivedataConnection.prototype, {
// if we were blocking a migration, see if it's now possible to // if we were blocking a migration, see if it's now possible to
// continue // continue
if (self.retry_migrate && self._readyToMigrate()) { if (self._retryMigrate && self._readyToMigrate()) {
self.retry_migrate(); self._retryMigrate();
self.retry_migrate = null; self._retryMigrate = null;
} }
}, },

View File

@@ -83,14 +83,20 @@
// function. The retry function will return immediately, but will // function. The retry function will return immediately, but will
// schedule the migration to be retried, meaning that every package // schedule the migration to be retried, meaning that every package
// will be polled once again for its migration data. If they are all // will be polled once again for its migration data. If they are all
// ready this time, then the migration will happen. // ready this time, then the migration will happen. name must be set if there
Meteor._reload.on_migrate = function (name, callback) { // is migration data.
Meteor._reload.onMigrate = function (name, callback) {
if (!callback) {
// name not provided, so first arg is callback.
callback = name;
name = undefined;
}
providers.push({name: name, callback: callback}); providers.push({name: name, callback: callback});
}; };
// Called by packages when they start up. // Called by packages when they start up.
// Returns the object that was saved, or undefined if none saved. // Returns the object that was saved, or undefined if none saved.
Meteor._reload.migration_data = function (name) { Meteor._reload.migrationData = function (name) {
return old_data[name]; return old_data[name];
}; };
@@ -108,7 +114,7 @@
var tryReload = function () { _.defer(function () { var tryReload = function () { _.defer(function () {
// Make sure each package is ready to go, and collect their // Make sure each package is ready to go, and collect their
// migration data // migration data
var migration_data = {}; var migrationData = {};
var remaining = _.clone(providers); var remaining = _.clone(providers);
while (remaining.length) { while (remaining.length) {
var p = remaining.shift(); var p = remaining.shift();
@@ -116,16 +122,16 @@
if (!status[0]) if (!status[0])
return; // not ready yet.. return; // not ready yet..
if (status.length > 1) if (status.length > 1)
migration_data[p.name] = status[1]; migrationData[p.name] = status[1];
}; };
try { try {
// Persist the migration data // Persist the migration data
var json = JSON.stringify({ var json = JSON.stringify({
time: (new Date()).getTime(), data: migration_data, reload: true time: (new Date()).getTime(), data: migrationData, reload: true
}); });
} catch (err) { } catch (err) {
Meteor._debug("Couldn't serialize data for migration", migration_data); Meteor._debug("Couldn't serialize data for migration", migrationData);
throw err; throw err;
} }

View File

@@ -111,15 +111,15 @@ Session = _.extend({}, {
if (Meteor._reload) { if (Meteor._reload) {
Meteor._reload.on_migrate('session', function () { Meteor._reload.onMigrate('session', function () {
// XXX sanitize and make sure it's JSONible? // XXX sanitize and make sure it's JSONible?
return [true, {keys: Session.keys}]; return [true, {keys: Session.keys}];
}); });
(function () { (function () {
var migration_data = Meteor._reload.migration_data('session'); var migrationData = Meteor._reload.migrationData('session');
if (migration_data && migration_data.keys) { if (migrationData && migrationData.keys) {
Session.keys = migration_data.keys; Session.keys = migrationData.keys;
} }
})(); })();
} }