From 3e8d5bd400a2fe6761a2874c9cf150ffa1ab15f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Ca=C5=82ka?= Date: Thu, 23 Dec 2021 13:59:25 +0100 Subject: [PATCH] Replace jQuery code with vanilla JS for better performance (#35) * Bundle `pusher-js` * Update `pusher/pusher-php-server` * StyleCi fix * Revert "Bundle `pusher-js`" This reverts commit 55bb1ff0988973e436317c00ca2b83ad4c39d6da. * Remove no longer used code * Replace `$.getScript` for better performance * Revert "Update `pusher/pusher-php-server`" This reverts commit 7d1e2336 * Use `import()` * Add `once` * Update js/tsconfig.json Co-authored-by: Alexander Skvortsov <38059171+askvortsov1@users.noreply.github.com> * Async function Co-authored-by: Alexander Skvortsov <38059171+askvortsov1@users.noreply.github.com> --- extensions/pusher/js/src/forum/index.ts | 57 +++++++++---------------- extensions/pusher/js/tsconfig.json | 3 +- 2 files changed, 21 insertions(+), 39 deletions(-) diff --git a/extensions/pusher/js/src/forum/index.ts b/extensions/pusher/js/src/forum/index.ts index f1c8c8d9f..a36282752 100644 --- a/extensions/pusher/js/src/forum/index.ts +++ b/extensions/pusher/js/src/forum/index.ts @@ -9,27 +9,27 @@ import ItemList from 'flarum/common/utils/ItemList'; import { VnodeDOM } from 'Mithril'; app.initializers.add('flarum-pusher', () => { - app.pusher = new Promise((resolve) => { - $.getScript('//cdn.jsdelivr.net/npm/pusher-js@7.0.3/dist/web/pusher.min.js', () => { - const socket: PusherTypes.default = new Pusher(app.forum.attribute('pusherKey'), { - authEndpoint: `${app.forum.attribute('apiUrl')}/pusher/auth`, - cluster: app.forum.attribute('pusherCluster'), - auth: { - headers: { - 'X-CSRF-Token': app.session.csrfToken, - }, - }, - }); + app.pusher = (async () => { + await import('//cdn.jsdelivr.net/npm/pusher-js@7.0.3/dist/web/pusher.min.js' /* webpackIgnore: true, webpackPrefetch: true */); - return resolve({ - channels: { - main: socket.subscribe('public'), - user: app.session.user ? socket.subscribe(`private-user${app.session.user.id()}`) : null, + const socket: PusherTypes.default = new Pusher(app.forum.attribute('pusherKey'), { + authEndpoint: `${app.forum.attribute('apiUrl')}/pusher/auth`, + cluster: app.forum.attribute('pusherCluster'), + auth: { + headers: { + 'X-CSRF-Token': app.session.csrfToken, }, - pusher: socket, - }); + }, }); - }); + + return { + channels: { + main: socket.subscribe('public'), + user: app.session.user ? socket.subscribe(`private-user${app.session.user.id()}`) : null, + }, + pusher: socket, + }; + })(); app.pushedUpdates = []; @@ -96,25 +96,6 @@ app.initializers.add('flarum-pusher', () => { } }); - // Prevent any newly-created discussions from triggering the discussion list - // update button showing. - // TODO: Might be better pause the response to the push updates while the - // composer is loading? idk - // TODO: It seems that this is not used - extend(DiscussionList.prototype, 'addDiscussion', function (returned, discussion) { - const index = app.pushedUpdates.indexOf(discussion.id()); - - if (index !== -1) { - app.pushedUpdates.splice(index, 1); - } - - if (app.current.matches(IndexPage)) { - app.setTitleCount(app.pushedUpdates.length); - } - - m.redraw(); - }); - extend(DiscussionPage.prototype, 'oncreate', function () { app.pusher.then((binding) => { const pusher = binding.pusher; @@ -131,7 +112,7 @@ app.initializers.add('flarum-pusher', () => { if (!document.hasFocus()) { app.setTitleCount(Math.max(0, this.discussion.commentCount() - oldCount)); - $(window).one('focus', () => app.setTitleCount(0)); + window.addEventListener('focus', () => app.setTitleCount(0), { once: true }); } }); } diff --git a/extensions/pusher/js/tsconfig.json b/extensions/pusher/js/tsconfig.json index 4463b83dc..37491d00e 100644 --- a/extensions/pusher/js/tsconfig.json +++ b/extensions/pusher/js/tsconfig.json @@ -7,6 +7,7 @@ "baseUrl": ".", "paths": { "flarum/*": ["../vendor/flarum/core/js/dist-typings/*"] - } + }, + "module": "es2020" } }