-
-
Notifications
You must be signed in to change notification settings - Fork 731
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
460 additions
and
448 deletions.
There are no files selected for viewing
603 changes: 304 additions & 299 deletions
603
src/lib/features/client-feature-toggles/cache/client-feature-toggle-cache.ts
Large diffs are not rendered by default.
Oops, something went wrong.
208 changes: 104 additions & 104 deletions
208
src/lib/features/client-feature-toggles/cache/revision-cache.test.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 |
---|---|---|
@@ -1,110 +1,110 @@ | ||
import { RevisionCache } from "./revision-cache"; | ||
import { Revision } from "./client-feature-toggle-cache"; | ||
import { RevisionCache } from './revision-cache'; | ||
import type { Revision } from './client-feature-toggle-cache'; | ||
|
||
describe("RevisionCache", () => { | ||
it("should create a new base when trying to add a new revision at the max limit", () => { | ||
const initialRevisions: Revision[] = [ | ||
{ | ||
revisionId: 1, | ||
updated: [ | ||
{ | ||
name: "test-flag", | ||
type: "release", | ||
enabled: false, | ||
project: "default", | ||
stale: false, | ||
strategies: [ | ||
{ | ||
name: "flexibleRollout", | ||
constraints: [], | ||
parameters: { | ||
groupId: "test-flag", | ||
rollout: "100", | ||
stickiness: "default", | ||
}, | ||
variants: [], | ||
}, | ||
], | ||
variants: [], | ||
description: null, | ||
impressionData: false, | ||
}, | ||
], | ||
removed: [], | ||
}, | ||
{ | ||
revisionId: 2, | ||
updated: [ | ||
{ | ||
name: "my-feature-flag", | ||
type: "release", | ||
enabled: true, | ||
project: "default", | ||
stale: false, | ||
strategies: [ | ||
{ | ||
name: "flexibleRollout", | ||
constraints: [], | ||
parameters: { | ||
groupId: "my-feature-flag", | ||
rollout: "100", | ||
stickiness: "default", | ||
}, | ||
variants: [], | ||
}, | ||
], | ||
variants: [], | ||
description: null, | ||
impressionData: false, | ||
}, | ||
], | ||
removed: [], | ||
}, | ||
]; | ||
describe('RevisionCache', () => { | ||
it('should create a new base when trying to add a new revision at the max limit', () => { | ||
const initialRevisions: Revision[] = [ | ||
{ | ||
revisionId: 1, | ||
updated: [ | ||
{ | ||
name: 'test-flag', | ||
type: 'release', | ||
enabled: false, | ||
project: 'default', | ||
stale: false, | ||
strategies: [ | ||
{ | ||
name: 'flexibleRollout', | ||
constraints: [], | ||
parameters: { | ||
groupId: 'test-flag', | ||
rollout: '100', | ||
stickiness: 'default', | ||
}, | ||
variants: [], | ||
}, | ||
], | ||
variants: [], | ||
description: null, | ||
impressionData: false, | ||
}, | ||
], | ||
removed: [], | ||
}, | ||
{ | ||
revisionId: 2, | ||
updated: [ | ||
{ | ||
name: 'my-feature-flag', | ||
type: 'release', | ||
enabled: true, | ||
project: 'default', | ||
stale: false, | ||
strategies: [ | ||
{ | ||
name: 'flexibleRollout', | ||
constraints: [], | ||
parameters: { | ||
groupId: 'my-feature-flag', | ||
rollout: '100', | ||
stickiness: 'default', | ||
}, | ||
variants: [], | ||
}, | ||
], | ||
variants: [], | ||
description: null, | ||
impressionData: false, | ||
}, | ||
], | ||
removed: [], | ||
}, | ||
]; | ||
|
||
const maxLength = 2; | ||
const deltaCache = new RevisionCache(initialRevisions, maxLength); | ||
const maxLength = 2; | ||
const deltaCache = new RevisionCache(initialRevisions, maxLength); | ||
|
||
// Add a new revision to trigger changeBase | ||
deltaCache.addRevision({ | ||
revisionId: 3, | ||
updated: [ | ||
{ | ||
name: "another-feature-flag", | ||
type: "release", | ||
enabled: true, | ||
project: "default", | ||
stale: false, | ||
strategies: [ | ||
{ | ||
name: "flexibleRollout", | ||
constraints: [], | ||
parameters: { | ||
groupId: "another-feature-flag", | ||
rollout: "100", | ||
stickiness: "default", | ||
}, | ||
variants: [], | ||
}, | ||
], | ||
variants: [], | ||
description: null, | ||
impressionData: false, | ||
}, | ||
], | ||
removed: [], | ||
}); | ||
// Add a new revision to trigger changeBase | ||
deltaCache.addRevision({ | ||
revisionId: 3, | ||
updated: [ | ||
{ | ||
name: 'another-feature-flag', | ||
type: 'release', | ||
enabled: true, | ||
project: 'default', | ||
stale: false, | ||
strategies: [ | ||
{ | ||
name: 'flexibleRollout', | ||
constraints: [], | ||
parameters: { | ||
groupId: 'another-feature-flag', | ||
rollout: '100', | ||
stickiness: 'default', | ||
}, | ||
variants: [], | ||
}, | ||
], | ||
variants: [], | ||
description: null, | ||
impressionData: false, | ||
}, | ||
], | ||
removed: [], | ||
}); | ||
|
||
const revisions = deltaCache.getRevisions(); | ||
const revisions = deltaCache.getRevisions(); | ||
|
||
// Check that the base has been changed and merged correctly | ||
expect(revisions.length).toBe(2); | ||
expect(revisions[0].updated.length).toBe(2); | ||
expect(revisions[0].updated).toEqual( | ||
expect.arrayContaining([ | ||
expect.objectContaining({ name: "test-flag" }), | ||
expect.objectContaining({ name: "my-feature-flag" }), | ||
]), | ||
); | ||
}); | ||
// Check that the base has been changed and merged correctly | ||
expect(revisions.length).toBe(2); | ||
expect(revisions[0].updated.length).toBe(2); | ||
expect(revisions[0].updated).toEqual( | ||
expect.arrayContaining([ | ||
expect.objectContaining({ name: 'test-flag' }), | ||
expect.objectContaining({ name: 'my-feature-flag' }), | ||
]), | ||
); | ||
}); | ||
}); |
82 changes: 42 additions & 40 deletions
82
src/lib/features/client-feature-toggles/cache/revision-cache.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 |
---|---|---|
@@ -1,46 +1,48 @@ | ||
import { Revision } from "./client-feature-toggle-cache"; | ||
import type { Revision } from './client-feature-toggle-cache'; | ||
|
||
const mergeWithoutDuplicates = (arr1: any[], arr2: any[]) => { | ||
const map = new Map(); | ||
arr1.concat(arr2).forEach((item) => { | ||
map.set(item.name, item); | ||
}); | ||
return Array.from(map.values()); | ||
const map = new Map(); | ||
arr1.concat(arr2).forEach((item) => { | ||
map.set(item.name, item); | ||
}); | ||
return Array.from(map.values()); | ||
}; | ||
|
||
export class RevisionCache { | ||
private cache: Revision[]; | ||
private maxLength: number; | ||
|
||
constructor(data: Revision[] = [], maxLength: number = 100) { | ||
this.cache = data; | ||
this.maxLength = maxLength; | ||
} | ||
|
||
public addRevision(revision: Revision): void { | ||
if (this.cache.length >= this.maxLength) { | ||
this.changeBase(); | ||
} | ||
|
||
this.cache = [...this.cache, revision]; | ||
} | ||
|
||
public getRevisions(): Revision[] { | ||
return this.cache; | ||
} | ||
|
||
public hasRevision(revisionId: number): boolean { | ||
return this.cache.some((revision) => revision.revisionId === revisionId); | ||
} | ||
|
||
private changeBase(): void { | ||
if (!(this.cache.length >= 2)) return; | ||
const base = this.cache[0]; | ||
const newBase = this.cache[1]; | ||
|
||
newBase.removed = mergeWithoutDuplicates(base.removed, newBase.removed); | ||
newBase.updated = mergeWithoutDuplicates(base.updated, newBase.updated); | ||
|
||
this.cache = [newBase, ...this.cache.slice(2)]; | ||
} | ||
private cache: Revision[]; | ||
private maxLength: number; | ||
|
||
constructor(data: Revision[] = [], maxLength: number = 100) { | ||
this.cache = data; | ||
this.maxLength = maxLength; | ||
} | ||
|
||
public addRevision(revision: Revision): void { | ||
if (this.cache.length >= this.maxLength) { | ||
this.changeBase(); | ||
} | ||
|
||
this.cache = [...this.cache, revision]; | ||
} | ||
|
||
public getRevisions(): Revision[] { | ||
return this.cache; | ||
} | ||
|
||
public hasRevision(revisionId: number): boolean { | ||
return this.cache.some( | ||
(revision) => revision.revisionId === revisionId, | ||
); | ||
} | ||
|
||
private changeBase(): void { | ||
if (!(this.cache.length >= 2)) return; | ||
const base = this.cache[0]; | ||
const newBase = this.cache[1]; | ||
|
||
newBase.removed = mergeWithoutDuplicates(base.removed, newBase.removed); | ||
newBase.updated = mergeWithoutDuplicates(base.updated, newBase.updated); | ||
|
||
this.cache = [newBase, ...this.cache.slice(2)]; | ||
} | ||
} |
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