Skip to content
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

[Feature] toFactory implementation #71

Merged
merged 9 commits into from
Oct 1, 2022
46 changes: 46 additions & 0 deletions spec/pipeline.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,50 @@ describe("pipelines", () => {
expect(data.gramps.children[0]).toEqual(data.dad);
expect(data.gramps.spouse).toBeNull();
});

it("pipelines to async factories", async () => {
const p = Pipe.Pipeline.start()
.addValues({ hello: "kitty", hola: "espanol" })
.addValues(() => Promise.resolve({ byebye: "birdie" }))
.addValues(v => ({
corner: `${v.hello} corner`,
golf: v.byebye
}))
.addFactory(childFactory, "kiddo", { grade: 2 })
.addFactory(parentFactory, "dad", v =>
Promise.resolve({
name: "Dad",
children: [v.kiddo],
spouse: null
})
)
.addTxFactory(grandpaFactory, "gramps", v => ({
name: "Gramps",
children: [v.dad],
spouse: null
}));

const p_factory = p.toFactory();
const data = await p_factory.build();

expect(data.hello).toEqual("kitty");
expect(data.hola).toEqual("espanol");
expect(data.byebye).toEqual("birdie");
expect(data.corner).toEqual("kitty corner");
expect(data.golf).toEqual("birdie");
expect(data.kiddo.grade).toEqual(2);
expect(data.kiddo.name).toEqual("Kid");
expect(data.dad.name).toEqual("Dad");
expect(data.dad.birthday.getTime()).toEqual(
new Date("2017/05/03").getTime()
);
expect(data.dad.children.length).toEqual(1);
expect(data.dad.children[0]).toEqual(data.kiddo);
expect(data.dad.spouse).toBeNull();
expect(data.gramps.name).toEqual("Gramps");
expect(data.gramps.spoils).toEqual(true);
expect(data.gramps.children.length).toEqual(1);
expect(data.gramps.children[0]).toEqual(data.dad);
expect(data.gramps.spouse).toBeNull();
});
});
18 changes: 10 additions & 8 deletions src/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export type ListFactoryFunc<T, K extends keyof T, U = T> = keyof T extends K
? (count: number, item?: RecPartial<T>) => Promise<U[]>
: (count: number, item: RecPartial<T> & Omit<T, K>) => Promise<U[]>;

function isPromise<T extends Object>(t: T | Promise<T>): t is Promise<T> {
return typeof (t as any)["then"] === "function";
function isPromise<T>(t: T | Promise<T>): t is Promise<T> {
SieR-VR marked this conversation as resolved.
Show resolved Hide resolved
return !!t && typeof (t as any)["then"] === "function";
}

export function lift<T>(t: T | Promise<T>): Promise<T> {
Expand Down Expand Up @@ -57,7 +57,7 @@ export class Factory<T, K extends keyof T = keyof T>
(this.config && this.config.startingSequenceNumber) || 0;

constructor(
readonly builder: Builder<T, K>,
readonly builder: Builder<T, K> | Promise<Builder<T, K>>,
private readonly config: AsyncFactoryConfig | undefined
) {
this.seqNum = this.getStartingSequenceNumber();
Expand Down Expand Up @@ -270,13 +270,15 @@ interface BaseBuild<T> {

async function buildBase<T, K extends keyof T>(
seqNum: number,
builder: Builder<T, K>
builder: Builder<T, K> | Promise<Builder<T, K>>
): Promise<BaseBuild<T>> {
const resolvedBuilder = await lift(builder);

const t: { [key: string]: any } = {};
const keys = Object.getOwnPropertyNames(builder);
const keys = Object.getOwnPropertyNames(resolvedBuilder);
const derived: BaseDerived[] = [];
for (const key of keys) {
const v = (builder as any)[key];
const v = (resolvedBuilder as any)[key];
let value = v;
if (!!v && typeof v === "object") {
if (isPromise(v)) {
Expand All @@ -299,14 +301,14 @@ async function buildBase<T, K extends keyof T>(
}

export function makeFactory<T>(
builder: Builder<T, keyof T>,
builder: Builder<T, keyof T> | Promise<Builder<T, keyof T>>,
config?: AsyncFactoryConfig
): Factory<T, keyof T> {
return new Factory(builder, config);
}

export function makeFactoryWithRequired<T, K extends keyof T>(
builder: Builder<T, Exclude<keyof T, K>>,
builder: Builder<T, Exclude<keyof T, K>> | Promise<Builder<T, Exclude<keyof T, K>>>,
config?: AsyncFactoryConfig
): Factory<T, Exclude<keyof T, K>> {
return new Factory(builder, config);
Expand Down
10 changes: 7 additions & 3 deletions src/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ type PipePartialRec<P, T, K extends keyof T> = MaybePromiseFunc<
RecPartial<Pick<T, K>> & Omit<T, K>
>;

export class Pipeline<P extends Object = {}> implements PromiseLike<P> {
constructor(private current: Promise<P>) {}
export class Pipeline<P extends Record<string, unknown> = {}> implements PromiseLike<P> {
constructor(private current: Promise<P>) { }

static start() {
return new Pipeline(Promise.resolve({}));
Expand Down Expand Up @@ -91,4 +91,8 @@ export class Pipeline<P extends Object = {}> implements PromiseLike<P> {
): Promise<TResult1 | TResult2> {
return this.current.then(onfulfilled, onrejected);
}
}

toFactory(): Async.Factory<P> {
return Async.makeFactory(this.current as Promise<Async.Builder<P>>);
}
}