Skip to content

Commit

Permalink
fix: lower memory usage (#1677)
Browse files Browse the repository at this point in the history
* fix: locale messages leaking in async context

* fix: reduce `deepCopy` memory usage

* fix: use `shallowRef` over `ref` server-side
  • Loading branch information
BobbieGoede authored Jan 6, 2024
1 parent 7f33b12 commit a287a26
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
13 changes: 8 additions & 5 deletions packages/shared/src/messages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hasOwn, isArray, isObject } from './utils'
import { isArray, isObject } from './utils'

const isNotObjectOrIsArray = (val: unknown) => !isObject(val) || isArray(val)
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
Expand All @@ -8,17 +8,20 @@ export function deepCopy(src: any, des: any): void {
throw new Error('Invalid value')
}

for (const key in src) {
if (hasOwn(src, key)) {
const stack = [{ src, des }]
while (stack.length) {
const { src, des } = stack.pop()!

Object.keys(src).forEach(key => {
if (isNotObjectOrIsArray(src[key]) || isNotObjectOrIsArray(des[key])) {
// replace with src[key] when:
// src[key] or des[key] is not an object, or
// src[key] or des[key] is an array
des[key] = src[key]
} else {
// src[key] and des[key] are both objects, merge them
deepCopy(src[key], des[key])
stack.push({ src: src[key], des: des[key] })
}
}
})
}
}
15 changes: 8 additions & 7 deletions packages/vue-i18n-core/src/composer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { ref, computed, getCurrentInstance, watch } from 'vue'
import { ref, computed, getCurrentInstance, watch, shallowRef } from 'vue'
import {
warn,
isArray,
Expand Down Expand Up @@ -1858,12 +1858,13 @@ export function createComposer(options: any = {}, VueI18nLegacy?: any): any {
>
const _isGlobal = __root === undefined
const flatJson = options.flatJson
const _ref = inBrowser ? ref : shallowRef

let _inheritLocale = isBoolean(options.inheritLocale)
? options.inheritLocale
: true

const _locale = ref<Locale>(
const _locale = _ref<Locale>(
// prettier-ignore
__root && _inheritLocale
? __root.locale.value
Expand All @@ -1872,7 +1873,7 @@ export function createComposer(options: any = {}, VueI18nLegacy?: any): any {
: DEFAULT_LOCALE
)

const _fallbackLocale = ref<FallbackLocale>(
const _fallbackLocale = _ref<FallbackLocale>(
// prettier-ignore
__root && _inheritLocale
? __root.fallbackLocale.value
Expand All @@ -1884,7 +1885,7 @@ export function createComposer(options: any = {}, VueI18nLegacy?: any): any {
: _locale.value
)

const _messages = ref<LocaleMessages<LocaleMessage<Message>>>(
const _messages = _ref(
getLocaleMessages<LocaleMessages<LocaleMessage<Message>>>(
_locale.value as Locale,
options
Expand All @@ -1893,7 +1894,7 @@ export function createComposer(options: any = {}, VueI18nLegacy?: any): any {

// prettier-ignore
const _datetimeFormats = !__LITE__
? ref<DateTimeFormatsType>(
? _ref<DateTimeFormatsType>(
isPlainObject(options.datetimeFormats)
? options.datetimeFormats
: { [_locale.value]: {} }
Expand All @@ -1902,12 +1903,12 @@ export function createComposer(options: any = {}, VueI18nLegacy?: any): any {

// prettier-ignore
const _numberFormats = !__LITE__
? ref<NumberFormatsType>(
? _ref<NumberFormatsType>(
isPlainObject(options.numberFormats)
? options.numberFormats
: { [_locale.value]: {} }
)
: /* #__PURE__*/ ref<NumberFormatsType>({})
: /* #__PURE__*/ _ref<NumberFormatsType>({})

// warning suppress options
// prettier-ignore
Expand Down

0 comments on commit a287a26

Please sign in to comment.