@@ -3,7 +3,7 @@ import { Base64 } from "js-base64";
3
3
import wefetch from "wefetch" ;
4
4
import { FPUser } from "./FPUser" ;
5
5
import StorageProvider from "./localstorage" ;
6
- import { FPToggleDetail , IParams , IOption , IStorageProvider } from "./types" ;
6
+ import { FPToggleDetail , IParams , FPOptions , FPStorageProvider } from "./types" ;
7
7
8
8
const PKG_VERSION = "SDK_VERSION" ;
9
9
const UA = "WECHAT_MINIPROGRAM/" + PKG_VERSION ;
@@ -22,6 +22,10 @@ const EVENTS = {
22
22
CACHE_READY : "cache_ready"
23
23
} ;
24
24
25
+ /**
26
+ * You can obtainan a client of FeatureProbe,
27
+ * which provides access to all of the SDK's functionality.
28
+ */
25
29
class FeatureProbe extends TinyEmitter {
26
30
private togglesUrl : string ;
27
31
private eventsUrl : string ;
@@ -33,7 +37,7 @@ class FeatureProbe extends TinyEmitter {
33
37
private timeoutTimer ?: NodeJS . Timeout ;
34
38
private readyPromise : null | Promise < void > ;
35
39
private status : string ;
36
- private storage : IStorageProvider ;
40
+ private storage : FPStorageProvider ;
37
41
private timeoutInterval : number ;
38
42
39
43
constructor ( ) {
@@ -51,6 +55,9 @@ class FeatureProbe extends TinyEmitter {
51
55
this . readyPromise = null ;
52
56
}
53
57
58
+ /**
59
+ * Initialize the FeatureProbe client.
60
+ */
54
61
public init ( {
55
62
remoteUrl,
56
63
togglesUrl,
@@ -59,7 +66,7 @@ class FeatureProbe extends TinyEmitter {
59
66
user,
60
67
refreshInterval = 1000 ,
61
68
timeoutInterval = 10000 ,
62
- } : IOption ) {
69
+ } : FPOptions ) {
63
70
if ( ! clientSdkKey ) {
64
71
throw new Error ( "clientSdkKey is required" ) ;
65
72
}
@@ -87,6 +94,9 @@ class FeatureProbe extends TinyEmitter {
87
94
this . timeoutInterval = timeoutInterval ;
88
95
}
89
96
97
+ /**
98
+ * Start the FeatureProbe client.
99
+ */
90
100
public async start ( ) {
91
101
this . timeoutTimer = setTimeout ( ( ) => {
92
102
if ( this . status === STATUS . PENDING ) {
@@ -108,13 +118,23 @@ class FeatureProbe extends TinyEmitter {
108
118
}
109
119
}
110
120
121
+ /**
122
+ * Stop the FeatureProbe client, once the client has been stopped,
123
+ * SDK will no longer listen for toggle changes or send metrics to Server.
124
+ */
111
125
public stop ( ) {
112
126
clearInterval ( this . timer ) ;
113
127
clearTimeout ( this . timeoutTimer ) ;
114
128
this . timeoutTimer = undefined ;
115
129
this . timer = undefined ;
116
130
}
117
131
132
+ /**
133
+ * Returns a Promise which tracks the client's ready state.
134
+ *
135
+ * The Promise will be resolved if the client successfully get toggles from the server
136
+ * or ejected if client error get toggles from the server until `timeoutInterval` countdown reaches.
137
+ */
118
138
public waitUntilReady ( ) : Promise < void > {
119
139
if ( this . readyPromise ) {
120
140
return this . readyPromise ;
@@ -146,50 +166,140 @@ class FeatureProbe extends TinyEmitter {
146
166
return this . readyPromise ;
147
167
}
148
168
169
+ /**
170
+ * Determines the return `boolean` value of a toggle for the current user.
171
+ *
172
+ *
173
+ * @param key
174
+ * The unique key of the toggle.
175
+ * @param defaultValue
176
+ * The default value of the toggle, to be used if the value is not available from FeatureProbe.
177
+ */
149
178
public boolValue ( key : string , defaultValue : boolean ) : boolean {
150
179
return this . toggleValue ( key , defaultValue , "boolean" ) ;
151
180
}
152
181
182
+ /**
183
+ * Determines the return `number` value of a toggle for the current user.
184
+ *
185
+ *
186
+ * @param key
187
+ * The unique key of the toggle.
188
+ * @param defaultValue
189
+ * The default value of the toggle, to be used if the value is not available from FeatureProbe.
190
+ */
153
191
public numberValue ( key : string , defaultValue : number ) : number {
154
192
return this . toggleValue ( key , defaultValue , "number" ) ;
155
193
}
156
194
195
+ /**
196
+ * Determines the return `string` value of a toggle for the current user.
197
+ *
198
+ *
199
+ * @param key
200
+ * The unique key of the toggle.
201
+ * @param defaultValue
202
+ * The default value of the toggle, to be used if the value is not available from FeatureProbe.
203
+ */
157
204
public stringValue ( key : string , defaultValue : string ) : string {
158
205
return this . toggleValue ( key , defaultValue , "string" ) ;
159
206
}
160
207
208
+ /**
209
+ * Determines the return `json` value of a toggle for the current user.
210
+ *
211
+ *
212
+ * @param key
213
+ * The unique key of the toggle.
214
+ * @param defaultValue
215
+ * The default value of the toggle, to be used if the value is not available from FeatureProbe.
216
+ */
161
217
public jsonValue ( key : string , defaultValue : object ) : object {
162
218
return this . toggleValue ( key , defaultValue , "object" ) ;
163
219
}
164
220
221
+ /**
222
+ * Determines the return `boolean` value of a toggle for the current user, along with information about how it was calculated.
223
+ *
224
+ *
225
+ * @param key
226
+ * The unique key of the toggle.
227
+ * @param defaultValue
228
+ * The default value of the toggle, to be used if the value is not available from FeatureProbe.
229
+ */
165
230
public boolDetail ( key : string , defaultValue : boolean ) : FPToggleDetail {
166
231
return this . toggleDetail ( key , defaultValue , "boolean" ) ;
167
232
}
168
233
234
+ /**
235
+ * Determines the return `number` value of a toggle for the current user, along with information about how it was calculated.
236
+ *
237
+ *
238
+ * @param key
239
+ * The unique key of the toggle.
240
+ * @param defaultValue
241
+ * The default value of the toggle, to be used if the value is not available from FeatureProbe.
242
+ */
169
243
public numberDetail ( key : string , defaultValue : number ) : FPToggleDetail {
170
244
return this . toggleDetail ( key , defaultValue , "number" ) ;
171
245
}
172
246
247
+ /**
248
+ * Determines the return `string` value of a toggle for the current user, along with information about how it was calculated.
249
+ *
250
+ *
251
+ * @param key
252
+ * The unique key of the toggle.
253
+ * @param defaultValue
254
+ * The default value of the toggle, to be used if the value is not available from FeatureProbe.
255
+ */
173
256
public stringDetail ( key : string , defaultValue : string ) : FPToggleDetail {
174
257
return this . toggleDetail ( key , defaultValue , "string" ) ;
175
258
}
176
259
260
+ /**
261
+ * Determines the return `json` value of a toggle for the current user, along with information about how it was calculated.
262
+ *
263
+ *
264
+ * @param key
265
+ * The unique key of the toggle.
266
+ * @param defaultValue
267
+ * The default value of the toggle, to be used if the value is not available from FeatureProbe.
268
+ */
177
269
public jsonDetail ( key : string , defaultValue : object ) : FPToggleDetail {
178
270
return this . toggleDetail ( key , defaultValue , "object" ) ;
179
271
}
180
272
273
+ /**
274
+ * Returns an object of all available toggles' details to the current user.
275
+ */
181
276
public allToggles ( ) : { [ key : string ] : FPToggleDetail } | undefined {
182
277
return Object . assign ( { } , this . toggles ) ;
183
278
}
184
279
280
+ /**
281
+ * Returns the current user.
282
+ *
283
+ * This is the user that was most recently passed to [[identifyUser]], or, if [[identifyUser]] has never
284
+ * been called, the initial user specified when the client was created.
285
+ */
185
286
public getUser ( ) : FPUser {
186
287
return this . user ;
187
288
}
188
289
290
+ /**
291
+ * Changing the current user to FeatureProbe.
292
+ *
293
+ * @param user
294
+ * A new FPUser instance.
295
+ */
189
296
public identifyUser ( user : FPUser ) {
190
297
this . user = user ;
191
298
}
192
299
300
+ /**
301
+ * Logout the current user, change the current user to an anonymous user.
302
+ */
193
303
public logout ( ) {
194
304
const user = new FPUser ( ) ;
195
305
this . identifyUser ( user ) ;
@@ -380,6 +490,9 @@ class FeatureProbe extends TinyEmitter {
380
490
}
381
491
}
382
492
493
+ /**
494
+ * The client of the FeatureProbe.
495
+ */
383
496
const featureProbeClient = new FeatureProbe ( ) ;
384
497
385
498
export { FeatureProbe , featureProbeClient } ;
0 commit comments