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

View File

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

View File

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