Skip to content

Commit 5076b50

Browse files
committed
fixup: compilation issue with server
Signed-off-by: Todd Baert <[email protected]>
1 parent 216d220 commit 5076b50

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

libs/hooks/debounce/src/lib/debounce-hook.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import type { EvaluationDetails, BaseHook, HookContext } from '@openfeature/core';
22
import { DebounceHook } from './debounce-hook';
33
import type { Hook as WebSdkHook } from '@openfeature/web-sdk';
4+
import { OpenFeature as OpenFeatureWeb } from '@openfeature/web-sdk';
45
import type { Hook as ServerSdkHook } from '@openfeature/server-sdk';
6+
import { OpenFeature as OpenFeaturServer } from '@openfeature/server-sdk';
57

68
describe('DebounceHook', () => {
79
describe('caching', () => {
@@ -203,6 +205,23 @@ describe('DebounceHook', () => {
203205

204206
describe('SDK compatibility', () => {
205207
describe('web-sdk hooks', () => {
208+
it('should have type compatibility with API, client, evaluation', () => {
209+
const webSdkHook = {} as WebSdkHook;
210+
211+
const hook = new DebounceHook<number>(webSdkHook, {
212+
debounceTime: 60_000,
213+
maxCacheItems: 100,
214+
});
215+
216+
OpenFeatureWeb.addHooks(hook);
217+
const client = OpenFeatureWeb.getClient().addHooks(hook);
218+
client.getBooleanValue('flag', false, { hooks: [hook] });
219+
220+
// these expectations are silly, the real test here is making sure the above compiles
221+
expect(OpenFeatureWeb.getHooks().length).toEqual(1);
222+
expect(client.getHooks().length).toEqual(1);
223+
});
224+
206225
it('should debounce synchronous hooks', () => {
207226
const innerWebSdkHook: WebSdkHook = {
208227
before: jest.fn(),
@@ -239,6 +258,23 @@ describe('DebounceHook', () => {
239258
const contextKey = 'key';
240259
const contextValue = 'value';
241260
const evaluationContext = { [contextKey]: contextValue };
261+
it('should have type compatibility with API, client, evaluation', () => {
262+
const serverHook = {} as ServerSdkHook;
263+
264+
const hook = new DebounceHook<number>(serverHook, {
265+
debounceTime: 60_000,
266+
maxCacheItems: 100,
267+
});
268+
269+
OpenFeaturServer.addHooks(hook);
270+
const client = OpenFeaturServer.getClient().addHooks(hook);
271+
client.getBooleanValue('flag', false, {}, { hooks: [hook] });
272+
273+
// these expectations are silly, the real test here is making sure the above compiles
274+
expect(OpenFeaturServer.getHooks().length).toEqual(1);
275+
expect(client.getHooks().length).toEqual(1);
276+
});
277+
242278
it('should debounce synchronous hooks', () => {
243279
const innerServerSdkHook: ServerSdkHook = {
244280
before: jest.fn(() => {

libs/hooks/debounce/src/lib/debounce-hook.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,15 @@ export type Options = {
8181
* The cacheKeySupplier is used to generate a cache key for the hook, which is used to determine if the hook should be executed or skipped.
8282
* If no cache key supplier is provided for a stage, that stage will always run.
8383
*/
84-
export class DebounceHook<T extends FlagValue = FlagValue> implements BaseHook {
84+
export class DebounceHook<T extends FlagValue = FlagValue>
85+
implements
86+
BaseHook<
87+
T,
88+
Record<string, unknown>,
89+
Promise<EvaluationContext | void> | EvaluationContext | void,
90+
Promise<void> | void
91+
>
92+
{
8593
private readonly cache: FixedSizeExpiringCache<HookStagesEntry>;
8694
private readonly cacheErrors: boolean;
8795
private readonly cacheKeySupplier: Options['cacheKeySupplier'];
@@ -136,11 +144,11 @@ export class DebounceHook<T extends FlagValue = FlagValue> implements BaseHook {
136144
);
137145
}
138146

139-
private maybeSkipAndCache(
147+
private maybeSkipAndCache<T extends Promise<EvaluationContext | void> | EvaluationContext | void>(
140148
stage: Stage,
141149
keyGenCallback: () => string | null | undefined,
142-
hookCallback: () => Promise<EvaluationContext | void> | EvaluationContext | void,
143-
) {
150+
hookCallback: () => T,
151+
): T | void {
144152
// the cache key is a concatenation of the result of calling keyGenCallback and the stage
145153
let dynamicKey: string | null | undefined;
146154

@@ -172,9 +180,9 @@ export class DebounceHook<T extends FlagValue = FlagValue> implements BaseHook {
172180
// already ran this stage for this key and is still in the debounce period
173181
if (typeof cachedStageResult === 'object') {
174182
// we have a cached context to return
175-
return cachedStageResult;
183+
return cachedStageResult as T;
176184
}
177-
return;
185+
return; // cached run with void return
178186
}
179187
}
180188

0 commit comments

Comments
 (0)