@@ -451,7 +451,7 @@ const useAsyncDeepState = (initialValue, {watch= true}= {}) => {
451
451
Object . assign ( current , {
452
452
state,
453
453
oldState : null ,
454
- callbacks : [ ]
454
+ callbacks : new Map ( )
455
455
} )
456
456
}
457
457
@@ -462,7 +462,7 @@ const useAsyncDeepState = (initialValue, {watch= true}= {}) => {
462
462
watch && useEffect ( ( ) => {
463
463
const data = [ state , current . oldState ] ;
464
464
current . callbacks . forEach ( cb => cb ( data ) ) ;
465
- current . callbacks . length = 0 ;
465
+ current . callbacks . clear ( ) ;
466
466
current . oldState = Object . assign ( { } , current . state ) ;
467
467
} , [ state ] ) ;
468
468
@@ -474,7 +474,7 @@ const useAsyncDeepState = (initialValue, {watch= true}= {}) => {
474
474
* @returns {Promise<*>|undefined }
475
475
*/
476
476
function ( newState ) {
477
- const setter = ( cb ) => {
477
+ const setter = ( scope , cb ) => {
478
478
if ( typeof newState === 'function' ) {
479
479
newState = newState ( current . state ) ;
480
480
}
@@ -485,11 +485,14 @@ const useAsyncDeepState = (initialValue, {watch= true}= {}) => {
485
485
486
486
setState ( ( state ) => {
487
487
if ( newState == null || isEqualObjects ( Object . assign ( current . state , newState ) , current . oldState ) ) {
488
- cb && cb ( [ state , current . oldState ] ) ;
488
+ scope && cb ( [ state , current . oldState ] ) ;
489
489
return state ;
490
490
}
491
491
492
- cb && current . callbacks . push ( cb ) ;
492
+ if ( scope ) {
493
+ current . callbacks . set ( scope , cb ) ;
494
+ scope . onDone ( ( ) => current . callbacks . delete ( scope ) )
495
+ }
493
496
494
497
return Object . create ( current . state )
495
498
} ) ;
@@ -500,27 +503,30 @@ const useAsyncDeepState = (initialValue, {watch= true}= {}) => {
500
503
return ;
501
504
}
502
505
503
- return new CPromise ( ( resolve ) => {
504
- setter ( resolve ) ;
506
+ return new CPromise ( ( resolve , reject , scope ) => {
507
+ setter ( scope , resolve ) ;
505
508
} ) ;
506
509
}
507
510
]
508
511
}
509
512
510
513
const useAsyncWatcher = ( ...value ) => {
511
514
const ref = useRef ( {
512
- callbacks : [ ] ,
513
515
oldValue : value
514
516
} ) ;
515
517
518
+ if ( ! ref . current . callbacks ) {
519
+ ref . current . callbacks = new Map ( )
520
+ }
521
+
516
522
const multiple = value . length > 1 ;
517
523
518
524
useEffect ( ( ) => {
519
525
const { current} = ref ;
520
526
const data = multiple ? value . map ( ( value , index ) => [ value , current . oldValue [ index ] ] ) :
521
527
[ value [ 0 ] , current . oldValue [ 0 ] ] ;
522
528
current . callbacks . forEach ( cb => cb ( data ) ) ;
523
- current . callbacks = [ ] ;
529
+ current . callbacks . clear ( ) ;
524
530
current . oldValue = value ;
525
531
} , value ) ;
526
532
@@ -529,14 +535,18 @@ const useAsyncWatcher = (...value) => {
529
535
* @returns {Promise }
530
536
*/
531
537
return ( grabPrevValue ) => {
532
- return new Promise ( ( resolve ) => {
533
- ref . current . callbacks . push ( entry => {
538
+ return new CPromise ( ( resolve , reject , scope ) => {
539
+ ref . current . callbacks . set ( scope , entry => {
534
540
if ( multiple ) {
535
541
resolve ( grabPrevValue ? entry : entry . map ( values => values [ 0 ] ) )
536
542
}
537
543
538
544
resolve ( grabPrevValue ? entry : entry [ 0 ] ) ;
539
545
} ) ;
546
+
547
+ scope . onDone ( ( ) => {
548
+ ref . current . callbacks . delete ( scope ) ;
549
+ } )
540
550
} ) ;
541
551
}
542
552
}
0 commit comments