From acd252803881b4d9d656e2b8a5a4e5a1059951e0 Mon Sep 17 00:00:00 2001 From: Shubham Dhama Date: Thu, 14 Jun 2018 21:46:02 +0530 Subject: [PATCH] transmit: Refactor logic for deferring the socket initialization. This makes sure that CSRF token is available while initializing Socket, irrespective of the order of execution of deferred callbacks after document becomes ready. This is part of #9416. --- frontend_tests/node_tests/transmit.js | 1 + static/js/setup.js | 2 +- static/js/transmit.js | 4 ++-- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/frontend_tests/node_tests/transmit.js b/frontend_tests/node_tests/transmit.js index 590a375117..6f0c7da0fc 100644 --- a/frontend_tests/node_tests/transmit.js +++ b/frontend_tests/node_tests/transmit.js @@ -22,6 +22,7 @@ zrequire('people'); zrequire('transmit'); function test_with_mock_socket(test_params) { + transmit.initialize(); var socket_send_called; var send_args = {}; diff --git a/static/js/setup.js b/static/js/setup.js index 38e5934d73..77caab0033 100644 --- a/static/js/setup.js +++ b/static/js/setup.js @@ -68,5 +68,5 @@ $(function () { return $(this).is(sel) || $(this).closest(sel).length; }; } - + transmit.initialize(); }); diff --git a/static/js/transmit.js b/static/js/transmit.js index bb5c5b615d..21a8c35575 100644 --- a/static/js/transmit.js +++ b/static/js/transmit.js @@ -3,7 +3,7 @@ var transmit = (function () { var exports = {}; var socket; -$(function () { +exports.initialize = function () { // We initialize the socket inside a function so that this code // runs after `csrf_token` is initialized in setup.js. if (page_params.use_websockets) { @@ -11,7 +11,7 @@ $(function () { } // For debugging. The socket will eventually move out of this file anyway. exports._socket = socket; -}); +}; function send_message_socket(request, success, error) { request.socket_user_agent = navigator.userAgent;