Compare commits
3 Commits
v0.0.6
...
v0.0.7-alp
| Author | SHA1 | Date | |
|---|---|---|---|
| 77e6394266 | |||
| 4e38c3a74e | |||
| 3f8971350e |
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@kevisual/query",
|
||||
"version": "0.0.6",
|
||||
"version": "0.0.7-alpha.1",
|
||||
"main": "dist/index.js",
|
||||
"module": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
|
||||
@@ -18,23 +18,24 @@ export default [
|
||||
typescript({
|
||||
allowImportingTsExtensions: true,
|
||||
noEmit: true,
|
||||
exclude: ['src/node-adapter.ts'],
|
||||
}), // 使用 @rollup/plugin-typescript 处理 TypeScript 文件
|
||||
],
|
||||
},
|
||||
{
|
||||
input: 'src/node-adapter.ts', // TypeScript 入口文件
|
||||
output: {
|
||||
file: 'dist/node-adapter.js', // 输出文件
|
||||
format: 'es', // 输出格式设置为 ES 模块
|
||||
},
|
||||
plugins: [
|
||||
resolve(), // 使用 @rollup/plugin-node-resolve 解析 node_modules 中的模块
|
||||
typescript({
|
||||
allowImportingTsExtensions: true,
|
||||
noEmit: true,
|
||||
}), // 使用 @rollup/plugin-typescript 处理 TypeScript 文件
|
||||
],
|
||||
},
|
||||
// {
|
||||
// input: 'src/node-adapter.ts', // TypeScript 入口文件
|
||||
// output: {
|
||||
// file: 'dist/node-adapter.js', // 输出文件
|
||||
// format: 'es', // 输出格式设置为 ES 模块
|
||||
// },
|
||||
// plugins: [
|
||||
// resolve(), // 使用 @rollup/plugin-node-resolve 解析 node_modules 中的模块
|
||||
// typescript({
|
||||
// allowImportingTsExtensions: true,
|
||||
// noEmit: true,
|
||||
// }), // 使用 @rollup/plugin-typescript 处理 TypeScript 文件
|
||||
// ],
|
||||
// },
|
||||
{
|
||||
input: 'src/ws.ts', // TypeScript 入口文件
|
||||
output: {
|
||||
@@ -46,6 +47,7 @@ export default [
|
||||
typescript({
|
||||
allowImportingTsExtensions: true,
|
||||
noEmit: true,
|
||||
declaration: false,
|
||||
}), // 使用 @rollup/plugin-typescript 处理 TypeScript 文件
|
||||
],
|
||||
},
|
||||
|
||||
13
src/index.ts
13
src/index.ts
@@ -1,6 +1,6 @@
|
||||
import { adapter } from './adapter.ts';
|
||||
import { QueryWs } from './ws.ts';
|
||||
export { QueryOpts };
|
||||
import { QueryWs, QueryWsOpts } from './ws.ts';
|
||||
export { QueryOpts, QueryWs };
|
||||
type Fn = (opts: {
|
||||
url?: string;
|
||||
headers?: Record<string, string>;
|
||||
@@ -98,7 +98,7 @@ export class QueryClient<U = any, V = any> extends Query<U, V> {
|
||||
storage: Storage;
|
||||
token: string;
|
||||
qws: QueryWs;
|
||||
constructor(opts?: QueryOpts & { tokenName?: string; storage?: Storage }) {
|
||||
constructor(opts?: QueryOpts & { tokenName?: string; storage?: Storage; io?: boolean }) {
|
||||
super(opts);
|
||||
this.tokenName = opts?.tokenName || 'token';
|
||||
this.storage = opts?.storage || localStorage;
|
||||
@@ -112,7 +112,12 @@ export class QueryClient<U = any, V = any> extends Query<U, V> {
|
||||
}
|
||||
return opts;
|
||||
};
|
||||
this.qws = new QueryWs({ url: opts?.url });
|
||||
if (opts?.io) {
|
||||
this.createWs();
|
||||
}
|
||||
}
|
||||
createWs(opts?: QueryWsOpts) {
|
||||
this.qws = new QueryWs({ url: this.url });
|
||||
}
|
||||
getToken() {
|
||||
return this.storage.getItem(this.tokenName);
|
||||
|
||||
41
src/ws.ts
41
src/ws.ts
@@ -9,7 +9,7 @@ type QueryWsStore = {
|
||||
};
|
||||
export type QuerySelectState = 'connecting' | 'connected' | 'disconnected';
|
||||
export type QueryWsStoreListener = (newState: QueryWsStore, oldState: QueryWsStore) => void;
|
||||
type QueryWsOpts = {
|
||||
export type QueryWsOpts = {
|
||||
url?: string;
|
||||
store?: StoreApi<QueryWsStore>;
|
||||
ws?: WebSocket;
|
||||
@@ -45,22 +45,31 @@ export class QueryWs {
|
||||
/**
|
||||
* 连接 WebSocket
|
||||
*/
|
||||
connect() {
|
||||
async connect(opts?: { timeout?: number }) {
|
||||
const store = this.store;
|
||||
const connected = store.getState().connected;
|
||||
if (connected) {
|
||||
return;
|
||||
return Promise.resolve(true);
|
||||
}
|
||||
const ws = this.ws || new WebSocket(this.url);
|
||||
ws.onopen = () => {
|
||||
store.getState().setConnected(true);
|
||||
store.getState().setStatus('connected');
|
||||
};
|
||||
ws.onclose = () => {
|
||||
store.getState().setConnected(false);
|
||||
store.getState().setStatus('disconnected');
|
||||
this.ws = null;
|
||||
};
|
||||
return new Promise((resolve, reject) => {
|
||||
const ws = this.ws || new WebSocket(this.url);
|
||||
const timeout = opts?.timeout || 5 * 60 * 1000; // 默认 2 分钟
|
||||
let timer = setTimeout(() => {
|
||||
console.error('WebSocket 连接超时');
|
||||
reject('timeout');
|
||||
}, timeout);
|
||||
ws.onopen = () => {
|
||||
store.getState().setConnected(true);
|
||||
store.getState().setStatus('connected');
|
||||
resolve(true);
|
||||
clearTimeout(timer);
|
||||
};
|
||||
ws.onclose = () => {
|
||||
store.getState().setConnected(false);
|
||||
store.getState().setStatus('disconnected');
|
||||
this.ws = null;
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
listenConnect(callback: () => void) {
|
||||
@@ -141,4 +150,10 @@ export class QueryWs {
|
||||
ws.send(data as string);
|
||||
}
|
||||
}
|
||||
getOpen() {
|
||||
if (!this.ws) {
|
||||
return false;
|
||||
}
|
||||
return this.ws.readyState === WebSocket.OPEN;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user