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

feat: add inactive status style #19

Merged
merged 2 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [1.4.3](https://github.com/HuolalaTech/page-spy/compare/v1.4.2...v1.4.3) (2023-12-01)

### [1.4.2](https://github.com/HuolalaTech/page-spy/compare/v1.4.1...v1.4.2) (2023-11-15)

### [1.3.1](https://github.com/HuolalaTech/page-spy/compare/v1.2.5...v1.3.1) (2023-10-16)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@huolala-tech/page-spy",
"version": "1.4.2",
"version": "1.4.3",
"description": "A developer tool for debugging remote web page.",
"main": "dist/index.min.js",
"types": "dist/types/index.d.ts",
Expand Down
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