-
-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(messageformat): 🎸 add cache support
Closes: #358
- Loading branch information
1 parent
0385eb2
commit 773bc5c
Showing
8 changed files
with
150 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
libs/transloco-messageformat/src/lib/messageformat.factory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { MFLocale } from './messageformat.config'; | ||
import MessageFormat, { | ||
MessageFormatOptions, | ||
MessageFunction, | ||
} from '@messageformat/core'; | ||
|
||
export type MFFactory = ( | ||
locales: MFLocale, | ||
messageConfig: MessageFormatOptions<'string'> | ||
) => MessageFormat; | ||
|
||
export function defaultFactory( | ||
locales: MFLocale, | ||
messageConfig: MessageFormatOptions<'string'> | ||
): MessageFormat { | ||
return new MessageFormat<'string'>(locales, messageConfig); | ||
} | ||
|
||
export function cachedFactory( | ||
locales: MFLocale, | ||
messageConfig: MessageFormatOptions<'string'> | ||
): MessageFormat { | ||
const mf = defaultFactory(locales, messageConfig); | ||
const original = mf.compile; | ||
const cache = new Map<string, MessageFunction<'string'>>(); | ||
const localeKey = `__${locales?.toString() || MessageFormat.defaultLocale}__`; | ||
|
||
mf.compile = function (messages: string): MessageFunction<'string'> { | ||
const cacheKey = `${localeKey}${messages}`; | ||
const cachedMsg = cache.get(cacheKey); | ||
|
||
if (cachedMsg) { | ||
return cachedMsg; | ||
} | ||
|
||
const msg = original.call(this, messages); | ||
cache.set(cacheKey, msg); | ||
|
||
return msg; | ||
}; | ||
|
||
return mf; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.