* add duckdb-ui-client & other ts pkgs * workflow fixes * fix working dir * no sparse checkout; specify package.json path * path to pnpm-lock.yaml * add check & build test * workflow step descriptions * use comments & names * one more naming tweak
73 lines
2.2 KiB
TypeScript
73 lines
2.2 KiB
TypeScript
import { sendDuckDBUIHttpRequest } from '../../http/functions/sendDuckDBUIHttpRequest.js';
|
|
import { tokenizeResultFromBuffer } from '../../serialization/functions/tokenizeResultFromBuffer.js';
|
|
import type { TokenizeResult } from '../../serialization/types/TokenizeResult.js';
|
|
import { DuckDBUIClientConnection } from './DuckDBUIClientConnection.js';
|
|
|
|
export { DuckDBUIClientConnection };
|
|
export type { TokenizeResult };
|
|
|
|
export class DuckDBUIClient {
|
|
private readonly eventSource: EventSource;
|
|
|
|
private defaultConnection: DuckDBUIClientConnection | undefined;
|
|
|
|
private constructor() {
|
|
this.eventSource = new EventSource('/localEvents');
|
|
}
|
|
|
|
public addOpenEventListener(listener: (event: Event) => void) {
|
|
this.eventSource.addEventListener('open', listener);
|
|
}
|
|
|
|
public removeOpenEventListener(listener: (event: Event) => void) {
|
|
this.eventSource.removeEventListener('open', listener);
|
|
}
|
|
|
|
public addErrorEventListener(listener: (event: Event) => void) {
|
|
this.eventSource.addEventListener('error', listener);
|
|
}
|
|
|
|
public removeErrorEventListener(listener: (event: Event) => void) {
|
|
this.eventSource.removeEventListener('error', listener);
|
|
}
|
|
|
|
public addMessageEventListener(
|
|
type: string,
|
|
listener: (event: MessageEvent) => void,
|
|
) {
|
|
this.eventSource.addEventListener(type, listener);
|
|
}
|
|
|
|
public removeMessageEventListener(
|
|
type: string,
|
|
listener: (event: MessageEvent) => void,
|
|
) {
|
|
this.eventSource.removeEventListener(type, listener);
|
|
}
|
|
|
|
public connect() {
|
|
return new DuckDBUIClientConnection();
|
|
}
|
|
|
|
public get connection(): DuckDBUIClientConnection {
|
|
if (!this.defaultConnection) {
|
|
this.defaultConnection = this.connect();
|
|
}
|
|
return this.defaultConnection;
|
|
}
|
|
|
|
public async tokenize(text: string): Promise<TokenizeResult> {
|
|
const buffer = await sendDuckDBUIHttpRequest('/ddb/tokenize', text);
|
|
return tokenizeResultFromBuffer(buffer);
|
|
}
|
|
|
|
private static singletonInstance: DuckDBUIClient;
|
|
|
|
public static get singleton(): DuckDBUIClient {
|
|
if (!DuckDBUIClient.singletonInstance) {
|
|
DuckDBUIClient.singletonInstance = new DuckDBUIClient();
|
|
}
|
|
return DuckDBUIClient.singletonInstance;
|
|
}
|
|
}
|