Skip to content

Commit

Permalink
feat: perfect track init
Browse files Browse the repository at this point in the history
  • Loading branch information
tclxshunquan-wang committed Aug 6, 2024
1 parent b162c08 commit d5a26e4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
9 changes: 8 additions & 1 deletion src/adapter/adapter-builder.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { UnionToTuple } from '../types/type-union-tuple.js';
import {
AdapterAfterFunction,
AdapterBeforeFunction,
Expand Down Expand Up @@ -116,10 +117,16 @@ export class AdapterBuilder<
);
return this.mountTransformHook<RightKey, RightEventData>(eventType, fun);
};
return {
const result = {
transform: transform,
...this.buildTransformChainer(),
};

return result as UnionToTuple<
Exclude<keyof LeftEventData, Key>
>['length'] extends 0
? ReturnType<typeof this.buildTransformChainer>
: typeof result;
};

private mountAfterHook = (fun: AdapterAfterFunction<Context, EventData>) => {
Expand Down
13 changes: 9 additions & 4 deletions src/core/track-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { executeAdapterCtx } from '../helpers/helper-adapter-ctx.js';
import { executeAdapterTrack } from '../helpers/helper-adapter-track.js';
import { deepMerge } from '../helpers/helper-deep-merge.js';
import { executeFunction } from '../helpers/helper-execute.js';
import { isFunction } from '../helpers/helper-is-function.js';
import { executeSelect } from '../helpers/helper-select-adapter.js';
import {
TrackContext,
Expand Down Expand Up @@ -50,9 +51,9 @@ export class TrackBuilder<
}

public init<AdapterMap extends TrackAdapterMap<Context, EventData>>(
fun: () => AdapterMap
fun: AdapterMap | (() => AdapterMap)
) {
return this.executeInit<AdapterMap>(fun());
return this.executeInit<AdapterMap>(fun);
}

private buildInitChainer<
Expand Down Expand Up @@ -106,9 +107,13 @@ export class TrackBuilder<
private executeInit = <
AdapterMap extends TrackAdapterMap<Context, EventData>,
>(
adapterMap: AdapterMap
adapterMap: AdapterMap | (() => AdapterMap)
) => {
this.adapterMap = adapterMap;
if (isFunction(adapterMap)) {
this.adapterMap = adapterMap();
} else {
this.adapterMap = adapterMap;
}
return this.buildInitChainer<AdapterMap>();
};

Expand Down
12 changes: 12 additions & 0 deletions src/types/type-union-tuple.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export type UnionToIntersection<U> = (
U extends any ? (a: (k: U) => void) => void : never
) extends (a: infer I) => void
? I
: never;

export type UnionLast<U> =
UnionToIntersection<U> extends (a: infer I) => void ? I : never;

export type UnionToTuple<U> = [U] extends [never]
? []
: [...UnionToTuple<Exclude<U, UnionLast<U>>>, UnionLast<U>];
9 changes: 5 additions & 4 deletions tests/test-track-pipeline.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ describe('test-track-pipeline.spec', () => {
timeStamp: '2024-09-01T00:00:00Z',
};
})
.transform('previewGoods', (ctx, eventType, eventData) => {})
.transform('registry', (ctx, eventType, eventData) => {})
.transform('timeStamp', (ctx, eventType, eventData) => {})
.after(async (ctx, eventType, eventData) => {
console.log('after', eventData);
})
Expand All @@ -70,16 +73,14 @@ describe('test-track-pipeline.spec', () => {
>();

await trackBuilder
.init(() => {
return { reportAdapter: adapter };
})
.init({ reportAdapter: adapter, consoleAdapter: adapter })
.before(async (ctx) => {
console.log('before');
})
.after(async (ctx) => {
console.log('after');
})
.select('reportAdapter')
.select(() => ['consoleAdapter', 'reportAdapter'])
.track('addCart', eventData.addCart);

expect(true).toBeTruthy();
Expand Down

0 comments on commit d5a26e4

Please sign in to comment.