-
Notifications
You must be signed in to change notification settings - Fork 59
feat: add debounce hook #1389
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
Merged
Merged
feat: add debounce hook #1389
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
80b81f8
feat: add debounce hook
toddbaert ef5caa6
fixup: improve docs, param names
toddbaert 1aaa99a
Update libs/hooks/debounce/README.md
toddbaert 169f522
fixup: review feedback and comments
toddbaert a61ce3c
fixup: conflict
toddbaert 30c7aed
fixup: more comments
toddbaert d88cf36
fixup: thisarg
toddbaert e521624
Apply suggestion from @gemini-code-assist[bot]
toddbaert d29aec0
Apply suggestion from @lukas-reining
toddbaert 7f838b9
Update libs/hooks/debounce/src/lib/debounce-hook.ts
toddbaert 16f63c2
use single supplier so hook is debounced in entirety
toddbaert e94812a
fixup: readme
toddbaert 4ef6156
Update libs/hooks/debounce/README.md
toddbaert ce32d2f
Update libs/hooks/debounce/src/lib/utils/fixed-size-expiring-cache.ts
toddbaert 69e462b
Update libs/hooks/debounce/src/lib/debounce-hook.ts
toddbaert 477c601
fixup: support web and server
toddbaert 0733ac7
fixup: typechecks
toddbaert 91ef84d
Update libs/hooks/debounce/README.md
toddbaert 216d220
Update libs/hooks/debounce/src/lib/debounce-hook.ts
toddbaert 5076b50
fixup: compilation issue with server
toddbaert 28a20cd
fixup: pr feedback
toddbaert 44818f3
Update libs/hooks/debounce/src/lib/debounce-hook.ts
toddbaert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| { | ||
| "extends": "../../../.eslintrc.json", | ||
| "ignorePatterns": ["!**/*"], | ||
| "overrides": [ | ||
| { | ||
| "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], | ||
| "rules": {} | ||
| }, | ||
| { | ||
| "files": ["*.ts", "*.tsx"], | ||
| "rules": {} | ||
| }, | ||
| { | ||
| "files": ["*.js", "*.jsx"], | ||
| "rules": {} | ||
| }, | ||
| { | ||
| "files": ["*.json"], | ||
| "parser": "jsonc-eslint-parser", | ||
| "rules": { | ||
| "@nx/dependency-checks": [ | ||
| "error", | ||
| { | ||
| "ignoredFiles": ["{projectRoot}/eslint.config.{js,cjs,mjs}"] | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # Debounce Hook | ||
|
|
||
| This is a utility "meta" hook, which can be used to effectively debounce or rate limit other hooks based on various parameters. | ||
| This can be especially useful for certain UI frameworks and SDKs that frequently re-render and re-evaluate flags (React, Angular, etc). | ||
|
|
||
| ## Installation | ||
|
|
||
| ``` | ||
| $ npm install @openfeature/debounce-hook | ||
| ``` | ||
|
|
||
| ### Peer dependencies | ||
|
|
||
| This package only requires the `@openfeature/core` dependency, which is installed automatically no matter which OpenFeature JavaScript SDK you are using. | ||
|
|
||
| ## Usage | ||
|
|
||
| Simply wrap your hook with the debounce hook by passing it as a constructor arg, and then configure the remaining options. | ||
| In the example below, we wrap a logging hook. | ||
| This debounces all its stages, so it only logs a maximum of once a minute for each flag key, no matter how many times that flag is evaluated. | ||
|
|
||
| ```ts | ||
| const debounceHook = new DebounceHook(loggingHook, { | ||
| debounceTime: 60_000, // how long to wait before the hook can fire again | ||
| maxCacheItems: 100, // max amount of items to keep in the cache; if exceeded, the oldest item is dropped | ||
| }); | ||
|
|
||
| // add the hook globally | ||
| OpenFeature.addHooks(debounceHook); | ||
|
|
||
| // or at a specific client | ||
| client.addHooks(debounceHook); | ||
| ``` | ||
toddbaert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| The hook maintains a simple expiring cache with a fixed max size and keeps a record of recent evaluations based on an optional key-generation function (cacheKeySupplier). | ||
| Be default, the key-generation function is purely based on the flag key. | ||
| Particularly in server use-cases, you may want to take the targetingKey or other contextual information into account in your debouncing. | ||
| Below we see an example using this, as well as other advanced options: | ||
|
|
||
| ```ts | ||
| const debounceHook = new DebounceHook<string>(loggingHook, { | ||
| cacheKeySupplier: (flagKey, context) => flagKey + context.targetingKey, // cache on a combination of user and flag key | ||
| debounceTime: 60_000, // debounce for 60 seconds (per user due to our cacheKeySupplier above) | ||
| maxCacheItems: 1000, // cache a maximum of 1000 records | ||
| cacheErrors: false, // don't debounce errors; always re-run if the last run threw | ||
| logger: console, // optional logger | ||
| }); | ||
| ``` | ||
|
|
||
toddbaert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ## Development | ||
|
|
||
| ### Building | ||
|
|
||
| Run `nx package hooks-debounce` to build the library. | ||
|
|
||
| ### Running unit tests | ||
|
|
||
| Run `nx test hooks-debounce` to execute the unit tests via [Jest](https://jestjs.io). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "presets": [["minify", { "builtIns": false }]] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| export default { | ||
| displayName: 'debounce', | ||
| preset: '../../../jest.preset.js', | ||
| transform: { | ||
| '^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }], | ||
| }, | ||
| moduleFileExtensions: ['ts', 'js', 'html'], | ||
| coverageDirectory: '../coverage/hooks', | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "name": "@openfeature/debounce-hook", | ||
| "version": "0.0.1", | ||
| "dependencies": { | ||
| "tslib": "^2.3.0" | ||
| }, | ||
| "main": "./src/index.js", | ||
| "typings": "./src/index.d.ts", | ||
| "scripts": { | ||
| "publish-if-not-exists": "cp $NPM_CONFIG_USERCONFIG .npmrc && if [ \"$(npm show $npm_package_name@$npm_package_version version)\" = \"$(npm run current-version -s)\" ]; then echo 'already published, skipping'; else npm publish --access public; fi", | ||
| "current-version": "echo $npm_package_version" | ||
| }, | ||
| "license": "Apache-2.0", | ||
| "peerDependencies": { | ||
| "@openfeature/core": "^1.9.1" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| { | ||
| "name": "debounce", | ||
| "$schema": "../node_modules/nx/schemas/project-schema.json", | ||
| "sourceRoot": "hooks/src", | ||
toddbaert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "projectType": "library", | ||
| "release": { | ||
| "version": { | ||
| "generatorOptions": { | ||
| "packageRoot": "dist/{projectRoot}", | ||
| "currentVersionResolver": "git-tag" | ||
| } | ||
| } | ||
| }, | ||
| "tags": [], | ||
| "targets": { | ||
| "nx-release-publish": { | ||
| "options": { | ||
| "packageRoot": "dist/{projectRoot}" | ||
| } | ||
| }, | ||
| "lint": { | ||
| "executor": "@nx/eslint:lint" | ||
| }, | ||
| "test": { | ||
| "executor": "@nx/jest:jest", | ||
| "outputs": ["{workspaceRoot}/coverage/{projectRoot}"], | ||
| "options": { | ||
| "jestConfig": "{projectRoot}/jest.config.ts" | ||
| } | ||
| }, | ||
| "package": { | ||
| "executor": "@nx/rollup:rollup", | ||
| "outputs": ["{options.outputPath}"], | ||
| "options": { | ||
| "project": "libs/hooks/debounce/package.json", | ||
| "outputPath": "dist/libs/hooks/debounce", | ||
| "entryFile": "libs/hooks/debounce/src/index.ts", | ||
| "tsConfig": "libs/hooks/debounce/tsconfig.lib.json", | ||
| "compiler": "tsc", | ||
| "generateExportsField": true, | ||
| "umdName": "debounce", | ||
| "external": "all", | ||
| "format": ["cjs", "esm"], | ||
| "assets": [ | ||
| { | ||
| "glob": "package.json", | ||
| "input": "./assets", | ||
| "output": "./src/" | ||
| }, | ||
| { | ||
| "glob": "LICENSE", | ||
| "input": "./", | ||
| "output": "./" | ||
| }, | ||
| { | ||
| "glob": "README.md", | ||
| "input": "./libs/hooks/debounce", | ||
| "output": "./" | ||
| } | ||
| ] | ||
| } | ||
| }, | ||
| "publish": { | ||
| "executor": "nx:run-commands", | ||
| "options": { | ||
| "command": "npm run publish-if-not-exists", | ||
| "cwd": "dist/libs/hooks/debounce" | ||
| }, | ||
| "dependsOn": [ | ||
| { | ||
| "projects": "self", | ||
| "target": "package" | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './lib/debounce-hook'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.