From 1310253c5e0c9b9ddfd6b263f7926db46324cfa5 Mon Sep 17 00:00:00 2001 From: Amine Elmouradi Date: Tue, 13 Feb 2024 01:46:10 +0100 Subject: [PATCH] fix: add locks to diagon init function --- examples/with-react/package-lock.json | 161 +------------------------- examples/with-react/package.json | 5 +- examples/with-react/src/App.tsx | 5 +- examples/with-react/vite.config.ts | 14 +-- src/lib/internal.ts | 12 +- src/translators/index.ts | 6 +- 6 files changed, 26 insertions(+), 177 deletions(-) diff --git a/examples/with-react/package-lock.json b/examples/with-react/package-lock.json index 179e9ac6..e84a77d4 100644 --- a/examples/with-react/package-lock.json +++ b/examples/with-react/package-lock.json @@ -8,7 +8,7 @@ "name": "with-react", "version": "0.0.0", "dependencies": { - "diagonjs": "^1.5.2", + "diagonjs": "^1.6.0", "react": "^18.2.0", "react-dom": "^18.2.0" }, @@ -22,8 +22,7 @@ "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" } }, "node_modules/@aashutoshrathi/word-wrap": { @@ -1525,19 +1524,6 @@ "node": ">=4" } }, - "node_modules/anymatch": { - "version": "3.1.3", - "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.3.tgz", - "integrity": "sha512-KMReFUr0B4t+D+OBkjR3KYqvocp2XaSzO55UcB6mgQMd3KbcE+mWTyvVV7D/zsdEbNnV6acZUutkiHQXvTr1Rw==", - "dev": true, - "dependencies": { - "normalize-path": "^3.0.0", - "picomatch": "^2.0.4" - }, - "engines": { - "node": ">= 8" - } - }, "node_modules/argparse": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", @@ -1559,15 +1545,6 @@ "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", "dev": true }, - "node_modules/binary-extensions": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz", - "integrity": "sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==", - "dev": true, - "engines": { - "node": ">=8" - } - }, "node_modules/brace-expansion": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz", @@ -1664,42 +1641,6 @@ "node": ">=4" } }, - "node_modules/chokidar": { - "version": "3.6.0", - "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.6.0.tgz", - "integrity": "sha512-7VT13fmjotKpGipCW9JEQAusEPE+Ei8nl6/g4FBAmIm0GOOLMua9NDDo/DWp0ZAxCr3cPq5ZpBqmPAQgDda2Pw==", - "dev": true, - "dependencies": { - "anymatch": "~3.1.2", - "braces": "~3.0.2", - "glob-parent": "~5.1.2", - "is-binary-path": "~2.1.0", - "is-glob": "~4.0.1", - "normalize-path": "~3.0.0", - "readdirp": "~3.6.0" - }, - "engines": { - "node": ">= 8.10.0" - }, - "funding": { - "url": "https://paulmillr.com/funding/" - }, - "optionalDependencies": { - "fsevents": "~2.3.2" - } - }, - "node_modules/chokidar/node_modules/glob-parent": { - "version": "5.1.2", - "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz", - "integrity": "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==", - "dev": true, - "dependencies": { - "is-glob": "^4.0.1" - }, - "engines": { - "node": ">= 6" - } - }, "node_modules/color-convert": { "version": "1.9.3", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz", @@ -1771,9 +1712,9 @@ "dev": true }, "node_modules/diagonjs": { - "version": "1.5.2", - "resolved": "https://registry.npmjs.org/diagonjs/-/diagonjs-1.5.2.tgz", - "integrity": "sha512-V7lTKB0LQq7X/6IrY1h1glPW+lAmbvP0++nmKbpbnTTiJZ21w9/Rq32yUq/bndOdcy08cCKcN7aob5QoZbktvA==" + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/diagonjs/-/diagonjs-1.6.0.tgz", + "integrity": "sha512-c7FpdxFNAFmY8i9UDQLZDe18G9gF0Iqn6naY7dcLxDuRxFibCmUGQp1yVI5QVDDSH865waOwqYGXSdnlLLZhMw==" }, "node_modules/dir-glob": { "version": "3.0.1", @@ -2258,20 +2199,6 @@ "integrity": "sha512-36yxDn5H7OFZQla0/jFJmbIKTdZAQHngCedGxiMmpNfEZM0sdEeT+WczLQrjK6D7o2aiyLYDnkw0R3JK0Qv1RQ==", "dev": true }, - "node_modules/fs-extra": { - "version": "11.2.0", - "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-11.2.0.tgz", - "integrity": "sha512-PmDi3uwK5nFuXh7XDTlVnS17xJS7vW36is2+w3xcv8SVxiB4NyATf4ctkVY5bkSjX0Y4nbvZCq1/EjtEyr9ktw==", - "dev": true, - "dependencies": { - "graceful-fs": "^4.2.0", - "jsonfile": "^6.0.1", - "universalify": "^2.0.0" - }, - "engines": { - "node": ">=14.14" - } - }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -2384,12 +2311,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/graceful-fs": { - "version": "4.2.11", - "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.11.tgz", - "integrity": "sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==", - "dev": true - }, "node_modules/graphemer": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/graphemer/-/graphemer-1.4.0.tgz", @@ -2455,18 +2376,6 @@ "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", "dev": true }, - "node_modules/is-binary-path": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz", - "integrity": "sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw==", - "dev": true, - "dependencies": { - "binary-extensions": "^2.0.0" - }, - "engines": { - "node": ">=8" - } - }, "node_modules/is-extglob": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz", @@ -2571,18 +2480,6 @@ "node": ">=6" } }, - "node_modules/jsonfile": { - "version": "6.1.0", - "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz", - "integrity": "sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==", - "dev": true, - "dependencies": { - "universalify": "^2.0.0" - }, - "optionalDependencies": { - "graceful-fs": "^4.1.6" - } - }, "node_modules/keyv": { "version": "4.5.4", "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.4.tgz", @@ -2719,15 +2616,6 @@ "integrity": "sha512-y10wOWt8yZpqXmOgRo77WaHEmhYQYGNA6y421PKsKYWEK8aW+cqAphborZDhqfyKrbZEN92CN1X2KbafY2s7Yw==", "dev": true }, - "node_modules/normalize-path": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", - "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==", - "dev": true, - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", @@ -2948,18 +2836,6 @@ "node": ">=0.10.0" } }, - "node_modules/readdirp": { - "version": "3.6.0", - "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz", - "integrity": "sha512-hOS089on8RduqdbhvQ5Z37A0ESjsqz6qnRcffsMU3495FuTdqSm+7bhJ29JvIOsBDEEnan5DPu9t3To9VRlMzA==", - "dev": true, - "dependencies": { - "picomatch": "^2.2.1" - }, - "engines": { - "node": ">=8.10.0" - } - }, "node_modules/resolve-from": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-4.0.0.tgz", @@ -3241,15 +3117,6 @@ "node": ">=14.17" } }, - "node_modules/universalify": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.1.tgz", - "integrity": "sha512-gptHNQghINnc/vTGIk0SOFGFNXw7JVrlRUtConJRlvaw6DuX0wO5Jeko9sWrMBhh+PsYAZ7oXAiOnf/UKogyiw==", - "dev": true, - "engines": { - "node": ">= 10.0.0" - } - }, "node_modules/update-browserslist-db": { "version": "1.0.13", "resolved": "https://registry.npmjs.org/update-browserslist-db/-/update-browserslist-db-1.0.13.tgz", @@ -3344,24 +3211,6 @@ } } }, - "node_modules/vite-plugin-static-copy": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/vite-plugin-static-copy/-/vite-plugin-static-copy-1.0.1.tgz", - "integrity": "sha512-3eGL4mdZoPJMDBT68pv/XKIHR4MgVolStIxxv1gIBP4R8TpHn9C9EnaU0hesqlseJ4ycLGUxckFTu/jpuJXQlA==", - "dev": true, - "dependencies": { - "chokidar": "^3.5.3", - "fast-glob": "^3.2.11", - "fs-extra": "^11.1.0", - "picocolors": "^1.0.0" - }, - "engines": { - "node": "^18.0.0 || >=20.0.0" - }, - "peerDependencies": { - "vite": "^5.0.0" - } - }, "node_modules/which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", diff --git a/examples/with-react/package.json b/examples/with-react/package.json index 0a2dd6bf..f13eb990 100644 --- a/examples/with-react/package.json +++ b/examples/with-react/package.json @@ -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" }, @@ -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" } } diff --git a/examples/with-react/src/App.tsx b/examples/with-react/src/App.tsx index 64ecf597..53bba6c8 100644 --- a/examples/with-react/src/App.tsx +++ b/examples/with-react/src/App.tsx @@ -8,9 +8,10 @@ function App() { const [output, setOutput] = useState(""); useEffect(() => { - Diagon.init().then((instance) => { + (async () => { + const instance = await Diagon.init(); setDiagon(instance); - }); + })(); }, []); return ( diff --git a/examples/with-react/vite.config.ts b/examples/with-react/vite.config.ts index bcdd1250..9cc50ead 100644 --- a/examples/with-react/vite.config.ts +++ b/examples/with-react/vite.config.ts @@ -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()], }); diff --git a/src/lib/internal.ts b/src/lib/internal.ts index 93541cf1..28dc31c8 100644 --- a/src/lib/internal.ts +++ b/src/lib/internal.ts @@ -26,11 +26,20 @@ let diagonModule: Awaited> | undefined; let _translate: TranslationFunction; +let initLock: Promise | null = null; + export const _init = async ({ wasmUrl }: { wasmUrl?: string } = {}) => { + if (initLock) { + await initLock; + } + if (diagonModule) { return; } + let lockResolver = () => {}; + initLock = new Promise((resolve) => (lockResolver = resolve)); + let wasmBinary; if (typeof window !== "undefined" || wasmUrl) { @@ -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; } @@ -52,6 +60,8 @@ export const _init = async ({ wasmUrl }: { wasmUrl?: string } = {}) => { "string", "string", ]); + initLock = null; + lockResolver(); }; const _stringifyOptions = (options: TranslationOptions): string => diff --git a/src/translators/index.ts b/src/translators/index.ts index ce9afb47..3a106743 100644 --- a/src/translators/index.ts +++ b/src/translators/index.ts @@ -189,8 +189,10 @@ export type DiagonType = { translate: Translator; }; -export const init = async (): Promise => { - await _init(); +export const init = async ({ + wasmUrl, +}: { wasmUrl?: string } = {}): Promise => { + await _init({ wasmUrl }); return { translate: {