From c16c920f941a025ad8cc4abab6acaa69bb1ee006 Mon Sep 17 00:00:00 2001 From: epriestley Date: Wed, 7 Dec 2011 13:49:56 -0800 Subject: [PATCH] Remove setTimeout() hacks for Javelin behavior initialization Summary: - Prioritize higher-priority behaviors on the server. - Remove setTimeout() hacks. Test Plan: Loaded Differential, didn't get CSRF races for comment previews. Reviewers: aran, jg, cpojer Reviewed By: jg CC: btrahan, jungejason, aran, epriestley, jg Differential Revision: 1183 --- .../CelerityStaticResourceResponse.php | 26 +++++++++++++++++-- .../celerity/response/__init__.php | 1 + .../differential/behavior-comment-preview.js | 4 +-- .../maniphest/behavior-transaction-preview.js | 2 +- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/infrastructure/celerity/response/CelerityStaticResourceResponse.php b/src/infrastructure/celerity/response/CelerityStaticResourceResponse.php index 4331df11cc..9ca17bbff6 100644 --- a/src/infrastructure/celerity/response/CelerityStaticResourceResponse.php +++ b/src/infrastructure/celerity/response/CelerityStaticResourceResponse.php @@ -146,9 +146,31 @@ final class CelerityStaticResourceResponse { $onload = array(); if ($this->behaviors) { - $behavior = json_encode($this->behaviors); - $onload[] = 'JX.initBehaviors('.$behavior.')'; + $behaviors = $this->behaviors; $this->behaviors = array(); + + $higher_priority_names = array( + 'refresh-csrf', + ); + + $higher_priority_behaviors = array_select_keys( + $behaviors, + $higher_priority_names); + + foreach ($higher_priority_names as $name) { + unset($behaviors[$name]); + } + + $behavior_groups = array( + $higher_priority_behaviors, + $behaviors); + + foreach ($behavior_groups as $group) { + if (!$group) { + continue; + } + $onload[] = 'JX.initBehaviors('.json_encode($group).')'; + } } if ($onload) { diff --git a/src/infrastructure/celerity/response/__init__.php b/src/infrastructure/celerity/response/__init__.php index 443a0c6c64..bb38ad13ca 100644 --- a/src/infrastructure/celerity/response/__init__.php +++ b/src/infrastructure/celerity/response/__init__.php @@ -9,6 +9,7 @@ phutil_require_module('phabricator', 'infrastructure/celerity/map'); phutil_require_module('phutil', 'markup'); +phutil_require_module('phutil', 'utils'); phutil_require_source('CelerityStaticResourceResponse.php'); diff --git a/webroot/rsrc/js/application/differential/behavior-comment-preview.js b/webroot/rsrc/js/application/differential/behavior-comment-preview.js index 0dca79339e..3312d449f4 100644 --- a/webroot/rsrc/js/application/differential/behavior-comment-preview.js +++ b/webroot/rsrc/js/application/differential/behavior-comment-preview.js @@ -30,7 +30,7 @@ JX.behavior('differential-feedback-preview', function(config) { JX.DOM.listen(content, 'keydown', null, trigger); JX.DOM.listen(action, 'change', null, trigger); - setTimeout(function() { request.start(); }, 0); + request.start(); function refreshInlinePreview() { @@ -46,5 +46,5 @@ JX.behavior('differential-feedback-preview', function(config) { null, refreshInlinePreview); - setTimeout(refreshInlinePreview, 0); + refreshInlinePreview(); }); diff --git a/webroot/rsrc/js/application/maniphest/behavior-transaction-preview.js b/webroot/rsrc/js/application/maniphest/behavior-transaction-preview.js index 7b84891f9a..2ee2d4d523 100644 --- a/webroot/rsrc/js/application/maniphest/behavior-transaction-preview.js +++ b/webroot/rsrc/js/application/maniphest/behavior-transaction-preview.js @@ -46,5 +46,5 @@ JX.behavior('maniphest-transaction-preview', function(config) { JX.DOM.listen(comments, 'keydown', null, trigger); JX.DOM.listen(action, 'change', null, trigger); - setTimeout(function() { request.start(); }, 0); + request.start(); });