@@ -10,7 +10,7 @@ import { value } from "../pin/value";
10
10
import { pipe } from "../pin/pipe" ;
11
11
import { PinLike } from "../pin/pin-like" ;
12
12
13
- import { SimpleDeep , DeepAccessor } from "./simple-deep" ;
13
+ import { SimpleDeep , DeepAccessor , DeepChildFactory } from "./simple-deep" ;
14
14
import { State , EqualityFunc } from "./state" ;
15
15
16
16
@@ -40,21 +40,22 @@ export class KeyedDeep extends SimpleDeep {
40
40
}
41
41
42
42
public key ( key : string | number ) : SimpleDeep ;
43
- public key ( key : string | number , subkeyfunc : KeyFunc ) : KeyedDeep ;
43
+ public key < T extends SimpleDeep > ( key : string | number , factory : DeepChildFactory < T > ) : T ;
44
44
/**
45
45
*
46
46
* Creates a sub-state bound to entity identified by given key. Entity `x` is
47
47
* said to be identified by key `k` if `state.keyfunc(x) === k`.
48
48
*
49
49
* @param key the identifier of the entity to track
50
- * @param subkeyfunc a key function for the sub-state. provide IF the sub-state also needs to be keyed.
50
+ * @param factory the factory function to be used to construct the sub-state
51
51
*
52
52
*/
53
- public key ( key : string | number , subkeyfunc ?: KeyFunc ) : SimpleDeep | KeyedDeep {
53
+ public key < T extends SimpleDeep > ( key : string | number , factory ?: DeepChildFactory < T > ) : SimpleDeep | T {
54
54
let initialized = false ;
55
55
let _this = this ;
56
+ let _factory = factory || ( ( accessor : DeepAccessor , compare : EqualityFunc ) => new SimpleDeep ( accessor , compare ) ) ;
56
57
57
- let accessor : DeepAccessor = {
58
+ return _factory ( {
58
59
initial : ( _this . _keyMap [ key ] || { item : undefined } ) . item
59
60
|| ( Object . values ( this . value ) || [ ] ) . find ( ( i : any ) => this . keyfunc ( i ) == key ) ,
60
61
get : group ( _this . changes , _this . reemit ) . to ( map ( ( ) => ( _this . _keyMap [ key ] || { item : undefined } ) . item ) ) ,
@@ -79,10 +80,7 @@ export class KeyedDeep extends SimpleDeep {
79
80
} catch ( err ) { }
80
81
} ) ,
81
82
bind ( ) { return this . set . subscribe ( ) ; } ,
82
- }
83
-
84
- if ( subkeyfunc ) return new KeyedDeep ( accessor , subkeyfunc , this . state . compare ) ;
85
- else return new SimpleDeep ( accessor , this . state . compare ) ;
83
+ } , this . state . compare ) ;
86
84
}
87
85
88
86
/**
0 commit comments