Skip to content

Commit 32c6c7f

Browse files
committed
add lock, and prevent double call for frontend mesasages loading
1 parent 9113d0d commit 32c6c7f

File tree

4 files changed

+52
-8
lines changed

4 files changed

+52
-8
lines changed

adminforth/plugins/i18n/Changelog.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
9+
## [1.0.14]
10+
11+
### Fixed
12+
13+
- Add `ignoreInitial` for watch to prevent initial messages loading
14+
- Add locking mechanism to prevent initial messages loading call in parallel (just in case)
15+
16+
## [1.0.13]
17+
18+
- Deduplicate frontend strings before creating translations
19+
20+
821
## [1.0.12]
922

1023
### Fixed

adminforth/plugins/i18n/index.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import iso6391, { LanguageCode } from 'iso-639-1';
55
import path from 'path';
66
import fs from 'fs-extra';
77
import chokidar from 'chokidar';
8+
import { AsyncQueue } from '@sapphire/async-queue';
9+
10+
const processFrontendMessagesQueue = new AsyncQueue();
811

912
const SLAVIC_PLURAL_EXAMPLES = {
1013
uk: 'яблук | Яблуко | Яблука | Яблук', // zero | singular | 2-4 | 5+
@@ -524,6 +527,7 @@ ${
524527
}
525528

526529
async processExtractedMessages(adminforth: IAdminForth, filePath: string) {
530+
await processFrontendMessagesQueue.wait();
527531
// messages file is in i18n-messages.json
528532
let messages;
529533
try {
@@ -535,7 +539,15 @@ ${
535539
return;
536540
}
537541
// loop over missingKeys[i].path and add them to database if not exists
538-
await Promise.all(messages.missingKeys.map(async (missingKey: any) => {
542+
543+
const missingKeysDeduplicated = messages.missingKeys.reduce((acc: any[], missingKey: any) => {
544+
if (!acc.find((a) => a.path === missingKey.path)) {
545+
acc.push(missingKey);
546+
}
547+
return acc;
548+
}, []);
549+
550+
await Promise.all(missingKeysDeduplicated.map(async (missingKey: any) => {
539551
const key = missingKey.path;
540552
const file = missingKey.file;
541553
const category = 'frontend';
@@ -567,15 +579,22 @@ ${
567579
const serveDir = adminforth.codeInjector.getServeDir();
568580
// messages file is in i18n-messages.json
569581
const messagesFile = path.join(serveDir, 'i18n-messages.json');
570-
console.log('🪲messagesFile', messagesFile);
582+
process.env.HEAVY_DEBUG && console.log('🪲🔔messagesFile read started', messagesFile);
571583
this.processExtractedMessages(adminforth, messagesFile);
572584
// we use watcher because file can't be yet created when we start - bundleNow can be done in build time or can be done now
573585
// that is why we make attempt to process it now and then watch for changes
574-
const w = chokidar.watch(messagesFile, { persistent: true });
586+
const w = chokidar.watch(messagesFile, {
587+
persistent: true,
588+
ignoreInitial: true, // don't trigger 'add' event for existing file on start
589+
});
575590
w.on('change', () => {
591+
process.env.HEAVY_DEBUG && console.log('🪲🔔messagesFile change', messagesFile);
592+
576593
this.processExtractedMessages(adminforth, messagesFile);
577594
});
578595
w.on('add', () => {
596+
process.env.HEAVY_DEBUG && console.log('🪲🔔messagesFile add', messagesFile);
597+
579598
this.processExtractedMessages(adminforth, messagesFile);
580599
});
581600

adminforth/plugins/i18n/package-lock.json

Lines changed: 15 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

adminforth/plugins/i18n/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@adminforth/i18n",
3-
"version": "1.0.12",
3+
"version": "1.0.13",
44
"main": "dist/index.js",
55
"types": "dist/index.d.ts",
66
"type": "module",
@@ -15,6 +15,7 @@
1515
"description": "",
1616
"dependencies": {
1717
"@aws-sdk/client-ses": "^3.654.0",
18+
"@sapphire/async-queue": "^1.5.5",
1819
"chokidar": "^4.0.1",
1920
"iso-639-1": "^3.1.3"
2021
},

0 commit comments

Comments
 (0)