@@ -3,7 +3,7 @@ import { Base64 } from "js-base64";
33import wefetch from "wefetch" ;
44import { FPUser } from "./FPUser" ;
55import StorageProvider from "./localstorage" ;
6- import { FPToggleDetail , IParams , IOption , IStorageProvider } from "./types" ;
6+ import { FPToggleDetail , IParams , FPOptions , FPStorageProvider } from "./types" ;
77
88const PKG_VERSION = "SDK_VERSION" ;
99const UA = "WECHAT_MINIPROGRAM/" + PKG_VERSION ;
@@ -22,6 +22,10 @@ const EVENTS = {
2222 CACHE_READY : "cache_ready"
2323} ;
2424
25+ /**
26+ * You can obtainan a client of FeatureProbe,
27+ * which provides access to all of the SDK's functionality.
28+ */
2529class FeatureProbe extends TinyEmitter {
2630 private togglesUrl : string ;
2731 private eventsUrl : string ;
@@ -33,7 +37,7 @@ class FeatureProbe extends TinyEmitter {
3337 private timeoutTimer ?: NodeJS . Timeout ;
3438 private readyPromise : null | Promise < void > ;
3539 private status : string ;
36- private storage : IStorageProvider ;
40+ private storage : FPStorageProvider ;
3741 private timeoutInterval : number ;
3842
3943 constructor ( ) {
@@ -51,6 +55,9 @@ class FeatureProbe extends TinyEmitter {
5155 this . readyPromise = null ;
5256 }
5357
58+ /**
59+ * Initialize the FeatureProbe client.
60+ */
5461 public init ( {
5562 remoteUrl,
5663 togglesUrl,
@@ -59,7 +66,7 @@ class FeatureProbe extends TinyEmitter {
5966 user,
6067 refreshInterval = 1000 ,
6168 timeoutInterval = 10000 ,
62- } : IOption ) {
69+ } : FPOptions ) {
6370 if ( ! clientSdkKey ) {
6471 throw new Error ( "clientSdkKey is required" ) ;
6572 }
@@ -87,6 +94,9 @@ class FeatureProbe extends TinyEmitter {
8794 this . timeoutInterval = timeoutInterval ;
8895 }
8996
97+ /**
98+ * Start the FeatureProbe client.
99+ */
90100 public async start ( ) {
91101 this . timeoutTimer = setTimeout ( ( ) => {
92102 if ( this . status === STATUS . PENDING ) {
@@ -108,13 +118,23 @@ class FeatureProbe extends TinyEmitter {
108118 }
109119 }
110120
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+ */
111125 public stop ( ) {
112126 clearInterval ( this . timer ) ;
113127 clearTimeout ( this . timeoutTimer ) ;
114128 this . timeoutTimer = undefined ;
115129 this . timer = undefined ;
116130 }
117131
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+ */
118138 public waitUntilReady ( ) : Promise < void > {
119139 if ( this . readyPromise ) {
120140 return this . readyPromise ;
@@ -146,50 +166,140 @@ class FeatureProbe extends TinyEmitter {
146166 return this . readyPromise ;
147167 }
148168
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+ */
149178 public boolValue ( key : string , defaultValue : boolean ) : boolean {
150179 return this . toggleValue ( key , defaultValue , "boolean" ) ;
151180 }
152181
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+ */
153191 public numberValue ( key : string , defaultValue : number ) : number {
154192 return this . toggleValue ( key , defaultValue , "number" ) ;
155193 }
156194
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+ */
157204 public stringValue ( key : string , defaultValue : string ) : string {
158205 return this . toggleValue ( key , defaultValue , "string" ) ;
159206 }
160207
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+ */
161217 public jsonValue ( key : string , defaultValue : object ) : object {
162218 return this . toggleValue ( key , defaultValue , "object" ) ;
163219 }
164220
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+ */
165230 public boolDetail ( key : string , defaultValue : boolean ) : FPToggleDetail {
166231 return this . toggleDetail ( key , defaultValue , "boolean" ) ;
167232 }
168233
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+ */
169243 public numberDetail ( key : string , defaultValue : number ) : FPToggleDetail {
170244 return this . toggleDetail ( key , defaultValue , "number" ) ;
171245 }
172246
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+ */
173256 public stringDetail ( key : string , defaultValue : string ) : FPToggleDetail {
174257 return this . toggleDetail ( key , defaultValue , "string" ) ;
175258 }
176259
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+ */
177269 public jsonDetail ( key : string , defaultValue : object ) : FPToggleDetail {
178270 return this . toggleDetail ( key , defaultValue , "object" ) ;
179271 }
180272
273+ /**
274+ * Returns an object of all available toggles' details to the current user.
275+ */
181276 public allToggles ( ) : { [ key : string ] : FPToggleDetail } | undefined {
182277 return Object . assign ( { } , this . toggles ) ;
183278 }
184279
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+ */
185286 public getUser ( ) : FPUser {
186287 return this . user ;
187288 }
188289
290+ /**
291+ * Changing the current user to FeatureProbe.
292+ *
293+ * @param user
294+ * A new FPUser instance.
295+ */
189296 public identifyUser ( user : FPUser ) {
190297 this . user = user ;
191298 }
192299
300+ /**
301+ * Logout the current user, change the current user to an anonymous user.
302+ */
193303 public logout ( ) {
194304 const user = new FPUser ( ) ;
195305 this . identifyUser ( user ) ;
@@ -380,6 +490,9 @@ class FeatureProbe extends TinyEmitter {
380490 }
381491}
382492
493+ /**
494+ * The client of the FeatureProbe.
495+ */
383496const featureProbeClient = new FeatureProbe ( ) ;
384497
385498export { FeatureProbe , featureProbeClient } ;
0 commit comments