Skip to content

Commit

Permalink
fix: clear locale lookup cache when new messages are added (#130)
Browse files Browse the repository at this point in the history
* Added test to prove that addMessage is not updating returned messages

* Clear lookupCache for locale when addMessages is called

* Update test/runtime/includes/lookup.test.ts

Co-authored-by: Christian Kaisermann <christian@kaisermann.me>
  • Loading branch information
bbuhler and kaisermann committed Mar 17, 2021
1 parent d4b9881 commit 88ad5b2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/runtime/stores/dictionary.ts
Expand Up @@ -4,6 +4,7 @@ import deepmerge from 'deepmerge';
import type { LocaleDictionary, LocalesDictionary } from '../types/index';
import { getFallbackOf } from './locale';
import { delve } from '../../shared/delve';
import { lookupCache } from '../includes/lookup';

let dictionary: LocalesDictionary;
const $dictionary = writable<LocalesDictionary>({});
Expand Down Expand Up @@ -39,6 +40,8 @@ export function getClosestAvailableLocale(locale: string): string | null {
}

export function addMessages(locale: string, ...partials: LocaleDictionary[]) {
delete lookupCache[locale];

$dictionary.update((d) => {
d[locale] = deepmerge.all<LocaleDictionary>([d[locale] || {}, ...partials]);

Expand Down
8 changes: 8 additions & 0 deletions test/runtime/includes/lookup.test.ts
Expand Up @@ -83,3 +83,11 @@ test("doesn't cache falsy messages", () => {
pt: { field_2: 'nome' },
});
});

test('clears a locale lookup cache when new messages are added', () => {
addMessages('en', { field: 'name' });
expect(lookup('field', 'en')).toBe('name');

addMessages('en', { field: 'name2' });
expect(lookup('field', 'en')).toBe('name2');
});

0 comments on commit 88ad5b2

Please sign in to comment.