From e090b8e8c69f92339b436a405180b09b386e9c2b Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Thu, 24 Jun 2021 15:28:20 +1000 Subject: [PATCH] remove getInstance on swarmPolling --- js/background.js | 6 ++++-- preload.js | 2 +- ts/components/session/ActionsPanel.tsx | 6 +++--- ts/receiver/closedGroups.ts | 8 ++++---- ts/session/group/index.ts | 4 ++-- ts/session/snode_api/index.ts | 4 +++- ts/session/snode_api/swarmPolling.ts | 18 +++++++++--------- 7 files changed, 26 insertions(+), 22 deletions(-) diff --git a/js/background.js b/js/background.js index a96323cef..fc18b5435 100644 --- a/js/background.js +++ b/js/background.js @@ -450,9 +450,11 @@ }, window.CONSTANTS.NOTIFICATION_ENABLE_TIMEOUT_SECONDS * 1000); window.NewReceiver.queueAllCached(); - window.SwarmPolling.addPubkey(window.libsession.Utils.UserUtils.getOurPubKeyStrFromCache()); + window + .getSwarmPollingInstance() + .addPubkey(window.libsession.Utils.UserUtils.getOurPubKeyStrFromCache()); - window.SwarmPolling.start(); + window.getSwarmPollingInstance().start(); window.libsession.Utils.AttachmentDownloads.start({ logger: window.log, }); diff --git a/preload.js b/preload.js index addb2108d..5023abca0 100644 --- a/preload.js +++ b/preload.js @@ -287,7 +287,7 @@ window.Signal = Signal.setup({ logger: window.log, }); -window.SwarmPolling = require('./ts/session/snode_api/swarmPolling').SwarmPolling.getInstance(); +window.getSwarmPollingInstance = require('./ts/session/snode_api/').getSwarmPollingInstance; const WorkerInterface = require('./js/modules/util_worker_interface'); diff --git a/ts/components/session/ActionsPanel.tsx b/ts/components/session/ActionsPanel.tsx index 09112d985..a942c70cc 100644 --- a/ts/components/session/ActionsPanel.tsx +++ b/ts/components/session/ActionsPanel.tsx @@ -39,7 +39,7 @@ import { import { OpenGroupManagerV2 } from '../../opengroup/opengroupV2/OpenGroupManagerV2'; import { loadDefaultRooms } from '../../opengroup/opengroupV2/ApiUtil'; import { forceRefreshRandomSnodePool } from '../../session/snode_api/snodePool'; -import { SwarmPolling } from '../../session/snode_api/swarmPolling'; +import { getSwarmPollingInstance } from '../../session/snode_api'; import { IMAGE_JPEG } from '../../types/MIME'; import { FSv2 } from '../../fileserver'; import { debounce } from 'lodash'; @@ -276,8 +276,8 @@ const doAppStartUp = () => { // TODO: Investigate the case where we reconnect const ourKey = UserUtils.getOurPubKeyStrFromCache(); - SwarmPolling.getInstance().addPubkey(ourKey); - SwarmPolling.getInstance().start(); + getSwarmPollingInstance().addPubkey(ourKey); + getSwarmPollingInstance().start(); }; /** diff --git a/ts/receiver/closedGroups.ts b/ts/receiver/closedGroups.ts index fe6ad80e8..82360fcc0 100644 --- a/ts/receiver/closedGroups.ts +++ b/ts/receiver/closedGroups.ts @@ -33,7 +33,7 @@ import { getMessageController } from '../session/messages'; import { ClosedGroupEncryptionPairReplyMessage } from '../session/messages/outgoing/controlMessage/group/ClosedGroupEncryptionPairReplyMessage'; import { queueAllCachedFromSource } from './receiver'; import { actions as conversationActions } from '../state/ducks/conversations'; -import { SwarmPolling } from '../session/snode_api/swarmPolling'; +import { getSwarmPollingInstance } from '../session/snode_api'; import { MessageModel } from '../models/message'; import { updateConfirmModal } from '../state/ducks/modalDialog'; @@ -275,7 +275,7 @@ export async function handleNewClosedGroup( await addClosedGroupEncryptionKeyPair(groupId, ecKeyPair.toHexKeyPair()); // start polling for this new group - SwarmPolling.getInstance().addGroupId(PubKey.cast(groupId)); + getSwarmPollingInstance().addGroupId(PubKey.cast(groupId)); await removeFromCache(envelope); // trigger decrypting of all this group messages we did not decrypt successfully yet. @@ -298,7 +298,7 @@ export async function markGroupAsLeftOrKicked( } else { groupConvo.set('left', true); } - SwarmPolling.getInstance().removePubkey(groupPublicKey); + getSwarmPollingInstance().removePubkey(groupPublicKey); } /** @@ -903,7 +903,7 @@ export async function createClosedGroup(groupName: string, members: Array { window?.log?.info( `Leaving message sent ${groupId}. Removing everything related to this group.` diff --git a/ts/session/snode_api/index.ts b/ts/session/snode_api/index.ts index dfa8edac6..eb5e91830 100644 --- a/ts/session/snode_api/index.ts +++ b/ts/session/snode_api/index.ts @@ -2,4 +2,6 @@ import * as SnodePool from './snodePool'; import * as SNodeAPI from './SNodeAPI'; import * as Onions from './onions'; -export { SnodePool, SNodeAPI, Onions }; +import { getSwarmPollingInstance } from './swarmPolling'; + +export { SnodePool, SNodeAPI, Onions, getSwarmPollingInstance }; diff --git a/ts/session/snode_api/swarmPolling.ts b/ts/session/snode_api/swarmPolling.ts index b97c49095..ea2512e45 100644 --- a/ts/session/snode_api/swarmPolling.ts +++ b/ts/session/snode_api/swarmPolling.ts @@ -41,25 +41,25 @@ export function processMessage(message: string, options: any = {}) { } } +let instance: SwarmPolling | undefined; +export const getSwarmPollingInstance = () => { + if (!instance) { + instance = new SwarmPolling(); + } + return instance; +}; + export class SwarmPolling { - private static instance: SwarmPolling; private pubkeys: Array; private groupPubkeys: Array; private readonly lastHashes: { [key: string]: PubkeyToHash }; - private constructor() { + constructor() { this.pubkeys = []; this.groupPubkeys = []; this.lastHashes = {}; } - public static getInstance() { - if (!SwarmPolling.instance) { - SwarmPolling.instance = new SwarmPolling(); - } - return SwarmPolling.instance; - } - public start(): void { this.loadGroupIds(); void this.pollForAllKeys();