Skip to content

Commit c815bc8

Browse files
authored
fix: provider js-doc improvments (#506)
In 3 event provider implementations, [flagd-java](open-feature/java-sdk-contrib#361 (review)), [go-feature-flag-web](open-feature/js-sdk-contrib#474 (comment)), and even my own implementation of flagd-js, authors have forgotten to properly implement `getState` (which means the SDK assumes the provider is already `READY` and doesn't run `initialize()`.) This is an easy pitfall. To help with this, I'm adding doc. We may be able to improve the interfaces to help with this, but I think this is a safe and helpful change for now. Relates to: open-feature/java-sdk#531 Signed-off-by: Todd Baert <[email protected]>
1 parent 9aca1cd commit c815bc8

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

packages/shared/src/provider/provider.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,44 @@ import { OpenFeatureEventEmitter } from '../events';
22
import { Metadata } from '../types';
33
import { EvaluationContext } from '../evaluation';
44

5+
/**
6+
* The state of the provider.
7+
*/
58
export enum ProviderStatus {
9+
/**
10+
* The provider has not been initialized and cannot yet evaluate flags.
11+
*/
612
NOT_READY = 'NOT_READY',
13+
14+
/**
15+
* The provider is ready to resolve flags.
16+
*/
717
READY = 'READY',
18+
19+
/**
20+
* The provider is in an error state and unable to evaluate flags.
21+
*/
822
ERROR = 'ERROR',
923
}
1024

25+
/**
26+
* Static data about the provider.
27+
*/
1128
export interface ProviderMetadata extends Metadata {
1229
readonly name: string;
1330
}
1431

1532
export interface CommonProvider {
1633
readonly metadata: ProviderMetadata;
34+
35+
/**
36+
* Returns a representation of the current readiness of the provider.
37+
* If the provider needs to be initialized, it should return {@link ProviderStatus.READY}.
38+
* If the provider is in an error state, it should return {@link ProviderStatus.ERROR}.
39+
* If the provider is functioning normally, it should return {@link ProviderStatus.NOT_READY}.
40+
*
41+
* _Providers which do not implement this method are assumed to be ready immediately._
42+
*/
1743
readonly status?: ProviderStatus;
1844

1945
/**
@@ -22,15 +48,19 @@ export interface CommonProvider {
2248
*/
2349
events?: OpenFeatureEventEmitter;
2450

51+
/**
52+
* A function used to shut down the provider.
53+
* Called when this provider is replaced with a new one, or when the OpenFeature is shut down.
54+
*/
2555
onClose?(): Promise<void>;
2656

2757
/**
28-
* A handler function used to setup the provider.
29-
* Called by the SDK after the provider is set.
58+
* A function used to setup the provider.
59+
* Called by the SDK after the provider is set if the provider's status is {@link ProviderStatus.NOT_READY}.
3060
* When the returned promise resolves, the SDK fires the ProviderEvents.Ready event.
3161
* If the returned promise rejects, the SDK fires the ProviderEvents.Error event.
3262
* Use this function to perform any context-dependent setup within the provider.
3363
* @param context
3464
*/
3565
initialize?(context?: EvaluationContext): Promise<void>;
36-
}
66+
}

0 commit comments

Comments
 (0)