Skip to content

Commit

Permalink
feat: remove private properties
Browse files Browse the repository at this point in the history
  • Loading branch information
ceopaludetto committed May 28, 2024
1 parent 8feb372 commit 3e9413d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
10 changes: 10 additions & 0 deletions .changeset/afraid-spies-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@diacritic/utilities": patch
"@diacritic/detector": patch
"@diacritic/runtime": patch
"@diacritic/parser": patch
"@diacritic/react": patch
"@diacritic/core": patch
---

Remove private properties (incompatible with React Native)
34 changes: 17 additions & 17 deletions packages/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,66 +59,66 @@ export class Diacritic {
public readonly languages!: Language[];
public readonly namespaces!: Namespace[];

#current!: Language;
#modules: Record<Language, Record<Namespace, any>> = {} as any;
private current!: Language;
private modules: Record<Language, Record<Namespace, any>> = {} as any;

#listeners: Set<(language: Language) => void> = new Set();
private listeners: Set<(language: Language) => void> = new Set();

public constructor(registry: Registry, initialLanguage: Language = registry.defaultLanguage) {
this.registry = registry;
this.#current = initialLanguage;
this.current = initialLanguage;
}

public t!: Proxy;

public get language(): Language {
return this.#current;
return this.current;
};

public setLanguage = (language: Language) => {
this.#current = language;
this.#listeners.forEach(listener => listener(language));
this.current = language;
this.listeners.forEach(listener => listener(language));

this.t = createProxy(this.#current, this.#modules);
this.t = createProxy(this.current, this.modules);
};

public onChange = (listener: (language: Language) => void) => {
this.#listeners.add(listener);
return () => this.#listeners.delete(listener);
this.listeners.add(listener);
return () => this.listeners.delete(listener);
};

public loadModules = async (languages: Language[], namespaces: Namespace[]) => {
const promises: Promise<void>[] = [];
for (const language of languages) {
for (const namespace of namespaces) {
if (this.#modules[language] && this.#modules[language]![namespace]) continue;
promises.push(this.#loadModule(language, namespace));
if (this.modules[language] && this.modules[language]![namespace]) continue;
promises.push(this.loadModule(language, namespace));
}
}

if (promises.length === 0) return;

await Promise.all(promises);
this.t = createProxy(this.#current, this.#modules);
this.t = createProxy(this.current, this.modules);
};

public needToLoadModules = (languages: Language[], namespaces: Namespace[]) => {
const missing = [];
for (const language of languages) {
for (const namespace of namespaces) {
if (this.#modules[language] && this.#modules[language]![namespace]) continue;
if (this.modules[language] && this.modules[language]![namespace]) continue;
missing.push({ language, namespace });
}
}

return missing.length > 0;
};

async #loadModule(language: Language, namespace: Namespace) {
private async loadModule(language: Language, namespace: Namespace) {
const module = await this.registry.importTranslationModule(language, namespace);

if (!this.#modules[language]) this.#modules[language] = {} as any;
this.#modules[language]![namespace] = module;
if (!this.modules[language]) this.modules[language] = {} as any;
this.modules[language]![namespace] = module;
}
}

Expand Down

0 comments on commit 3e9413d

Please sign in to comment.