Skip to content

Commit 15b7f1c

Browse files
committed
feat: implement typescript for the core lib files
1 parent 9493786 commit 15b7f1c

File tree

8 files changed

+70
-26
lines changed

8 files changed

+70
-26
lines changed

biome.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"fix": "none",
5757
"level": "warn"
5858
},
59+
"noExplicitAny": "warn",
5960
"noConsoleLog": {
6061
"fix": "none",
6162
"level": "warn"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@joelshejar/react-ai-translator",
33
"description": "",
4-
"version": "0.0.59",
4+
"version": "0.0.60",
55
"author": "Joel Rajesh",
66
"license": "MIT",
77
"keywords": [],
File renamed without changes.

src/hooks/useTranslation.js renamed to src/hooks/useTranslation.ts

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,47 @@
11
import { useEffect, useRef, useState } from "react";
22

3-
const useTranslation = () => {
4-
const worker = useRef(null);
3+
// Define the possible statuses returned by the worker
4+
type WorkerStatus =
5+
| "initiate"
6+
| "progress"
7+
| "done"
8+
| "ready"
9+
| "update"
10+
| "complete";
11+
12+
// Define the structure of the progress items
13+
interface ProgressItem {
14+
file?: string;
15+
progress?: number;
16+
}
17+
18+
// Define the data structure for worker messages
19+
interface WorkerMessageData {
20+
status: WorkerStatus;
21+
output?: string;
22+
progress?: number;
23+
file?: string;
24+
}
25+
26+
// Hook return type
27+
interface UseTranslationReturn {
28+
translate: (text: string, srcLang: string, tgtLang: string) => void;
29+
translatedText: string;
30+
loading: boolean;
31+
modelLoading: boolean;
32+
progress: ProgressItem[];
33+
error: string | null;
34+
}
35+
36+
const useTranslation = (_workerScript: string): UseTranslationReturn => {
37+
const worker = useRef<Worker | null>(null);
538

639
// States for managing translation
7-
const [loading, setLoading] = useState(false);
8-
const [modelLoading, setModelLoading] = useState(false);
9-
const [progress, setProgress] = useState([]);
10-
const [translatedText, setTranslatedText] = useState("");
11-
const [error, setError] = useState(null);
40+
const [loading, setLoading] = useState<boolean>(false);
41+
const [modelLoading, setModelLoading] = useState<boolean>(false);
42+
const [progress, setProgress] = useState<ProgressItem[]>([]);
43+
const [translatedText, setTranslatedText] = useState<string>("");
44+
const [error, setError] = useState<string | null>(null);
1245

1346
console.log("hhhhh");
1447

@@ -21,7 +54,7 @@ const useTranslation = () => {
2154
}
2255

2356
// Handle worker messages
24-
const onMessage = (e) => {
57+
const onMessage = (e: MessageEvent<WorkerMessageData>) => {
2558
const { status, output, progress: progressValue, file } = e.data;
2659

2760
console.log(status, "status");
@@ -62,7 +95,9 @@ const useTranslation = () => {
6295
console.log("update");
6396
console.log(output, "output");
6497
// Append partial translations
65-
setTranslatedText(output);
98+
if (output) {
99+
setTranslatedText(output);
100+
}
66101
break;
67102
}
68103

@@ -77,13 +112,14 @@ const useTranslation = () => {
77112
worker.current.addEventListener("message", onMessage);
78113

79114
return () => {
80-
// worker.current.terminate(); // Clean up worker on unmount
115+
// If you want the worker to be terminated on unmount, uncomment below:
116+
// worker.current?.terminate();
81117
// worker.current = null;
82118
};
83119
}, []);
84120

85121
// Function to send translation requests to the worker
86-
const translate = (text, srcLang, tgtLang) => {
122+
const translate = (text: string, srcLang: string, tgtLang: string) => {
87123
console.log("kkkk");
88124
if (!worker.current) {
89125
console.error("Worker is not initialized.");
File renamed without changes.

src/worker.js renamed to src/worker.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { pipeline } from "@xenova/transformers";
1+
// worker.ts
2+
3+
import { type PipelineType, pipeline } from "@xenova/transformers";
4+
5+
// Extend the global scope for a dedicated worker
6+
declare const self: DedicatedWorkerGlobalScope & typeof globalThis;
27

38
/**
49
* This class uses the Singleton pattern to ensure that only one instance of the
@@ -8,23 +13,24 @@ import { pipeline } from "@xenova/transformers";
813
class MyTranslationPipeline {
914
static task = "translation";
1015
static model = "Xenova/nllb-200-distilled-600M";
11-
static instance = null;
16+
static instance: any = null;
1217

13-
static async getInstance(progressCallback = null) {
18+
static async getInstance(
19+
progressCallback: ((x: any) => void) | undefined = undefined,
20+
): Promise<any> {
1421
if (MyTranslationPipeline.instance === null) {
1522
MyTranslationPipeline.instance = pipeline(
16-
MyTranslationPipeline.task,
23+
MyTranslationPipeline.task as PipelineType,
1724
MyTranslationPipeline.model,
18-
{ dtype: "q8", progress_callback: progressCallback },
25+
{ quantized: true, progress_callback: progressCallback },
1926
);
2027
}
21-
2228
return MyTranslationPipeline.instance;
2329
}
2430
}
2531

2632
// Listen for messages from the main thread
27-
self.addEventListener("message", async (event) => {
33+
self.addEventListener("message", async (event: MessageEvent) => {
2834
// Retrieve the translation pipeline. When called for the first time,
2935
// this will load the pipeline and save it for future use.
3036
const translator = await MyTranslationPipeline.getInstance((x) => {
@@ -33,15 +39,15 @@ self.addEventListener("message", async (event) => {
3339
self.postMessage(x);
3440
});
3541

36-
// Actually perform the translation
37-
42+
// Log the language codes
3843
console.log(event.data.tgt_lang, event.data.src_lang, "langcode");
44+
45+
// Actually perform the translation
3946
const output = await translator?.(event.data.text, {
4047
tgt_lang: event.data.tgt_lang,
4148
src_lang: event.data.src_lang,
42-
4349
// Allows for partial output
44-
callback_function: (x) => {
50+
callback_function: (x: any) => {
4551
self.postMessage({
4652
status: "update",
4753
output: translator.tokenizer.decode(x[0].output_token_ids, {

tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
"esModuleInterop": true,
44
"jsx": "react-jsx",
55
"module": "ESNext",
6-
"moduleResolution": "Bundler",
6+
"moduleResolution": "node",
77
"skipLibCheck": true,
88
"strict": true,
9-
"noEmit": true
9+
"noEmit": true,
10+
"lib": ["ESNext", "DOM", "WebWorker"]
1011
}
1112
}

tsup.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import path from "node:path";
55
import { type Options, defineConfig } from "tsup";
66

77
const common: Options = {
8-
entry: ["src/index.js", "src/worker.js"],
8+
entry: ["src/index.ts", "src/worker.ts"],
99
treeshake: false,
1010
sourcemap: "inline",
1111
minify: true,

0 commit comments

Comments
 (0)