Skip to content

Commit

Permalink
feat: add inactive status style
Browse files Browse the repository at this point in the history
  • Loading branch information
wqcstrong committed Dec 4, 2023
1 parent 572bb06 commit b10d79f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 26 deletions.
4 changes: 4 additions & 0 deletions src/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
box-shadow: 0px 4px 8px 2px rgba(0, 0, 0, 0.2);
cursor: pointer;
z-index: 13000;
&.inactive {
background-color: #a2a2a2;
filter: grayscale(1);
}
}
.page-spy-modal {
position: fixed;
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ export default class PageSpy {
img.height = 50;
logo.append(img);
root.insertAdjacentElement('afterbegin', logo);
window.addEventListener('sdk-inactive', () => {
logo.classList.add('inactive');
});

const modal = new Modal();
const content = new Content({
Expand Down
31 changes: 11 additions & 20 deletions src/utils/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ export class SocketStore {

private reconnectTimes = 3;

// Don't try to reconnect and close immediately
// when user refresh the page.
private closeImmediately: boolean = false;

// indicated connected whether or not
public connectionStatus: boolean = false;

Expand Down Expand Up @@ -121,7 +117,8 @@ export class SocketStore {

public close() {
this.clearPing();
this.closeImmediately = true;
this.reconnectTimes = 0;
this.reconnectable = false;
this.socket?.close();
}

Expand All @@ -136,27 +133,21 @@ export class SocketStore {
this.connectionStatus = false;
this.socketConnection = null;
this.clearPing();

if (this.closeImmediately) return;
this.tryReconnect();
}

private tryReconnect() {
if (!this.reconnectable) {
if (!this.reconnectable || this.reconnectTimes <= 0) {
window.dispatchEvent(new CustomEvent('sdk-inactive'));
sessionStorage.setItem(
ROOM_SESSION_KEY,
JSON.stringify({ usable: false }),
);
return;
}
if (this.reconnectTimes > 0) {
this.reconnectTimes -= 1;
this.init(this.socketUrl);
} /* c8 ignore start */ else {
this.reconnectable = false;
psLog.error('Websocket reconnect failed');
}
/* c8 ignore stop */

this.tryReconnect();
}

tryReconnect() {
this.reconnectTimes -= 1;
this.init(this.socketUrl);
}

private pingConnect() {
Expand Down
7 changes: 1 addition & 6 deletions tests/utils/socket.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { WS } from 'jest-websocket-mock';
import { DEBUG_MESSAGE_TYPE } from 'src/utils/message';
import { SocketStore } from 'src/utils/socket';
import {
ConnectEvent,
UnicastEvent,
ErrorEvent,
JoinEvent,
} from 'types/lib/socket-event';
import { ConnectEvent, ErrorEvent } from 'types/lib/socket-event';
import * as SERVER_MESSAGE_TYPE from 'src/utils/message/server-type';

// Mock micro task delay
Expand Down

0 comments on commit b10d79f

Please sign in to comment.