-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use IndexedDB for config caching in web workers + add sample app
- Loading branch information
Showing
14 changed files
with
204 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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,18 @@ | ||
{ | ||
"name": "app", | ||
"version": "1.0.0", | ||
"description": "Sample application for ConfigCat JS SDK", | ||
"type": "module", | ||
"private": true, | ||
"scripts": { | ||
"build": "webpack --mode development", | ||
"start": "npm run build & node ../shared/server dist" | ||
}, | ||
"dependencies": { | ||
"@configcat/sdk": "^0.0.1" | ||
}, | ||
"devDependencies": { | ||
"webpack": "^5.91.0", | ||
"webpack-cli": "^5.1.4" | ||
} | ||
} |
This file contains 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 @@ | ||
function addComponent(value) { | ||
const element = document.createElement("div"); | ||
|
||
element.innerHTML = "isAwesomeFeatureEnabled: " + value; | ||
|
||
document.body.appendChild(element); | ||
} | ||
|
||
const myWorker = new Worker("worker.js"); | ||
|
||
myWorker.onmessage = (e) => { | ||
console.log("Feature flag value received from worker"); | ||
addComponent(e.data); | ||
} | ||
|
||
myWorker.postMessage("isAwesomeFeatureEnabled"); | ||
console.log("Feature flag key posted to worker"); |
This file contains 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,15 @@ | ||
import * as configcat from "@configcat/sdk/browser"; | ||
|
||
const logger = configcat.createConsoleLogger(configcat.LogLevel.Info); // Setting log level to Info to show detailed feature flag evaluation | ||
|
||
// You can instantiate the client with different polling modes. See the Docs: https://configcat.com/docs/sdk-reference/js/#polling-modes | ||
const configCatClient = configcat.getClient("PKDVCLf-Hq-h-kCzMp-L7Q/HhOWfwVtZ0mb30i9wi17GQ", configcat.PollingMode.AutoPoll, { pollIntervalSeconds: 2, logger: logger }); | ||
|
||
onmessage = async (e) => { | ||
console.log("Feature flag key received from main script"); | ||
|
||
const value = await configCatClient.getValueAsync(e.data, false); | ||
|
||
postMessage(value); | ||
console.log("Feature flag value posted back to main script"); | ||
}; |
This file contains 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,10 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES2017", | ||
"module": "NodeNext", | ||
"moduleResolution": "NodeNext", | ||
"declaration": false, | ||
"sourceMap": true | ||
}, | ||
"include": ["src/**/*.ts"] | ||
} |
This file contains 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,25 @@ | ||
export default { | ||
mode: "production", | ||
entry: { | ||
"main": "./src/index.ts", | ||
"worker": "./src/worker.ts" | ||
}, | ||
output: { | ||
filename: "[name].js", | ||
library: { type: "umd" }, | ||
}, | ||
resolve: { | ||
extensions: [".ts", ".js"] | ||
}, | ||
module: { | ||
rules: [ | ||
{ | ||
test: /.ts$/, | ||
use: [{ | ||
loader: "ts-loader" | ||
}] | ||
} | ||
] | ||
}, | ||
devtool: "source-map" | ||
}; |
This file contains 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 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 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 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 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,70 @@ | ||
import type { IConfigCatCache } from "../ConfigCatCache"; | ||
import { ExternalConfigCache } from "../ConfigCatCache"; | ||
import type { IConfigCatKernel } from "../ConfigCatClient"; | ||
|
||
const OBJECT_STORE_NAME = "configCache"; | ||
|
||
type DBConnectionFactory = () => Promise<IDBDatabase>; | ||
|
||
export class IndexedDBCache implements IConfigCatCache { | ||
static setup(kernel: IConfigCatKernel, dbConnectionFactoryGetter?: () => DBConnectionFactory | null): IConfigCatKernel { | ||
const dbConnectionFactory = (dbConnectionFactoryGetter ?? getDBConnectionFactory)(); | ||
if (dbConnectionFactory) { | ||
kernel.defaultCacheFactory = options => new ExternalConfigCache(new IndexedDBCache(dbConnectionFactory), options.logger); | ||
} | ||
return kernel; | ||
} | ||
|
||
constructor(private readonly dbConnectionFactory: DBConnectionFactory) { | ||
} | ||
|
||
async set(key: string, value: string): Promise<void> { | ||
const db = await this.dbConnectionFactory(); | ||
try { | ||
await new Promise<void>((resolve, reject) => { | ||
const transaction = db.transaction(OBJECT_STORE_NAME, "readwrite"); | ||
const store = transaction.objectStore(OBJECT_STORE_NAME); | ||
store.put(value, key); | ||
transaction.oncomplete = () => resolve(); | ||
transaction.onerror = event => reject((event.target as IDBRequest<IDBValidKey>).error); | ||
}); | ||
} | ||
finally { db.close(); } | ||
} | ||
|
||
async get(key: string): Promise<string | undefined> { | ||
const db = await this.dbConnectionFactory(); | ||
try { | ||
return await new Promise<string | undefined>((resolve, reject) => { | ||
const transaction = db.transaction(OBJECT_STORE_NAME, "readonly"); | ||
const store = transaction.objectStore(OBJECT_STORE_NAME); | ||
const storeRequest = store.get(key); | ||
let value: string | undefined; | ||
storeRequest.onsuccess = event => value = (event.target as IDBRequest<any>).result; | ||
transaction.oncomplete = () => resolve(value); | ||
transaction.onerror = event => reject((event.target as IDBRequest<IDBValidKey>).error); | ||
}); | ||
} | ||
finally { db.close(); } | ||
} | ||
} | ||
|
||
export function getDBConnectionFactory(): DBConnectionFactory | null { | ||
try { | ||
const dbConnectionFactory = () => new Promise<IDBDatabase>((resolve, reject) => { | ||
const openRequest = indexedDB.open("@configcat/sdk"); | ||
openRequest.onupgradeneeded = event => | ||
(event.target as IDBOpenDBRequest).result.createObjectStore(OBJECT_STORE_NAME); | ||
openRequest.onsuccess = event => resolve((event.target as IDBOpenDBRequest).result); | ||
openRequest.onerror = event => reject((event.target as IDBOpenDBRequest).error); | ||
}); | ||
|
||
// Check if it is possible to connect to the DB. | ||
dbConnectionFactory().then(db => db.close()); | ||
|
||
return dbConnectionFactory; | ||
} | ||
catch { /* intentional no-op */ } | ||
|
||
return null; | ||
} |
This file contains 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 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 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,20 @@ | ||
import { assert } from "chai"; | ||
import { IndexedDBCache, getDbConnectionFactory } from "#lib/shared/IndexedDBCache"; | ||
|
||
describe("IndexedDBCache cache tests", () => { | ||
it("IndexedDBCache works with non latin 1 characters", async function() { | ||
if (typeof indexedDB === "undefined") { | ||
this.skip(); | ||
} | ||
|
||
const dbConnectionFactory = getDbConnectionFactory(); | ||
assert.isNotNull(dbConnectionFactory); | ||
|
||
const cache = new IndexedDBCache(dbConnectionFactory!); | ||
const key = "testkey"; | ||
const text = "äöüÄÖÜçéèñışğ⢙✓😀"; | ||
cache.set(key, text); | ||
const retrievedValue = await cache.get(key); | ||
assert.strictEqual(retrievedValue, text); | ||
}); | ||
}); |