@@ -14,13 +14,24 @@ import { SimpleDeep, DeepAccessor } from "./simple-deep";
14
14
import { State , EqualityFunc } from "./state" ;
15
15
16
16
17
+ /**
18
+ *
19
+ * Represents a [keyed deep state](https://connective.dev/docs/deep#keyed-deep).
20
+ *
21
+ */
17
22
export class KeyedDeep extends SimpleDeep {
18
23
private _keyMap : KeyMap = { } ;
19
24
20
25
constructor ( state : State , keyfunc : KeyFunc ) ;
21
26
constructor ( accessor : DeepAccessor , keyfunc : KeyFunc , compare ?: EqualityFunc ) ;
22
27
constructor ( stateOrAccessor : State | DeepAccessor , keyfunc : KeyFunc , compare ?: EqualityFunc | undefined ) ;
23
-
28
+ /**
29
+ *
30
+ * @param stateOrAccessor underlying state of this deep state or a state tree accessor (for sub-states)
31
+ * @param keyfunc key function to be used to track entities within the state's value
32
+ * @param compare equality function used to detect changes. If state is passed as first argument this is ignored.
33
+ *
34
+ */
24
35
constructor ( stateOrAccessor : State | DeepAccessor , readonly keyfunc : KeyFunc , compare ?: EqualityFunc | undefined ) {
25
36
super ( stateOrAccessor , compare , {
26
37
inputs : [ 'value' ] ,
@@ -30,14 +41,23 @@ export class KeyedDeep extends SimpleDeep {
30
41
31
42
public key ( key : string | number ) : SimpleDeep ;
32
43
public key ( key : string | number , subkeyfunc : KeyFunc ) : KeyedDeep ;
44
+ /**
45
+ *
46
+ * Creates a sub-state bound to entity identified by given key. Entity `x` is
47
+ * said to be identified by key `k` if `state.keyfunc(x) === k`.
48
+ *
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.
51
+ *
52
+ */
33
53
public key ( key : string | number , subkeyfunc ?: KeyFunc ) : SimpleDeep | KeyedDeep {
34
54
let initialized = false ;
35
55
let _this = this ;
36
56
37
57
this . output ; // --> wire output before hand
38
58
39
59
let accessor : DeepAccessor = {
40
- initial : ( _this . _keyMap [ key ] || { item : undefined } ) . item
60
+ initial : ( _this . _keyMap [ key ] || { item : undefined } ) . item
41
61
|| ( Object . values ( this . value ) || [ ] ) . find ( ( i : any ) => this . keyfunc ( i ) == key ) ,
42
62
get : group ( _this . changes , _this . reemit ) . to ( map ( ( ) => ( _this . _keyMap [ key ] || { item : undefined } ) . item ) ) ,
43
63
set : sink ( ( v , context ) => {
@@ -67,11 +87,20 @@ export class KeyedDeep extends SimpleDeep {
67
87
else return new SimpleDeep ( accessor , this . state . compare ) ;
68
88
}
69
89
90
+ /**
91
+ *
92
+ * Returns a [pin](https://connective.dev/docs/pin) that reflects the reactive value of
93
+ * the index of entity identified by given key in the state's value. Entity `x` is said
94
+ * to be identified by key `k` if `state.keyfunc(x) === k`.
95
+ *
96
+ * @param key the key to identify target entity with
97
+ *
98
+ */
70
99
public index ( key : string | number ) {
71
100
let initial : string | number ;
72
101
73
102
if ( this . _keyMap [ key ] ) initial = this . _keyMap [ key ] . index ;
74
- else initial = ( ( Object . entries ( this . value ) || [ ] ) . find ( ( [ index , item ] ) => this . keyfunc ( item ) == key )
103
+ else initial = ( ( Object . entries ( this . value ) || [ ] ) . find ( ( [ index , item ] ) => this . keyfunc ( item ) == key )
75
104
|| [ - 1 , undefined ] ) [ 0 ] ;
76
105
77
106
return group (
@@ -80,16 +109,47 @@ export class KeyedDeep extends SimpleDeep {
80
109
) . to ( pipe ( distinctUntilKeyChanged ( 'value' ) ) ) ;
81
110
}
82
111
112
+ /**
113
+ *
114
+ * Will bind the underlying state, and cause deep change-detection to happen upon
115
+ * changes of the state value. [Read this](https://connective.dev/docs/deep#change-detection)
116
+ * for more information on deep change-detection.
117
+ *
118
+ * If this is a sub-state, also enables up-propagation
119
+ * of state value, causing the parent state to pick up changes made to the value of this
120
+ * sub-state. [Read this](https://connective.dev/docs/deep#two-way-keyed) for more details
121
+ * and examples.
122
+ *
123
+ */
83
124
bind ( ) {
84
125
super . bind ( ) ;
85
126
this . track ( this . changes . subscribe ( ) ) ;
86
127
return this ;
87
128
}
88
129
130
+ /**
131
+ *
132
+ * Keys that entities within the value of the state are identified with. Entity
133
+ * `x` is said to be indetified with key `k` if `state.keyfunc(x) === k`.
134
+ *
135
+ * **WARNING** the keys will not be calculcated unless deep change-detection is active.
136
+ * You can ensure deep change-detection is active by subscribing on `.changes` or
137
+ * calling `.bind()`. [Read this](https://connective.dev/docs/deep#change-detection)
138
+ * for more information on deep change-detection.
139
+ *
140
+ */
89
141
public get keys ( ) {
90
- return Object . keys ( this . _keyMap ) ;
142
+ return Object . keys ( this . _keyMap ) ;
91
143
}
92
144
145
+ /**
146
+ *
147
+ * A [pin](https://connective.dev/docs/pin) that emits changes to this deep state's list value.
148
+ * These changes include entities being added to the list, removed from it or moved around in it.
149
+ * [Read this](https://connective.dev/docs/deep#change-detection) for more information on
150
+ * deep change-detection.
151
+ *
152
+ */
93
153
public get changes ( ) { return this . out ( 'changes' ) ; }
94
154
95
155
protected createOutput ( label : string ) : PinLike {
0 commit comments