(experimentId: ExperimentId)
| 262 | } |
| 263 | |
| 264 | private async refreshExperimentImpl(experimentId: ExperimentId): Promise<void> { |
| 265 | const client = this.telemetryService.getPostHogClient(); |
| 266 | const distinctId = this.telemetryService.getDistinctId(); |
| 267 | assert(client, "PostHog client must exist when remote evaluation is enabled"); |
| 268 | assert(distinctId, "distinctId must exist when remote evaluation is enabled"); |
| 269 | |
| 270 | const flagKey = this.getFlagKey(experimentId); |
| 271 | |
| 272 | try { |
| 273 | const value = await client.getFeatureFlag(flagKey, distinctId); |
| 274 | if (typeof value !== "string" && typeof value !== "boolean") { |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | const cached: CachedVariant = { |
| 279 | value, |
| 280 | fetchedAtMs: Date.now(), |
| 281 | source: "posthog", |
| 282 | }; |
| 283 | |
| 284 | this.cachedVariants.set(experimentId, cached); |
| 285 | if (!this.overrides.has(experimentId)) { |
| 286 | this.telemetryService.setFeatureFlagVariant(flagKey, value); |
| 287 | } |
| 288 | |
| 289 | await this.writeCacheToDisk(); |
| 290 | } catch (error) { |
| 291 | log.debug("Failed to refresh experiment from PostHog", { |
| 292 | experimentId, |
| 293 | error: getErrorMessage(error), |
| 294 | }); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | private maybeRefreshInBackground(experimentId: ExperimentId): void { |
| 299 | if (!this.isExperimentSupported(experimentId)) { |
no test coverage detected