@@ -554,45 +554,39 @@ export function createComposer<
554
554
_context . missing = _runtimeMissing
555
555
}
556
556
557
- function defineComputed < T , U = T > (
557
+ function wrapWithDeps < T , U = T > (
558
558
fn : ( context : unknown ) => unknown ,
559
559
argumentParser : ( ) => string ,
560
560
warnType : ComposerWarnType ,
561
561
fallbackSuccess : ( root : Composer < T > & ComposerInternal ) => U ,
562
562
fallbackFail : ( key : string ) => U ,
563
563
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
+ )
589
575
}
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
+ }
591
585
}
592
586
593
587
// t
594
588
function t ( ...args : unknown [ ] ) : string {
595
- return defineComputed < string > (
589
+ return wrapWithDeps < string > (
596
590
context =>
597
591
translate < Messages , string > (
598
592
context as RuntimeTranslationContext < Messages , string > ,
@@ -603,12 +597,12 @@ export function createComposer<
603
597
root => root . t ( ...args ) ,
604
598
key => key ,
605
599
val => isString ( val )
606
- ) . value
600
+ )
607
601
}
608
602
609
603
// d
610
604
function d ( ...args : unknown [ ] ) : string {
611
- return defineComputed < string > (
605
+ return wrapWithDeps < string > (
612
606
context =>
613
607
datetime < DateTimeFormats , string > (
614
608
context as RuntimeDateTimeContext < DateTimeFormats , string > ,
@@ -619,12 +613,12 @@ export function createComposer<
619
613
root => root . d ( ...args ) ,
620
614
( ) => MISSING_RESOLVE_VALUE ,
621
615
val => isString ( val )
622
- ) . value
616
+ )
623
617
}
624
618
625
619
// n
626
620
function n ( ...args : unknown [ ] ) : string {
627
- return defineComputed < string > (
621
+ return wrapWithDeps < string > (
628
622
context =>
629
623
number < NumberFormats , string > (
630
624
context as RuntimeNumberContext < NumberFormats , string > ,
@@ -635,7 +629,7 @@ export function createComposer<
635
629
root => root . n ( ...args ) ,
636
630
( ) => MISSING_RESOLVE_VALUE ,
637
631
val => isString ( val )
638
- ) . value
632
+ )
639
633
}
640
634
641
635
// for custom processor
@@ -654,7 +648,7 @@ export function createComposer<
654
648
655
649
// __transrateVNode, using for `i18n-t` component
656
650
function __transrateVNode ( ...args : unknown [ ] ) : VNodeArrayChildren {
657
- return defineComputed < VNode , VNodeArrayChildren > (
651
+ return wrapWithDeps < VNode , VNodeArrayChildren > (
658
652
context => {
659
653
let ret : unknown
660
654
try {
@@ -671,33 +665,33 @@ export function createComposer<
671
665
root => root . __transrateVNode ( ...args ) ,
672
666
key => [ createVNode ( Text , null , key , 0 ) ] ,
673
667
val => isArray ( val )
674
- ) . value
668
+ )
675
669
}
676
670
677
671
// __numberParts, using for `i18n-n` component
678
672
function __numberParts ( ...args : unknown [ ] ) : string | Intl . NumberFormatPart [ ] {
679
- return defineComputed < string | Intl . NumberFormatPart [ ] > (
673
+ return wrapWithDeps < string | Intl . NumberFormatPart [ ] > (
680
674
context => number ( context as RuntimeContext < Messages , string > , ...args ) ,
681
675
( ) => parseNumberArgs ( ...args ) [ 0 ] ,
682
676
'number format' ,
683
677
root => root . __numberParts ( ...args ) ,
684
678
( ) => [ ] ,
685
679
val => isString ( val ) || isArray ( val )
686
- ) . value
680
+ )
687
681
}
688
682
689
683
// __datetimeParts, using for `i18n-d` component
690
684
function __datetimeParts (
691
685
...args : unknown [ ]
692
686
) : string | Intl . DateTimeFormatPart [ ] {
693
- return defineComputed < string | Intl . DateTimeFormatPart [ ] > (
687
+ return wrapWithDeps < string | Intl . DateTimeFormatPart [ ] > (
694
688
context => datetime ( context as RuntimeContext < Messages , string > , ...args ) ,
695
689
( ) => parseDateTimeArgs ( ...args ) [ 0 ] ,
696
690
'datetime format' ,
697
691
root => root . __datetimeParts ( ...args ) ,
698
692
( ) => [ ] ,
699
693
val => isString ( val ) || isArray ( val )
700
- ) . value
694
+ )
701
695
}
702
696
703
697
// getLocaleMessage
0 commit comments