Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve double polling for node-sdk #206

Merged
merged 1 commit into from
Mar 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion featurehub-javascript-client-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"eslint": "^8.0.0",
"eslint-plugin-filenames-simple": "0.7.0",
"mocha": "^9.1.4",
"sinon": "^15.0.1",
"sinon": "^15.0.1",
"ts-node": "^10.9.1",
"typescript": "^4.9.5"
},
Expand Down
3 changes: 3 additions & 0 deletions featurehub-javascript-node-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#### 1.4.0
- polling functionality changed to ensure double polling doesn't happen when the context is changed, but as node
relies on client-sdk, we have to bump the versions.
#### 1.3.3
- listeners were not being fired when added to contexts that matched strategies. [bugfix](https://github.com/featurehub-io/featurehub-javascript-sdk/issues/196)
- all the getX methods on the Context now have defaults, so you can say fhContext.getFlag("feature", false) and if it isn't set or doesn't exist, it will return false. This is an optional field so it doesn't break existing code. (feature)
Expand Down
33 changes: 31 additions & 2 deletions featurehub-javascript-node-sdk/app/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import {
FeatureHubPollingClient,
FeatureStateUpdate, FeatureUpdatePostManager, FeatureUpdater, GoogleAnalyticsApiClient, GoogleAnalyticsCollector,
FeatureStateUpdate,
FeatureUpdatePostManager,
FeatureUpdater,
GoogleAnalyticsApiClient,
GoogleAnalyticsCollector,
NodejsOptions,
PollingBase,
FeatureHubEventSourceClient,
PollingService, FeaturesFunction, FeatureEnvironmentCollection, EdgeFeatureHubConfig, FHLog, fhLog
PollingService,
FeaturesFunction,
FeatureEnvironmentCollection,
EdgeFeatureHubConfig,
FHLog,
fhLog,
PromiseLikeFunction, RejectLikeFunction
} from 'featurehub-javascript-client-sdk';
import { URL } from 'url';
import { RequestOptions } from 'https';
Expand All @@ -17,6 +27,11 @@ FeatureHubEventSourceClient.eventSourceProvider = (url, dict) => {
return new ES(url, dict);
};

interface PromiseLikeData {
resolve: PromiseLikeFunction;
reject: RejectLikeFunction;
}

export type ModifyRequestFunction = (options: RequestOptions) => void;

export class NodejsPollingService extends PollingBase implements PollingService {
Expand All @@ -32,10 +47,18 @@ export class NodejsPollingService extends PollingBase implements PollingService
}

public poll(): Promise<void> {
if (this._busy) {
return new Promise((resolve, reject) => {
this._outstandingPromises.push({ resolve: resolve, reject: reject } as PromiseLikeData);
});
}

if (this._stopped) {
return new Promise((resolve) => resolve());
}

this._busy = true;

return new Promise(((resolve, reject) => {
const http = this.uri.protocol === 'http:' ? require('http') : require('https');
let data = '';
Expand Down Expand Up @@ -71,10 +94,16 @@ export class NodejsPollingService extends PollingBase implements PollingService
this._etag = res.headers.etag;
this._callback(JSON.parse(data) as Array<FeatureEnvironmentCollection>);
this._stopped = (res.statusCode === 236);
this._busy = false;
this.resolveOutstanding();
resolve();
} else if (res.statusCode == 304) {
this._busy = false;
this.resolveOutstanding();
resolve();
} else {
this._busy = false;
this.rejectOutstanding(req.status);
reject(res.statusCode);
}
});
Expand Down
Loading