From 90693842034a1f3f44a84ff77f71a5ba45f24919 Mon Sep 17 00:00:00 2001 From: Matt Gibson Date: Mon, 20 May 2024 17:31:33 -0400 Subject: [PATCH] Rename to Factory --- .../context/execution-context.builder.spec.ts | 10 +++--- .../context/execution-context.builder.ts | 34 ++++++++----------- .../context/user-execution-context.builder.ts | 2 +- 3 files changed, 21 insertions(+), 25 deletions(-) diff --git a/libs/common/src/platform/context/execution-context.builder.spec.ts b/libs/common/src/platform/context/execution-context.builder.spec.ts index 54dafdf077..99e4620078 100644 --- a/libs/common/src/platform/context/execution-context.builder.spec.ts +++ b/libs/common/src/platform/context/execution-context.builder.spec.ts @@ -4,7 +4,7 @@ import { ExecutionContextBuilder } from "./execution-context.builder"; describe("ExecutionContextBuilder", () => { describe("with", () => { - it("builds the context with the value returned by the predicate when the value is not a promise or observable", async () => { + it("builds the context with the value returned by the factory when the value is not a promise or observable", async () => { const context = await new ExecutionContextBuilder() .with(() => 42) .as("value") @@ -13,7 +13,7 @@ describe("ExecutionContextBuilder", () => { expect(context).toEqual({ value: 42 }); }); - it("builds the context with the value resolved by the promise returned by the predicate", async () => { + it("builds the context with the value resolved by the promise returned by the factory", async () => { const context = await new ExecutionContextBuilder() .with(() => Promise.resolve(42)) .as("value") @@ -22,7 +22,7 @@ describe("ExecutionContextBuilder", () => { expect(context).toEqual({ value: 42 }); }); - it("builds the context with the value resolved by the observable returned by the predicate", async () => { + it("builds the context with the value resolved by the observable returned by the factory", async () => { const context = await new ExecutionContextBuilder() .with(() => of(42)) .as("value") @@ -33,7 +33,7 @@ describe("ExecutionContextBuilder", () => { }); describe("multiple with", () => { - it("builds the context with all the values returned by the predicates", async () => { + it("builds the context with all the values returned by the factories", async () => { const context = await new ExecutionContextBuilder() .with(() => 42) .as("value") @@ -44,7 +44,7 @@ describe("ExecutionContextBuilder", () => { expect(context).toEqual({ value: 42, string: "foo" }); }); - it("provides the current context to the predicate", async () => { + it("provides the current context to the factory", async () => { const context = await new ExecutionContextBuilder() .withValue(42) .as("first") diff --git a/libs/common/src/platform/context/execution-context.builder.ts b/libs/common/src/platform/context/execution-context.builder.ts index 99deb7e5b1..912ee62000 100644 --- a/libs/common/src/platform/context/execution-context.builder.ts +++ b/libs/common/src/platform/context/execution-context.builder.ts @@ -9,25 +9,24 @@ export class ExecutionContextPropertyBuilder< > { constructor( private readonly executionContextBuilder: TExecutionContextBuilder, - private readonly predicate: ExecutionContextPredicate, + private readonly factory: ExecutionContextFactory, ) {} as( propertyName: TKey, ): ExecutionContextBuilder> { - return this.executionContextBuilder.addPropertyBuilder(propertyName, this.predicate); + return this.executionContextBuilder.addPropertyBuilder(propertyName, this.factory); } } -type ExecutionContextPredicate = (context: TContext) => T | Promise | Observable; +type ExecutionContextFactory = (context: TContext) => T | Promise | Observable; export class ExecutionContextBuilder { - // The type of these predicates are the context that is built by the time they are called and the value they return, + // The type of these factories are the context that is built by the time they are called and the value they return, // which is protected by the `with` method and `as` method on ExecutionContextPropertyBuilder. - protected contextMap: { name: string; predicate: ExecutionContextPredicate }[] = - []; + protected contextMap: { name: string; factory: ExecutionContextFactory }[] = []; with( - contextBuilder: ExecutionContextPredicate, + contextBuilder: ExecutionContextFactory, ): ExecutionContextPropertyBuilder { return new ExecutionContextPropertyBuilder(this, contextBuilder); } @@ -46,13 +45,13 @@ export class ExecutionContextBuilder { async build(): Promise { const context: Record = {}; - for (const { name, predicate } of this.contextMap) { - let predicateResult = predicate(context); - if (isObservable(predicateResult)) { - predicateResult = firstValueFrom(predicateResult); + for (const { name, factory } of this.contextMap) { + let factoryResult = factory(context); + if (isObservable(factoryResult)) { + factoryResult = firstValueFrom(factoryResult); } - context[name] = await predicateResult; + context[name] = await factoryResult; } return context as T; } @@ -61,19 +60,16 @@ export class ExecutionContextBuilder { * @internal This method is part of the builder pattern and is not intended for external use. * * @param propertyName The name of the new context property - * @param predicate The predicate that will be used to build the new context property + * @param factory The factory that will be used to build the new context property * @returns A new ExecutionContextBuilder with the new context property added */ addPropertyBuilder< TKey extends string, TValue, - TValuePredicate extends ExecutionContextPredicate, - >( - propertyName: TKey, - predicate: TValuePredicate, - ): ExecutionContextBuilder> { + TValueFactory extends ExecutionContextFactory, + >(propertyName: TKey, factory: TValueFactory): ExecutionContextBuilder> { const result = new ExecutionContextBuilder>(); - result.contextMap = this.contextMap.concat({ name: propertyName, predicate }); + result.contextMap = this.contextMap.concat({ name: propertyName, factory }); return result; } } diff --git a/libs/common/src/platform/context/user-execution-context.builder.ts b/libs/common/src/platform/context/user-execution-context.builder.ts index 09bac56ebb..816cc369fc 100644 --- a/libs/common/src/platform/context/user-execution-context.builder.ts +++ b/libs/common/src/platform/context/user-execution-context.builder.ts @@ -13,7 +13,7 @@ export class UserExecutionContextBuilder extends ExecutionContextBuilder<{ this.contextMap.push({ name: "userId", - predicate: () => this.accountService.activeAccount$.pipe(map((a) => a?.id)), + factory: () => this.accountService.activeAccount$.pipe(map((a) => a?.id)), }); } }