From 4bec4877e37ea5548c72636825b0579d404ccecb Mon Sep 17 00:00:00 2001 From: David Glasser Date: Wed, 18 Jun 2014 17:02:03 -0700 Subject: [PATCH] after startup, Meteor.startup(c) should call c now This was the client behavior and is now the server behavior as well. Fixes #2239. --- packages/meteor/helpers_test.js | 9 +++++++++ packages/meteor/startup_server.js | 7 ++++++- tools/server/boot.js | 12 +++++++++--- tools/unipackage.js | 9 +++++++-- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/packages/meteor/helpers_test.js b/packages/meteor/helpers_test.js index ec5afd3859..14d083f085 100644 --- a/packages/meteor/helpers_test.js +++ b/packages/meteor/helpers_test.js @@ -75,3 +75,12 @@ Tinytest.add("environment - helpers", function (test) { Meteor._delete(x, "a"); test.equal(x, {}); }); + +Tinytest.add("environment - startup", function (test) { + // After startup, Meteor.startup should call the callback immediately. + var called = false; + Meteor.startup(function () { + called = true; + }); + test.isTrue(called); +}); diff --git a/packages/meteor/startup_server.js b/packages/meteor/startup_server.js index af0ca8126c..2a08396ac1 100644 --- a/packages/meteor/startup_server.js +++ b/packages/meteor/startup_server.js @@ -1,3 +1,8 @@ Meteor.startup = function (callback) { - __meteor_bootstrap__.startup_hooks.push(callback); + if (__meteor_bootstrap__.startupHooks) { + __meteor_bootstrap__.startupHooks.push(callback); + } else { + // We already started up. Just call it now. + callback(); + } }; diff --git a/tools/server/boot.js b/tools/server/boot.js index ecba19b232..ac5d08b92f 100644 --- a/tools/server/boot.js +++ b/tools/server/boot.js @@ -23,7 +23,7 @@ var configJson = // Set up environment __meteor_bootstrap__ = { - startup_hooks: [], + startupHooks: [], serverDir: serverDir, configJson: configJson }; __meteor_runtime_config__ = { meteorRelease: configJson.meteorRelease }; @@ -161,8 +161,14 @@ Fiber(function () { func.call(global, Npm, Assets); // Coffeescript }); - // run the user startup hooks. - _.each(__meteor_bootstrap__.startup_hooks, function (x) { x(); }); + // run the user startup hooks. other calls to startup() during this can still + // add hooks to the end. + while (__meteor_bootstrap__.startupHooks.length) { + var hook = __meteor_bootstrap__.startupHooks.shift(); + hook(); + } + // Setting this to null tells Meteor.startup to call hooks immediately. + __meteor_bootstrap__.startupHooks = null; // find and run main() // XXX hack. we should know the package that contains main. diff --git a/tools/unipackage.js b/tools/unipackage.js index 53da2f00ad..b4854329f7 100644 --- a/tools/unipackage.js +++ b/tools/unipackage.js @@ -66,7 +66,7 @@ var load = function (options) { // will get refactored before too long. Note that // __meteor_bootstrap__.require is no longer provided. var env = { - __meteor_bootstrap__: { startup_hooks: [] }, + __meteor_bootstrap__: { startupHooks: [] }, __meteor_runtime_config__: { meteorRelease: options.release } }; @@ -83,7 +83,12 @@ var load = function (options) { ret = image.load(env); // Run any user startup hooks. - _.each(env.__meteor_bootstrap__.startup_hooks, function (x) { x(); }); + while (env.__meteor_bootstrap__.startupHooks.length) { + var hook = env.__meteor_bootstrap__.startupHooks.shift(); + hook(); + } + // Setting this to null tells Meteor.startup to call hooks immediately. + env.__meteor_bootstrap__.startupHooks = null; }); if (messages.hasMessages()) {