Skip to content

Commit cf005d4

Browse files
authored
remove computed wrapping (#81)
1 parent b7ae4de commit cf005d4

File tree

1 file changed

+33
-39
lines changed

1 file changed

+33
-39
lines changed

src/composer.ts

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -554,45 +554,39 @@ export function createComposer<
554554
_context.missing = _runtimeMissing
555555
}
556556

557-
function defineComputed<T, U = T>(
557+
function wrapWithDeps<T, U = T>(
558558
fn: (context: unknown) => unknown,
559559
argumentParser: () => string,
560560
warnType: ComposerWarnType,
561561
fallbackSuccess: (root: Composer<T> & ComposerInternal) => U,
562562
fallbackFail: (key: string) => U,
563563
successCondition: (val: unknown) => boolean
564-
): ComputedRef<U> {
565-
return computed<U>(
566-
(): U => {
567-
const ret = fn(getRuntimeContext())
568-
if (isNumber(ret) && ret === NOT_REOSLVED) {
569-
const key = argumentParser()
570-
if (__DEV__ && _fallbackRoot && __root) {
571-
warn(
572-
getWarnMessage(I18nWarnCodes.FALLBACK_TO_ROOT, {
573-
key,
574-
type: warnType
575-
})
576-
)
577-
}
578-
return _fallbackRoot && __root
579-
? fallbackSuccess(
580-
(__root as unknown) as Composer<T> & ComposerInternal
581-
)
582-
: fallbackFail(key)
583-
} else if (successCondition(ret)) {
584-
return ret as U
585-
} else {
586-
/* istanbul ignore next */
587-
throw createI18nError(I18nErrorCodes.UNEXPECTED_RETURN_TYPE)
588-
}
564+
): U {
565+
const ret = fn(getRuntimeContext()) // track reactive dependency, see the getRuntimeContext
566+
if (isNumber(ret) && ret === NOT_REOSLVED) {
567+
const key = argumentParser()
568+
if (__DEV__ && _fallbackRoot && __root) {
569+
warn(
570+
getWarnMessage(I18nWarnCodes.FALLBACK_TO_ROOT, {
571+
key,
572+
type: warnType
573+
})
574+
)
589575
}
590-
)
576+
return _fallbackRoot && __root
577+
? fallbackSuccess((__root as unknown) as Composer<T> & ComposerInternal)
578+
: fallbackFail(key)
579+
} else if (successCondition(ret)) {
580+
return ret as U
581+
} else {
582+
/* istanbul ignore next */
583+
throw createI18nError(I18nErrorCodes.UNEXPECTED_RETURN_TYPE)
584+
}
591585
}
592586

593587
// t
594588
function t(...args: unknown[]): string {
595-
return defineComputed<string>(
589+
return wrapWithDeps<string>(
596590
context =>
597591
translate<Messages, string>(
598592
context as RuntimeTranslationContext<Messages, string>,
@@ -603,12 +597,12 @@ export function createComposer<
603597
root => root.t(...args),
604598
key => key,
605599
val => isString(val)
606-
).value
600+
)
607601
}
608602

609603
// d
610604
function d(...args: unknown[]): string {
611-
return defineComputed<string>(
605+
return wrapWithDeps<string>(
612606
context =>
613607
datetime<DateTimeFormats, string>(
614608
context as RuntimeDateTimeContext<DateTimeFormats, string>,
@@ -619,12 +613,12 @@ export function createComposer<
619613
root => root.d(...args),
620614
() => MISSING_RESOLVE_VALUE,
621615
val => isString(val)
622-
).value
616+
)
623617
}
624618

625619
// n
626620
function n(...args: unknown[]): string {
627-
return defineComputed<string>(
621+
return wrapWithDeps<string>(
628622
context =>
629623
number<NumberFormats, string>(
630624
context as RuntimeNumberContext<NumberFormats, string>,
@@ -635,7 +629,7 @@ export function createComposer<
635629
root => root.n(...args),
636630
() => MISSING_RESOLVE_VALUE,
637631
val => isString(val)
638-
).value
632+
)
639633
}
640634

641635
// for custom processor
@@ -654,7 +648,7 @@ export function createComposer<
654648

655649
// __transrateVNode, using for `i18n-t` component
656650
function __transrateVNode(...args: unknown[]): VNodeArrayChildren {
657-
return defineComputed<VNode, VNodeArrayChildren>(
651+
return wrapWithDeps<VNode, VNodeArrayChildren>(
658652
context => {
659653
let ret: unknown
660654
try {
@@ -671,33 +665,33 @@ export function createComposer<
671665
root => root.__transrateVNode(...args),
672666
key => [createVNode(Text, null, key, 0)],
673667
val => isArray(val)
674-
).value
668+
)
675669
}
676670

677671
// __numberParts, using for `i18n-n` component
678672
function __numberParts(...args: unknown[]): string | Intl.NumberFormatPart[] {
679-
return defineComputed<string | Intl.NumberFormatPart[]>(
673+
return wrapWithDeps<string | Intl.NumberFormatPart[]>(
680674
context => number(context as RuntimeContext<Messages, string>, ...args),
681675
() => parseNumberArgs(...args)[0],
682676
'number format',
683677
root => root.__numberParts(...args),
684678
() => [],
685679
val => isString(val) || isArray(val)
686-
).value
680+
)
687681
}
688682

689683
// __datetimeParts, using for `i18n-d` component
690684
function __datetimeParts(
691685
...args: unknown[]
692686
): string | Intl.DateTimeFormatPart[] {
693-
return defineComputed<string | Intl.DateTimeFormatPart[]>(
687+
return wrapWithDeps<string | Intl.DateTimeFormatPart[]>(
694688
context => datetime(context as RuntimeContext<Messages, string>, ...args),
695689
() => parseDateTimeArgs(...args)[0],
696690
'datetime format',
697691
root => root.__datetimeParts(...args),
698692
() => [],
699693
val => isString(val) || isArray(val)
700-
).value
694+
)
701695
}
702696

703697
// getLocaleMessage

0 commit comments

Comments
 (0)