Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logout sync #329

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: use timeout & cleanup
jiafuei committed Jul 19, 2022
commit 4d79e9d0a143009aae5b101ecb9da4e1adff30f4
9 changes: 2 additions & 7 deletions src/controllers/TorusController.ts
Original file line number Diff line number Diff line change
@@ -1704,14 +1704,9 @@ export default class TorusController extends BaseController<TorusControllerConfi
private attachLogoutBC() {
const channelName = getLogoutBcChannelName(this.origin, this.userInfo);
const bc = new BroadcastChannel<LogoutMessage>(channelName);
const start = new Date().getTime();
const thisInstance = this.instanceId.slice(0, 8);
const eventListener = (msg: LogoutMessage) => {
const nowTime = new Date().getTime();
const msElapsedSinceAttach = nowTime - start;
const thisInstance = this.instanceId.slice(0, 8);
// Ignore the messages sent within 500ms of login, logic can be removed once TODO below is resolved
// TODO: Investigate Brave Browser receiving old logout messges from broadcast channel server
if (thisInstance === msg.instanceId || msElapsedSinceAttach <= 500) return;
if (thisInstance === msg.instanceId) return;
bc.removeEventListener("message", eventListener);
bc.close()
.then(() => this.emit("logout", true))
5 changes: 3 additions & 2 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { concatSig } from "@toruslabs/base-controllers";
import { concatSig, UserInfo } from "@toruslabs/base-controllers";
import { BroadcastChannel } from "@toruslabs/broadcast-channel";
import { post } from "@toruslabs/http-helpers";
import bowser from "bowser";
@@ -10,6 +10,7 @@ import config from "@/config";
import { addToast } from "@/modules/app";

import { LOCALE_EN, LOGIN_CONFIG, STORAGE_TYPE } from "./enums";
import { LogoutMessage } from "./interfaces";

export function getStorage(key: STORAGE_TYPE): Storage | undefined {
if (config.isStorageAvailable[key]) return window[key];
@@ -204,7 +205,7 @@ export const getLogoutBcChannelName = (origin: string, userInfo: UserInfo) => {

export const logoutWithBC = async (origin: string, _instanceId: string, userInfo: UserInfo) => {
const channelName = getLogoutBcChannelName(origin, userInfo);
const bc = new BroadcastChannel<LogoutMessage>(`${channelName}`);
const bc = new BroadcastChannel<LogoutMessage>(`${channelName}`, { server: { timeout: 5 } });
const timestamp = new Date().getTime();
const instanceId = _instanceId.slice(0, 8);
bc.postMessage({ instanceId, timestamp })