调整目录
This commit is contained in:
54
packages/message/extension.ts
Normal file
54
packages/message/extension.ts
Normal file
@ -0,0 +1,54 @@
|
||||
import EventEmitter from 'eventemitter3';
|
||||
import { Connect } from '.';
|
||||
|
||||
export class ExtServer {
|
||||
private EE: EventEmitter;
|
||||
|
||||
constructor() {
|
||||
this.EE = new EventEmitter();
|
||||
chrome.runtime.onConnect.addListener((port) => {
|
||||
this.EE.emit('connect', new ExtConnect(port));
|
||||
});
|
||||
}
|
||||
|
||||
onConnect(callback: (con: Connect) => void) {
|
||||
this.EE.on('connect', callback);
|
||||
}
|
||||
}
|
||||
|
||||
export function connect() {
|
||||
return new ExtConnect(chrome.runtime.connect());
|
||||
}
|
||||
|
||||
export class ExtConnect implements Connect {
|
||||
private EE: EventEmitter;
|
||||
private port: chrome.runtime.Port;
|
||||
|
||||
constructor(port: chrome.runtime.Port) {
|
||||
this.EE = new EventEmitter();
|
||||
this.port = port;
|
||||
port.onMessage.addListener((message) => {
|
||||
this.EE.emit('message', message);
|
||||
});
|
||||
port.onDisconnect.addListener(() => {
|
||||
this.EE.emit('disconnect');
|
||||
this.EE.removeAllListeners();
|
||||
});
|
||||
}
|
||||
|
||||
postMessage(message: unknown) {
|
||||
this.port.postMessage(message);
|
||||
}
|
||||
|
||||
onMessage(callback: (message: unknown) => void) {
|
||||
this.EE.on('message', callback);
|
||||
}
|
||||
|
||||
onDisconnect(callback: () => void) {
|
||||
this.EE.on('disconnect', callback);
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
this.port.disconnect();
|
||||
}
|
||||
}
|
63
packages/message/index.ts
Normal file
63
packages/message/index.ts
Normal file
@ -0,0 +1,63 @@
|
||||
import EventEmitter from 'eventemitter3';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
export interface Server {
|
||||
onConnect: (callback: (con: Connect) => void) => void;
|
||||
}
|
||||
|
||||
export interface Connect {
|
||||
postMessage: (message: unknown) => void;
|
||||
onMessage: (callback: (message: unknown) => void) => void;
|
||||
onDisconnect: (callback: () => void) => void;
|
||||
disconnect: () => void;
|
||||
}
|
||||
|
||||
// 消息通道, 通过连接封装消息通道
|
||||
export class MessageChannel {
|
||||
private EE: EventEmitter;
|
||||
// 实例id
|
||||
private id: string;
|
||||
|
||||
constructor(private connect: Connect) {
|
||||
this.EE = new EventEmitter();
|
||||
this.id = uuidv4();
|
||||
connect.onMessage((message) => {
|
||||
// 判断消息是否为连接消息
|
||||
if (message && typeof message === 'object') {
|
||||
const data = message as { type: string; id: string };
|
||||
// 判断实例
|
||||
if (data.id == this.id) {
|
||||
switch (data.type) {
|
||||
case 'connect':
|
||||
// 建立连接
|
||||
break;
|
||||
case 'message':
|
||||
// 发送消息
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 发送一次消息并接收结果
|
||||
async send(message: unknown) {
|
||||
return new Promise((resolve) => {
|
||||
const callback = (message: unknown) => {
|
||||
this.connect.onMessage(callback);
|
||||
resolve(message);
|
||||
};
|
||||
this.connect.onMessage(callback);
|
||||
this.connect.postMessage(message);
|
||||
});
|
||||
}
|
||||
|
||||
// 发送消息
|
||||
postMessage(message: unknown) {
|
||||
this.connect.postMessage({
|
||||
type: 'message',
|
||||
id: this.id,
|
||||
data: message,
|
||||
});
|
||||
}
|
||||
}
|
13
packages/messages/.gitignore
vendored
13
packages/messages/.gitignore
vendored
@ -1,13 +0,0 @@
|
||||
# Local
|
||||
.DS_Store
|
||||
*.local
|
||||
*.log*
|
||||
|
||||
# Dist
|
||||
node_modules
|
||||
dist/
|
||||
|
||||
# IDE
|
||||
.vscode/*
|
||||
!.vscode/extensions.json
|
||||
.idea
|
@ -1,4 +0,0 @@
|
||||
# Lock files
|
||||
package-lock.json
|
||||
pnpm-lock.yaml
|
||||
yarn.lock
|
@ -1,3 +0,0 @@
|
||||
{
|
||||
"singleQuote": true
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
# Rslib Project
|
||||
|
||||
## Setup
|
||||
|
||||
Install the dependencies:
|
||||
|
||||
```bash
|
||||
pnpm install
|
||||
```
|
||||
|
||||
## Get Started
|
||||
|
||||
Build the library:
|
||||
|
||||
```bash
|
||||
pnpm build
|
||||
```
|
||||
|
||||
Build the library in watch mode:
|
||||
|
||||
```bash
|
||||
pnpm dev
|
||||
```
|
@ -1,10 +0,0 @@
|
||||
import js from '@eslint/js';
|
||||
import globals from 'globals';
|
||||
import ts from 'typescript-eslint';
|
||||
|
||||
export default [
|
||||
{ languageOptions: { globals: globals.browser } },
|
||||
js.configs.recommended,
|
||||
...ts.configs.recommended,
|
||||
{ ignores: ['dist/'] },
|
||||
];
|
@ -1,40 +0,0 @@
|
||||
{
|
||||
"name": "messages",
|
||||
"version": "1.0.0",
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"import": "./dist/index.js",
|
||||
"require": "./dist/index.cjs"
|
||||
}
|
||||
},
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "rslib build",
|
||||
"dev": "rslib build --watch",
|
||||
"format": "prettier --write .",
|
||||
"lint": "eslint .",
|
||||
"test": "vitest run"
|
||||
},
|
||||
"dependencies": {
|
||||
"eventemitter3": "~5.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^9.12.0",
|
||||
"@rslib/core": "^0.0.16",
|
||||
"@types/node": "^22.8.1",
|
||||
"eslint": "^9.12.0",
|
||||
"globals": "^15.11.0",
|
||||
"prettier": "^3.3.3",
|
||||
"typescript": "^5.6.3",
|
||||
"typescript-eslint": "^8.8.1",
|
||||
"vitest": "^2.1.4"
|
||||
},
|
||||
"private": true
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
import { defineConfig } from '@rslib/core';
|
||||
|
||||
export default defineConfig({
|
||||
lib: [
|
||||
{
|
||||
format: 'esm',
|
||||
syntax: 'es2021',
|
||||
dts: true,
|
||||
},
|
||||
{
|
||||
format: 'cjs',
|
||||
syntax: 'es2021',
|
||||
},
|
||||
],
|
||||
output: { target: 'node' },
|
||||
});
|
@ -1,3 +0,0 @@
|
||||
|
||||
|
||||
|
@ -1,37 +0,0 @@
|
||||
import EventEmitter from 'eventemitter3';
|
||||
|
||||
export class Server {
|
||||
private EE: EventEmitter;
|
||||
|
||||
constructor() {
|
||||
this.EE = new EventEmitter();
|
||||
}
|
||||
|
||||
onConnect(callback: (con: Connect) => void) {
|
||||
this.EE.on('connect', callback);
|
||||
}
|
||||
}
|
||||
|
||||
export interface Connect {
|
||||
postMessage: (message: unknown) => void;
|
||||
onMessage: (callback: (message: unknown) => void) => void;
|
||||
}
|
||||
|
||||
export class ChromeServer {
|
||||
|
||||
private EE: EventEmitter;
|
||||
|
||||
constructor() {
|
||||
chrome.runtime.onConnect.addListener((port) => {
|
||||
this.EE.emit('connect', {
|
||||
postMessage: (message: unknown) => port.postMessage(message),
|
||||
onMessage: (callback: (message: unknown) => void) =>
|
||||
port.onMessage.addListener(callback),
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
onConnect(callback: (con: Connect) => void) {
|
||||
this.EE.on('connect', callback);
|
||||
}
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
import { expect, test } from 'vitest';
|
||||
|
||||
test('squared', () => {
|
||||
expect(4).toBe(4);
|
||||
});
|
@ -1,15 +0,0 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"lib": ["ES2021"],
|
||||
"module": "ESNext",
|
||||
"noEmit": true,
|
||||
"strict": true,
|
||||
"skipLibCheck": true,
|
||||
"isolatedModules": true,
|
||||
"resolveJsonModule": true,
|
||||
"moduleResolution": "bundler",
|
||||
"useDefineForClassFields": true,
|
||||
"allowImportingTsExtensions": true
|
||||
},
|
||||
"include": ["src"]
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
import { defineConfig } from 'vitest/config';
|
||||
|
||||
export default defineConfig({
|
||||
// Configure Vitest (https://vitest.dev/config/)
|
||||
test: {},
|
||||
});
|
Reference in New Issue
Block a user