Skip to content

Commit

Permalink
update HMR interface
Browse files Browse the repository at this point in the history
  • Loading branch information
LorisSigrist committed Oct 17, 2023
1 parent b4868a3 commit 972768b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/core/HMR.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* @typedef {{
* "t18s:createLocale": { locale: string }
* "t18s:removeLocale": { locale: string }
* "t18s:invalidateLocale": { locale: string }
* "t18s:addDictionary": { locale: string, domain: string }
* "t18s:removeDictionary": { locale: string, domain: string }
* "t18s:reloadDictionary": { locale: string, domain: string }
* }} HMREventMap
*/

Expand Down
12 changes: 6 additions & 6 deletions src/core/MessageCatalogue.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { DoubleKeyedMap } from "./utils/DoubleKeyedMap.js";
* The valid events that may be emitted by a locale registry.
*
* @typedef {{
* "locale_added": CustomEvent<{ locale: string, dictionary: Dictionary }>,
* "locale_removed": CustomEvent<{ locale: string }>,
* "locale_updated": CustomEvent<{ locale: string, dictionary: Dictionary }>,
* "dictionary_added": CustomEvent<{ locale: string, domain: string, dictionary: Dictionary }>,
* "dictionary_removed": CustomEvent<{ locale: string, domain:string }>,
* "dictionary_changed": CustomEvent<{ locale: string, domain: string, dictionary: Dictionary }>,
* "changed": CustomEvent<{}>;
* }} LocaleRegistryEventMap
*/
Expand Down Expand Up @@ -48,7 +48,7 @@ export class MessageCatalogue extends MessageCatalogueEventTarget {
this.#files.set(locale, domain, filePath);
this.#dictionaries.set(locale, domain, dictionary);

this.#dispatch("locale_added", { locale, dictionary });
this.#dispatch("dictionary_added", { locale, domain, dictionary });
this.#dispatch("changed", {});
}

Expand All @@ -61,7 +61,7 @@ export class MessageCatalogue extends MessageCatalogueEventTarget {
this.#dictionaries.delete(locale, domain);
this.#files.delete(locale, domain);

this.#dispatch("locale_removed", { locale });
this.#dispatch("dictionary_removed", { locale, domain });
this.#dispatch("changed", {});
}

Expand All @@ -77,7 +77,7 @@ export class MessageCatalogue extends MessageCatalogueEventTarget {
throw new LocaleNotFoundException(locale);
this.#dictionaries.set(locale, domain, dictionary);
this.#dispatch("changed", {});
this.#dispatch("locale_updated", { locale, dictionary });
this.#dispatch("dictionary_changed", { locale, domain, dictionary });
}

/**
Expand Down
9 changes: 4 additions & 5 deletions src/core/codegen/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,8 @@ export function generateMainModuleCode(Catalogue, verbose) {
}
});
if(import.meta.hot) {
import.meta.hot.on("t18s:createLocale", async (data) => {
if(import.meta.hot) {
import.meta.hot.on("t18s:addDictionary", async (data) => {
locales.update((locales) => [...locales, data.locale]);
//Force-reload the module - Add a random query parameter to bust the cache
Expand All @@ -142,7 +141,7 @@ export function generateMainModuleCode(Catalogue, verbose) {
t.set(getMessage); //update the store
});
import.meta.hot.on("t18s:invalidateLocale", async (data) => {
import.meta.hot.on("t18s:reloadDictionary", async (data) => {
//Force-reload the module - Add a random query parameter to bust the cache
const newMessages = (await import(/* @vite-ignore */ "/@id/__x00__t18s-dictionary:" + data.locale + ":messages" + "?" + Math.random())).default;
Expand All @@ -154,7 +153,7 @@ export function generateMainModuleCode(Catalogue, verbose) {
t.set(getMessage); //update the store
});
import.meta.hot.on("t18s:removeLocale", async (data) => {
import.meta.hot.on("t18s:removeDictionary", async (data) => {
${
verbose
? 'console.info("[t18s] Removing locale " + data.locale);\n'
Expand Down
15 changes: 9 additions & 6 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,20 @@ export function t18sCore(pluginConfig) {
const Catalogue = new MessageCatalogue();

Catalogue.addEventListener("changed", async () => await regenerateDTS());
Catalogue.addEventListener("locale_added", (e) => {
Catalogue.addEventListener("dictionary_added", (e) => {
reporter.localeCreated(e.detail.locale);
dispatch("t18s:createLocale", { locale: e.detail.locale });
dispatch("t18s:addDictionary", {
locale: e.detail.locale,
domain: e.detail.domain,
});
});
Catalogue.addEventListener("locale_removed", (e) => {
Catalogue.addEventListener("dictionary_removed", (e) => {
reporter.localeDeleted(e.detail.locale);
dispatch("t18s:removeLocale", { locale: e.detail.locale });
dispatch("t18s:removeDictionary", { locale: e.detail.locale, domain: e.detail.domain });
});
Catalogue.addEventListener("locale_updated", (e) => {
Catalogue.addEventListener("dictionary_changed", (e) => {
reporter.localeUpdated(e.detail.locale);
dispatch("t18s:invalidateLocale", { locale: e.detail.locale });
dispatch("t18s:reloadDictionary", { locale: e.detail.locale, domain: e.detail.domain });
});

/** Handles interactions with translation files */
Expand Down

0 comments on commit 972768b

Please sign in to comment.