Skip to content

Commit

Permalink
_
Browse files Browse the repository at this point in the history
  • Loading branch information
HeyHamachi committed Feb 17, 2024
1 parent 68a6145 commit a7d6c68
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 39 deletions.
74 changes: 37 additions & 37 deletions src/js/postMessage.awaitable.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,48 @@

("use strict");
export class Connector {
constructor(tabId) {
this.tabId = tabId;
this.connector = null;
this.postPool = {};
if (tabId) {
this.load(tabId);
}
}

async load(tabId) {
this.tabId = tabId;
this.connector = await chrome.tabs.connect(tabId);
this.connector.onMessage.addListener((P) => {
if (Object.keys(this.postPool).includes(P.uuid)) {
this.postPool[P.uuid](P);
delete this.postPool[P.uuid];
}
});
this.connector.onDisconnect.addListener((P) => {
this.connector = null;
});
constructor(tabId) {
this.tabId = tabId;
this.connector = null;
this.postPool = {};
if (tabId) {
this.load(tabId);
}
}

async post(P) {
let uuid = crypto.randomUUID();
async load(tabId) {
this.tabId = tabId;
this.connector = await chrome.tabs.connect(tabId);
this.connector.onMessage.addListener((P) => {
if (P.uuid in this.postPool) {
this.postPool[P.uuid](P);
delete this.postPool[P.uuid];
}
});
this.connector.onDisconnect.addListener((P) => {
this.connector = null;
});
}

let R = new Promise((resolve, reject) => {
this.postPool[uuid] = (_) => {
resolve(_);
};
});
async post(P) {
let uuid = crypto.randomUUID();

if (!this.connector) {
await this.load(this.tabId);
}
this.connector.postMessage(
Object.assign(P, {
uuid: uuid,
})
);
let R = new Promise((resolve, reject) => {
this.postPool[uuid] = (_) => {
resolve(_);
};
});

return R;
if (!this.connector) {
await this.load(this.tabId);
}
this.connector.postMessage(
Object.assign(P, {
uuid: uuid,
})
);

return R;
}
}

12 changes: 10 additions & 2 deletions src/js/websocket.awaitable.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// https://github.com/xoFeulB

("use strict");

export class AwaitbleWebSocket {
constructor(url) {
this.socket = new WebSocket(url);
Expand Down Expand Up @@ -39,7 +38,16 @@ export class AwaitbleWebSocket {
async send(message) {
let uuid = crypto.randomUUID();
this.socket.send(
JSON.stringify(Object.assign({ "message": message }, { uuid: uuid }))
JSON.stringify(
Object.assign(
{
message: message,
},
{
uuid: uuid,
}
)
)
);
let R = new Promise((resolve, reject) => {
this.messagePool[uuid] = (_) => {
Expand Down

0 comments on commit a7d6c68

Please sign in to comment.