Skip to content

Commit

Permalink
fix: add locks to diagon init function
Browse files Browse the repository at this point in the history
  • Loading branch information
elmouradiaminedev committed Feb 13, 2024
1 parent 199e708 commit 1310253
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 177 deletions.
161 changes: 5 additions & 156 deletions examples/with-react/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions examples/with-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"preview": "vite preview"
},
"dependencies": {
"diagonjs": "^1.5.2",
"diagonjs": "^1.6.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand All @@ -24,7 +24,6 @@
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.5",
"typescript": "^5.2.2",
"vite": "^5.1.0",
"vite-plugin-static-copy": "^1.0.1"
"vite": "^5.1.0"
}
}
5 changes: 3 additions & 2 deletions examples/with-react/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ function App() {
const [output, setOutput] = useState("");

useEffect(() => {
Diagon.init().then((instance) => {
(async () => {
const instance = await Diagon.init();
setDiagon(instance);
});
})();
}, []);

return (
Expand Down
14 changes: 1 addition & 13 deletions examples/with-react/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import { viteStaticCopy } from "vite-plugin-static-copy";

// https://vitejs.dev/config/
export default defineConfig({
plugins: [
react(),
// Make sure to copy the wasm file to the root of your build folder
viteStaticCopy({
targets: [
{
src: "**/**.wasm",
dest: "./",
},
],
}),
],
plugins: [react()],
});
12 changes: 11 additions & 1 deletion src/lib/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,20 @@ let diagonModule: Awaited<ReturnType<typeof DiagonModule>> | undefined;

let _translate: TranslationFunction;

let initLock: Promise<void> | null = null;

export const _init = async ({ wasmUrl }: { wasmUrl?: string } = {}) => {
if (initLock) {
await initLock;
}

if (diagonModule) {
return;
}

let lockResolver = () => {};
initLock = new Promise<void>((resolve) => (lockResolver = resolve));

let wasmBinary;

if (typeof window !== "undefined" || wasmUrl) {
Expand All @@ -39,7 +48,6 @@ export const _init = async ({ wasmUrl }: { wasmUrl?: string } = {}) => {
`https://cdn.jsdelivr.net/npm/diagonjs@${version}/dist/diagon.js-1.1.wasm`,
);
const buffer = await response.arrayBuffer();

wasmBinary = buffer;
}

Expand All @@ -52,6 +60,8 @@ export const _init = async ({ wasmUrl }: { wasmUrl?: string } = {}) => {
"string",
"string",
]);
initLock = null;
lockResolver();
};

const _stringifyOptions = (options: TranslationOptions): string =>
Expand Down
6 changes: 4 additions & 2 deletions src/translators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,10 @@ export type DiagonType = {
translate: Translator;
};

export const init = async (): Promise<DiagonType> => {
await _init();
export const init = async ({
wasmUrl,
}: { wasmUrl?: string } = {}): Promise<DiagonType> => {
await _init({ wasmUrl });

return {
translate: {
Expand Down

0 comments on commit 1310253

Please sign in to comment.