Skip to content

Commit d073cf7

Browse files
committed
fixup: improve docs, param names
Signed-off-by: Todd Baert <[email protected]>
1 parent 5aa11ce commit d073cf7

File tree

3 files changed

+30
-27
lines changed

3 files changed

+30
-27
lines changed

libs/hooks/debounce/README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,20 @@ NOTE: if you're using the React or Angular SDKs, you don't need to directly inst
2323

2424
The hook maintains a simple expiring cache with a fixed max size and keeps a record of recent evaluations based on a user-defined key-generation function (keySupplier).
2525
Simply wrap your hook with the debounce hook by passing it a constructor arg, and then configure the remaining options.
26-
In the example below, we wrap a logging hook so that it only logs a maximum of once a minute for each flag key, no matter how many times that flag is evaluated.
26+
In the example below, we wrap the "after" stage of a logging hook so that it only logs a maximum of once a minute for each flag key, no matter how many times that flag is evaluated.
2727

2828
```ts
29-
// a function defining the key for the hook stage
29+
// a function defining the key for the hook stage; if this matches a recent key, the hook execution for this stage will be bypassed
3030
const supplier = (flagKey: string, context: EvaluationContext, details: EvaluationDetails<T>) => flagKey;
3131

32-
const hook = new DebounceHook<string>(loggingHook, {
33-
afterCacheKeySupplier: supplier, // if the key calculated by the supplier exists in the cache, the wrapped hook's stage will not run
34-
ttlMs: 60_000, // how long to cache keys for
35-
maxCacheItems: 100, // max amount of items to keep in the LRU cache
36-
cacheErrors: false // whether or not to cache the errors thrown by hook stages
37-
});
32+
const hook = new DebounceHook<string>(loggingHook,
33+
60_000,
34+
{
35+
debounceTime: 60_000, // how long to wait before the hook can fire again (applied to each stage independently) in milliseconds
36+
afterCacheKeySupplier: supplier, // if the key calculated by the supplier exists in the cache, the wrapped hook's stage will not run
37+
maxCacheItems: 100, // max amount of items to keep in the cache; if exceeded, the oldest item is dropped
38+
cacheErrors: false // whether or not to cache the errors thrown by hook stages
39+
});
3840
```
3941

4042
## Development

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('DebounceHook', () => {
2121
afterCacheKeySupplier: supplier,
2222
errorCacheKeySupplier: supplier,
2323
finallyCacheKeySupplier: supplier,
24-
ttlMs: 60_000,
24+
debounceTime: 60_000,
2525
maxCacheItems: 100,
2626
});
2727

@@ -85,7 +85,7 @@ describe('DebounceHook', () => {
8585

8686
const hook = new DebounceHook<string>(innerHook, {
8787
beforeCacheKeySupplier: (flagKey: string) => flagKey,
88-
ttlMs: 60_000,
88+
debounceTime: 60_000,
8989
maxCacheItems: 1,
9090
});
9191

@@ -97,7 +97,7 @@ describe('DebounceHook', () => {
9797
expect(innerHook.before).toHaveBeenCalledTimes(3);
9898
});
9999

100-
it('should rerun inner hook stage only after ttl', async () => {
100+
it('should rerun inner hook only after debounce time', async () => {
101101
const innerHook: Hook = {
102102
before: jest.fn(),
103103
};
@@ -106,7 +106,7 @@ describe('DebounceHook', () => {
106106

107107
const hook = new DebounceHook<string>(innerHook, {
108108
beforeCacheKeySupplier: (flagKey: string) => flagKey,
109-
ttlMs: 500,
109+
debounceTime: 500,
110110
maxCacheItems: 1,
111111
});
112112

@@ -136,7 +136,7 @@ describe('DebounceHook', () => {
136136

137137
// no suppliers defined, so we no-op (do no caching)
138138
const hook = new DebounceHook<string>(innerHook, {
139-
ttlMs: 60_000,
139+
debounceTime: 60_000,
140140
maxCacheItems: 100,
141141
});
142142

@@ -182,7 +182,7 @@ describe('DebounceHook', () => {
182182
const hook = new DebounceHook<string>(innerErrorHook, {
183183
beforeCacheKeySupplier: (flagKey: string) => flagKey,
184184
maxCacheItems: 100,
185-
ttlMs: 60_000,
185+
debounceTime: 60_000,
186186
cacheErrors,
187187
});
188188

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ export type Options<T extends FlagValue = FlagValue> = {
8787
*/
8888
cacheErrors?: boolean;
8989
/**
90-
* Time to live for items in cache in milliseconds.
90+
* Debounce timeout - how long to wait before the hook can fire again (applied to each stage independently) in milliseconds.
9191
*/
92-
ttlMs: number;
92+
debounceTime: number;
9393
/**
9494
* Max number of items to be kept in cache before the oldest entry falls out.
9595
*/
@@ -107,7 +107,7 @@ export class DebounceHook<T extends FlagValue = FlagValue> implements Hook {
107107
this.cacheErrors = options.cacheErrors || false;
108108
this.cache = new FixedSizeExpiringCache<true | CachedError>({
109109
maxItems: options.maxCacheItems,
110-
ttlMs: options.ttlMs,
110+
ttlMs: options.debounceTime,
111111
});
112112
}
113113

@@ -162,18 +162,19 @@ export class DebounceHook<T extends FlagValue = FlagValue> implements Hook {
162162
throw cached;
163163
}
164164
return;
165-
}
166-
try {
167-
hookCallback();
168-
this.cache.set(cacheKey, true);
169-
} catch (error: unknown) {
170-
if (this.cacheErrors) {
171-
// cache error
172-
this.cache.set(cacheKey, new CachedError(error));
165+
} else {
166+
try {
167+
hookCallback();
168+
this.cache.set(cacheKey, true);
169+
} catch (error: unknown) {
170+
if (this.cacheErrors) {
171+
// cache error
172+
this.cache.set(cacheKey, new CachedError(error));
173+
}
174+
throw error;
173175
}
174-
throw error;
176+
return;
175177
}
176-
return;
177178
} else {
178179
hookCallback();
179180
}

0 commit comments

Comments
 (0)