From 3d8d9bac8ecde27841f91c34cd759297c0a45790 Mon Sep 17 00:00:00 2001 From: David Baker Date: Tue, 20 Oct 2015 11:02:54 +0100 Subject: [PATCH] Allow the dispatcher to dispatch sync if required. --- src/dispatcher.js | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/dispatcher.js b/src/dispatcher.js index 4b856bf310..a989351a5e 100644 --- a/src/dispatcher.js +++ b/src/dispatcher.js @@ -19,12 +19,25 @@ limitations under the License. var flux = require("flux"); class MatrixDispatcher extends flux.Dispatcher { - dispatch(payload) { - // We always set a timeout to do this: The flux dispatcher complains - // if you dispatch from within a dispatch, so rather than action - // handlers having to worry about not calling anything that might - // then dispatch, we just do dispatches asynchronously. - setTimeout(super.dispatch.bind(this, payload), 0); + /** + * @param {Object} payload Required. The payload to dispatch. + * Must contain at least an 'action' key. + * @param {boolean} sync Optional. Pass true to dispatch + * synchronously. This is useful for anything triggering + * an operation that the browser requires user interaction + * for. + */ + dispatch(payload, sync) { + if (sync) { + super.dispatch(payload); + } else { + // Unless the caller explicitly asked for us to dispatch synchronously, + // we always set a timeout to do this: The flux dispatcher complains + // if you dispatch from within a dispatch, so rather than action + // handlers having to worry about not calling anything that might + // then dispatch, we just do dispatches asynchronously. + setTimeout(super.dispatch.bind(this, payload), 0); + } } };