Skip to content

Commit 3152850

Browse files
committed
refactor: waitUntilReady
1 parent 4f3a623 commit 3152850

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

example/index.html

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
color: #556ee6;
1515
}
1616
</style>
17-
<!-- <script type="text/javascript" src="https://unpkg.com/featureprobe-client-sdk-js@latest/dist/featureprobe-client-sdk-js.min.js"></script> -->
18-
<script type="text/javascript" src="../dist/featureprobe-client-sdk-js.min.js"></script>
17+
<script type="text/javascript" src="https://unpkg.com/featureprobe-client-sdk-js@latest/dist/featureprobe-client-sdk-js.min.js"></script>
18+
<!-- <script type="text/javascript" src="../dist/featureprobe-client-sdk-js.min.js"></script> -->
1919
</head>
2020

2121
<body>
@@ -40,10 +40,11 @@ <h3>boolean type</h3>
4040
user.with("userId", "1234567890");
4141

4242
const fpClient = new featureProbe.FeatureProbe({
43-
remoteUrl: "https://featureprobe.io/server",
43+
remoteUrl: "https://featureprobe.io/server/bcc",
4444
clientSdkKey: "client-25614c7e03e9cb49c0e96357b797b1e47e7f2dff",
4545
user,
4646
refreshInterval: 5000,
47+
timeoutInterval: 5000,
4748
});
4849

4950
fpClient.on("cache_ready", function() {
@@ -66,6 +67,14 @@ <h3>boolean type</h3>
6667
console.log("error");
6768
});
6869

70+
setTimeout(() => {
71+
fpClient.waitUntilReady().then(() => {
72+
console.log('wait until ready');
73+
}).catch(() => {
74+
console.log('wait until ready catch');
75+
})
76+
}, 5000);
77+
6978
fpClient.start();
7079
</script>
7180
</body>

setupJest.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
11
global.fetch = require('jest-fetch-mock');
2-
3-
process.on('unhandledRejection', (reason, promise) => {
4-
console.log('unhandledRejection', reason, promise);
5-
});

src/FeatureProbe.ts

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class FeatureProbe extends TinyEmitter {
3232
private toggles: { [key: string]: FPToggleDetail } | undefined;
3333
private timer?: any;
3434
private timeoutTimer?: any;
35-
private readyPromise: Promise<void>;
35+
private readyPromise: null | Promise<void>;
3636
private status: string;
3737
private timeoutInterval: number;
3838
private storage: IStorageProvider;
@@ -75,20 +75,7 @@ class FeatureProbe extends TinyEmitter {
7575
this.timeoutInterval = timeoutInterval;
7676
this.status = STATUS.PENDING;
7777
this.storage = new StorageProvider();
78-
79-
this.readyPromise = new Promise<void>((resolve, reject) => {
80-
const onReadyCallback = () => {
81-
this.off(EVENTS.READY, onReadyCallback);
82-
resolve();
83-
};
84-
const onErrorCallback = (err: Error) => {
85-
this.off(EVENTS.ERROR, onErrorCallback);
86-
reject(err);
87-
};
88-
89-
this.on(EVENTS.READY, onReadyCallback);
90-
this.on(EVENTS.ERROR, onErrorCallback);
91-
});
78+
this.readyPromise = null;
9279
}
9380

9481
public async start() {
@@ -120,6 +107,33 @@ class FeatureProbe extends TinyEmitter {
120107
}
121108

122109
public waitUntilReady(): Promise<void> {
110+
if (this.readyPromise) {
111+
return this.readyPromise;
112+
}
113+
114+
if (this.status === STATUS.READY) {
115+
return Promise.resolve();
116+
}
117+
118+
if (this.status === STATUS.ERROR) {
119+
return Promise.reject();
120+
}
121+
122+
this.readyPromise = new Promise((resolve, reject) => {
123+
const onReadyCallback = () => {
124+
this.off(EVENTS.READY, onReadyCallback);
125+
resolve();
126+
};
127+
128+
const onErrorCallback = () => {
129+
this.off(EVENTS.ERROR, onErrorCallback);
130+
reject();
131+
};
132+
133+
this.on(EVENTS.READY, onReadyCallback);
134+
this.on(EVENTS.ERROR, onErrorCallback);
135+
});
136+
123137
return this.readyPromise;
124138
}
125139

0 commit comments

Comments
 (0)