Skip to content

Commit e8589a4

Browse files
committed
feat: show connecting overlay after a 3s timer so that it doesn't interfere the workflow
1 parent b964496 commit e8589a4

File tree

1 file changed

+44
-6
lines changed

1 file changed

+44
-6
lines changed

src/LiveDevelopment/main.js

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,25 +276,52 @@ define(function main(require, exports, module) {
276276
let $overlayContainer = null; // the overlay container
277277
let shouldShowSyncErrorOverlay = true; // once user closes the overlay we don't show them again
278278
let shouldShowConnectingOverlay = true;
279+
let connectingOverlayTimer = null; // this is needed as we show the connecting overlay after 3s
280+
let connectingOverlayTimeDuration = 3000;
279281

280282
/**
281-
* this function is responsible to show the overlay.
282-
* so overlay is shown when the live preview is connecting or live preview stopped because of some syntax error
283+
* this function is responsible to check whether to show the overlay or not and how it should be shown
284+
* because if user has closed the overlay manually, we don't show it again
285+
* secondly, for connecting overlay we show that after a 3s timer, but sync error overlay is shown immediately
283286
* @param {String} textMessage - the text that is written inside the overlay
284287
* @param {Number} status - 1 for connect, 4 for sync error but we match it using MultiBrowserLiveDev
285288
*/
286-
function _showOverlay(textMessage, status) {
289+
function _handleOverlay(textMessage, status) {
287290
if (!$livePreviewPanel) {
288291
$livePreviewPanel = $("#panel-live-preview");
289292
}
290293

291-
// remove any existing overlay
294+
// remove any existing overlay & timer
292295
_hideOverlay();
293296

294297
// to not show the overlays if user has already closed it before
295298
if(status === MultiBrowserLiveDev.STATUS_CONNECTING && !shouldShowConnectingOverlay) { return; }
296299
if(status === MultiBrowserLiveDev.STATUS_SYNC_ERROR && !shouldShowSyncErrorOverlay) { return; }
297300

301+
// for connecting status, we delay showing the overlay by 3 seconds
302+
if(status === MultiBrowserLiveDev.STATUS_CONNECTING) {
303+
connectingOverlayTimer = setTimeout(() => {
304+
_createAndShowOverlay(textMessage, status);
305+
connectingOverlayTimer = null;
306+
}, connectingOverlayTimeDuration);
307+
return;
308+
}
309+
310+
// for sync error status, show immediately
311+
_createAndShowOverlay(textMessage, status);
312+
}
313+
314+
/**
315+
* this function is responsible to create & show the overlay.
316+
* so overlay is shown when the live preview is connecting or live preview stopped because of some syntax error
317+
* @param {String} textMessage - the text that is written inside the overlay
318+
* @param {Number} status - 1 for connect, 4 for sync error but we match it using MultiBrowserLiveDev
319+
*/
320+
function _createAndShowOverlay(textMessage, status) {
321+
if (!$livePreviewPanel) {
322+
$livePreviewPanel = $("#panel-live-preview");
323+
}
324+
298325
// create the overlay element
299326
// styled inside the 'src/extensionsIntegrated/Phoenix-live-preview/live-preview.css'
300327
$overlayContainer = $("<div>").addClass("live-preview-status-overlay"); // the wrapper for overlay element
@@ -323,12 +350,23 @@ define(function main(require, exports, module) {
323350
* responsible to hide the overlay
324351
*/
325352
function _hideOverlay() {
353+
_clearConnectingOverlayTimer();
326354
if ($overlayContainer) {
327355
$overlayContainer.remove();
328356
$overlayContainer = null;
329357
}
330358
}
331359

360+
/**
361+
* This is a helper function that just checks that if connectingOverlayTimer exists, we clear it
362+
*/
363+
function _clearConnectingOverlayTimer() {
364+
if (connectingOverlayTimer) {
365+
clearTimeout(connectingOverlayTimer);
366+
connectingOverlayTimer = null;
367+
}
368+
}
369+
332370
/**
333371
* this function adds/remove the full-width class from the overlay container
334372
* styled inside 'src/extensionsIntegrated/Phoenix-live-preview/live-preview.css'
@@ -406,9 +444,9 @@ define(function main(require, exports, module) {
406444

407445
MultiBrowserLiveDev.on(MultiBrowserLiveDev.EVENT_STATUS_CHANGE, function(event, status) {
408446
if (status === MultiBrowserLiveDev.STATUS_CONNECTING) {
409-
_showOverlay(Strings.LIVE_DEV_STATUS_TIP_PROGRESS1, status);
447+
_handleOverlay(Strings.LIVE_DEV_STATUS_TIP_PROGRESS1, status);
410448
} else if (status === MultiBrowserLiveDev.STATUS_SYNC_ERROR) {
411-
_showOverlay(Strings.LIVE_DEV_STATUS_TIP_SYNC_ERROR, status);
449+
_handleOverlay(Strings.LIVE_DEV_STATUS_TIP_SYNC_ERROR, status);
412450
} else {
413451
_hideOverlay();
414452
}

0 commit comments

Comments
 (0)