1
1
import { type Segments } from './types' ;
2
2
import { Doc } from './Doc' ;
3
3
import { Model , RootModel } from './Model' ;
4
+ import { JSONObject } from 'sharedb/lib/sharedb' ;
5
+ import { VerifyJsonWebKeyInput } from 'crypto' ;
6
+ import { Path , ReadonlyDeep , ShallowCopiedValue } from '../types' ;
4
7
var LocalDoc = require ( './LocalDoc' ) ;
5
8
var util = require ( '../util' ) ;
6
9
7
10
export class ModelCollections {
8
11
docs : Record < string , any > ;
9
12
}
10
- export class ModelData { }
11
- export class DocMap { }
12
- export class CollectionData { }
13
+
14
+ /** Root model data */
15
+ export class ModelData {
16
+ [ collectionName : string ] : CollectionData < JSONObject > ;
17
+ }
18
+
19
+ class DocMap {
20
+ [ id : string ] : Doc ;
21
+ }
22
+
23
+ /** Dictionary of document id to document data */
24
+ export class CollectionData < T extends JSONObject > {
25
+ [ id : string ] : T ;
26
+ }
13
27
14
28
declare module './Model' {
15
29
interface RootModel {
16
30
collections : ModelCollections ;
17
31
data : ModelData ;
18
32
}
19
- interface Model {
33
+ interface Model < T > {
20
34
destroy ( subpath ?: string ) : void ;
21
- get ( subpath ?: string ) : any ;
22
- get < T > ( subpath ?: string ) : T ;
23
- getCollection ( collecitonName : string ) : ModelCollections ;
24
- getCopy ( subpath : string ) : any ;
25
- getDeepCopy ( subpath : string ) : any ;
35
+
36
+ /**
37
+ * Gets the value located at this model's path or a relative subpath.
38
+ *
39
+ * If no value exists at the path, this returns `undefined`.
40
+ *
41
+ * _Note:_ The value is returned by reference, and object values should not
42
+ * be directly modified - use the Model mutator methods instead. The
43
+ * TypeScript compiler will enforce no direct modifications, but there are
44
+ * no runtime guards, which means JavaScript source code could still
45
+ * improperly make direct modifications.
46
+ *
47
+ * @param subpath
48
+ */
49
+ get < S > ( subpath : Path ) : ReadonlyDeep < S > | undefined ;
50
+ get ( ) : ReadonlyDeep < T > | undefined ;
51
+
52
+ getCollection ( collectionName : string ) : Collection < JSONObject > ;
53
+
54
+ /**
55
+ * Gets a shallow copy of the value located at this model's path or a relative
56
+ * subpath.
57
+ *
58
+ * If no value exists at the path, this returns `undefined`.
59
+ *
60
+ * @param subpath
61
+ */
62
+ getCopy < S > ( subpath : Path ) : ShallowCopiedValue < S > | undefined ;
63
+ getCopy ( ) : ShallowCopiedValue < T > | undefined ;
64
+
65
+ /**
66
+ * Gets a deep copy of the value located at this model's path or a relative
67
+ * subpath.
68
+ *
69
+ * If no value exists at the path, this returns `undefined`.
70
+ *
71
+ * @param subpath
72
+ */
73
+ getDeepCopy < S > ( subpath : Path ) : S | undefined ;
74
+ getDeepCopy ( ) : T | undefined ;
75
+
26
76
getDoc ( collecitonName : string , id : string ) : any | undefined ;
27
77
getOrCreateCollection ( name : string ) : Collection ;
28
78
getOrCreateDoc ( collectionName : string , id : string , data : any ) ;
@@ -47,28 +97,28 @@ Model.prototype.getDoc = function(collectionName, id) {
47
97
return collection && collection . docs [ id ] ;
48
98
} ;
49
99
50
- Model . prototype . get = function ( subpath ) {
100
+ Model . prototype . get = function < S > ( subpath ?: Path ) {
51
101
var segments = this . _splitPath ( subpath ) ;
52
- return this . _get ( segments ) ;
102
+ return this . _get ( segments ) as ReadonlyDeep < S > ;
53
103
} ;
54
104
55
105
Model . prototype . _get = function ( segments ) {
56
106
return util . lookup ( segments , this . root . data ) ;
57
107
} ;
58
108
59
- Model . prototype . getCopy = function ( subpath ) {
109
+ Model . prototype . getCopy = function < S > ( subpath ?: Path ) {
60
110
var segments = this . _splitPath ( subpath ) ;
61
- return this . _getCopy ( segments ) ;
111
+ return this . _getCopy ( segments ) as ReadonlyDeep < S > ;
62
112
} ;
63
113
64
114
Model . prototype . _getCopy = function ( segments ) {
65
115
var value = this . _get ( segments ) ;
66
116
return util . copy ( value ) ;
67
117
} ;
68
118
69
- Model . prototype . getDeepCopy = function ( subpath ) {
119
+ Model . prototype . getDeepCopy = function < S > ( subpath ?: Path ) {
70
120
var segments = this . _splitPath ( subpath ) ;
71
- return this . _getDeepCopy ( segments ) ;
121
+ return this . _getDeepCopy ( segments ) as S ;
72
122
} ;
73
123
74
124
Model . prototype . _getDeepCopy = function ( segments ) {
@@ -135,12 +185,12 @@ Model.prototype.destroy = function(subpath) {
135
185
}
136
186
} ;
137
187
138
- export class Collection {
188
+ export class Collection < T extends JSONObject = { } > {
139
189
model : RootModel ;
140
190
name : string ;
141
191
size : number ;
142
192
docs : DocMap ;
143
- data : CollectionData ;
193
+ data : CollectionData < T > ;
144
194
Doc : typeof Doc ;
145
195
146
196
constructor ( model : RootModel , name : string , docClass : typeof Doc ) {
@@ -149,7 +199,7 @@ export class Collection {
149
199
this . Doc = docClass ;
150
200
this . size = 0 ;
151
201
this . docs = new DocMap ( ) ;
152
- this . data = model . data [ name ] = new CollectionData ( ) ;
202
+ this . data = model . data [ name ] = new CollectionData < T > ( ) ;
153
203
}
154
204
155
205
/**
0 commit comments