Skip to content

Commit

Permalink
Merge branch 'main' into pm-14921-customers-managed-by-a-reseller-nee…
Browse files Browse the repository at this point in the history
…d-to-see-how-many-seats
  • Loading branch information
cyprain-okeke authored Jan 15, 2025
2 parents 1d16265 + 55e4b5e commit bd7de98
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import { DerivedStateProvider } from "../derived-state.provider";
import { DefaultDerivedState } from "./default-derived-state";

export class DefaultDerivedStateProvider implements DerivedStateProvider {
private cache: Record<string, DerivedState<unknown>> = {};
/**
* The cache uses a WeakMap to maintain separate derived states per user.
* Each user's state Observable acts as a unique key, without needing to
* pass around `userId`. Also, when a user's state Observable is cleaned up
* (like during an account swap) their cache is automatically garbage
* collected.
*/
private cache = new WeakMap<Observable<unknown>, Record<string, DerivedState<unknown>>>();

constructor() {}

Expand All @@ -17,16 +24,22 @@ export class DefaultDerivedStateProvider implements DerivedStateProvider {
deriveDefinition: DeriveDefinition<TFrom, TTo, TDeps>,
dependencies: TDeps,
): DerivedState<TTo> {
let stateCache = this.cache.get(parentState$);
if (!stateCache) {
stateCache = {};
this.cache.set(parentState$, stateCache);
}

const cacheKey = deriveDefinition.buildCacheKey();
const existingDerivedState = this.cache[cacheKey];
const existingDerivedState = stateCache[cacheKey];
if (existingDerivedState != null) {
// I have to cast out of the unknown generic but this should be safe if rules
// around domain token are made
return existingDerivedState as DefaultDerivedState<TFrom, TTo, TDeps>;
}

const newDerivedState = this.buildDerivedState(parentState$, deriveDefinition, dependencies);
this.cache[cacheKey] = newDerivedState;
stateCache[cacheKey] = newDerivedState;
return newDerivedState;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { DeriveDefinition } from "../derive-definition";
import { StateDefinition } from "../state-definition";

import { DefaultDerivedState } from "./default-derived-state";
import { DefaultDerivedStateProvider } from "./default-derived-state.provider";

let callCount = 0;
const cleanupDelayMs = 10;
Expand Down Expand Up @@ -182,4 +183,29 @@ describe("DefaultDerivedState", () => {
expect(await firstValueFrom(observable)).toEqual(new Date(newDate));
});
});

describe("account switching", () => {
let provider: DefaultDerivedStateProvider;

beforeEach(() => {
provider = new DefaultDerivedStateProvider();
});

it("should provide a dedicated cache for each account", async () => {
const user1State$ = new Subject<string>();
const user1Derived = provider.get(user1State$, deriveDefinition, deps);
const user1Emissions = trackEmissions(user1Derived.state$);

const user2State$ = new Subject<string>();
const user2Derived = provider.get(user2State$, deriveDefinition, deps);
const user2Emissions = trackEmissions(user2Derived.state$);

user1State$.next("2015-12-30");
user2State$.next("2020-12-29");
await awaitAsync();

expect(user1Emissions).toEqual([new Date("2015-12-30")]);
expect(user2Emissions).toEqual([new Date("2020-12-29")]);
});
});
});

0 comments on commit bd7de98

Please sign in to comment.