Skip to content

Commit

Permalink
fix: fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tclxshunquan-wang committed Aug 25, 2024
1 parent 5fd49ce commit fa472d2
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 77 deletions.
4 changes: 2 additions & 2 deletions examples/next-example/track/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { CheckUndefined } from '@hyperse/track';
import { GetSafeRealEventTypes } from '@hyperse/track';

export type ReportAdapterOptions<Context, EventData, RealEventData> = {
setup?: <EventType extends keyof EventData>(
ctx: Context,
eventType: CheckUndefined<RealEventData, EventData>,
eventType: GetSafeRealEventTypes<RealEventData, EventData>,
eventData: EventData[EventType]
) => {
timeStamp: number;
Expand Down
59 changes: 25 additions & 34 deletions src/adapter/adapter-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
AdapterBeforeFunction,
AdapterReportData,
AdapterTransformFunction,
CheckUndefined,
GetSafeRealEventTypes,
TrackAdapter,
} from '../types/types-adapter.js';
import { TrackAdapterOptions, TrackContext } from '../types/types-create.js';
Expand All @@ -30,35 +30,30 @@ export abstract class BaseAdapter<
private afterHook?: AdapterAfterFunction<Context, RealEventData, EventData>;

abstract isTrackable<
EventType extends CheckUndefined<RealEventData, EventData>,
EventType extends GetSafeRealEventTypes<RealEventData, EventData>,
>(
ctx: Context,
eventType: CheckUndefined<RealEventData, EventData>,
eventType: GetSafeRealEventTypes<RealEventData, EventData>,
reportData?:
| AdapterReportData<RealEventData, EventData, EventType>
| Awaited<AdapterReportData<RealEventData, EventData, EventType>>
): boolean | Promise<boolean>;

protected isEventOfReportDataEqual<
EventType extends
| CheckUndefined<RealEventData, EventData>
| CheckUndefined<RealEventData, EventData>[],
EventType extends GetSafeRealEventTypes<RealEventData, EventData>,
>(
eventType: CheckUndefined<RealEventData, EventData>,
eventType: GetSafeRealEventTypes<RealEventData, EventData>,
reportData: RealEventData[keyof RealEventData] | EventData[keyof EventData],
realEventType: EventType
): reportData is EventType extends CheckUndefined<RealEventData, EventData>[]
? AdapterReportData<RealEventData, EventData, EventType[number]>
: AdapterReportData<RealEventData, EventData, EventType> {
if (Array.isArray(realEventType)) {
return realEventType.map(String).includes(`${eventType}`);
}
): reportData is AdapterReportData<RealEventData, EventData, EventType> {
return eventType === realEventType;
}

protected report<EventType extends CheckUndefined<RealEventData, EventData>>(
protected report<
EventType extends GetSafeRealEventTypes<RealEventData, EventData>,
>(
ctx: Context,
eventType: CheckUndefined<RealEventData, EventData>,
eventType: GetSafeRealEventTypes<RealEventData, EventData>,
reportData?:
| AdapterReportData<RealEventData, EventData, EventType>
| Awaited<AdapterReportData<RealEventData, EventData, EventType>>,
Expand Down Expand Up @@ -108,26 +103,20 @@ export abstract class BaseAdapter<
}

private executeTransform = async <EventType extends keyof EventData>(
adapterName: string,
ctx: Context,
eventType: EventType,
eventData: EventData[EventType]
) => {
if (Object.keys(this.transformHookMap).length < 1) {
ctx.logger?.warn('Adapter transform hook is not defined');
return {
reportData: eventData,
realEventType: eventType as unknown as CheckUndefined<
RealEventData,
EventData
>,
};
}
const transformHook = this.transformHookMap[eventType];

if (!transformHook) {
ctx.logger?.debug(
`Adapter ${adapterName}: transform hook is not defined`
);
return {
reportData: eventData,
realEventType: eventType as unknown as CheckUndefined<
realEventType: eventType as unknown as GetSafeRealEventTypes<
RealEventData,
EventData
>,
Expand All @@ -143,19 +132,20 @@ export abstract class BaseAdapter<

return {
reportData,
realEventType: transformHook.realEventType as unknown as CheckUndefined<
RealEventData,
EventData
>,
realEventType:
transformHook.realEventType as unknown as GetSafeRealEventTypes<
RealEventData,
EventData
>,
};
};

private executeReport = async <
EventType extends CheckUndefined<RealEventData, EventData>,
EventType extends GetSafeRealEventTypes<RealEventData, EventData>,
>(
adapterName: string,
ctx: Context,
realEventType: CheckUndefined<RealEventData, EventData>,
realEventType: GetSafeRealEventTypes<RealEventData, EventData>,
eventData: EventData[keyof EventData],
reportData?:
| AdapterReportData<RealEventData, EventData, EventType>
Expand All @@ -169,7 +159,7 @@ export abstract class BaseAdapter<
);

if (!isTrackable) {
ctx.logger?.warn(`Adapter is not trackable: ${adapterName}`);
ctx.logger?.warn(`Adapter ${adapterName}: is not trackable`);
return;
}

Expand Down Expand Up @@ -198,7 +188,8 @@ export abstract class BaseAdapter<
await pipe(
async () =>
await executeFunction(this.beforeHook, ctx, eventType, eventData),
async () => await this.executeTransform(ctx, eventType, eventData),
async () =>
await this.executeTransform(adapterName, ctx, eventType, eventData),
async ({ reportData, realEventType }) => {
await this.executeReport(
adapterName,
Expand Down
10 changes: 6 additions & 4 deletions src/types/types-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type TransformEventData<Key, RealEventData, EventData> =
? EventData[Key[0]]
: never;

export type CheckUndefined<RealEventData, EventData> =
export type GetSafeRealEventTypes<RealEventData, EventData> =
RealEventData extends undefined ? keyof EventData : keyof RealEventData;

/**
Expand All @@ -31,7 +31,7 @@ export type CheckUndefined<RealEventData, EventData> =
export type AdapterReportData<
RealEventData,
EventData,
EventType = CheckUndefined<RealEventData, EventData>,
EventType = GetSafeRealEventTypes<RealEventData, EventData>,
> = EventType extends keyof EventData
? EventData[EventType]
: EventType extends keyof RealEventData
Expand Down Expand Up @@ -59,7 +59,7 @@ export type AdapterBeforeFunction<Context, EventData> = (
*/
export type AdapterAfterFunction<Context, RealEventData, EventData> = (
ctx: Context,
eventType: CheckUndefined<RealEventData, EventData>,
eventType: GetSafeRealEventTypes<RealEventData, EventData>,
reportData?:
| AdapterReportData<RealEventData, EventData>
| Awaited<AdapterReportData<RealEventData, EventData>>
Expand Down Expand Up @@ -136,7 +136,9 @@ export interface TrackAdapter<
* it uses data from `EventData`; otherwise, it uses data from `RealEventData`.
* @returns A boolean or a promise that resolves to a boolean indicating whether the event is trackable.
*/
isTrackable<EventType extends CheckUndefined<RealEventData, EventData>>(
isTrackable<
EventType extends GetSafeRealEventTypes<RealEventData, EventData>,
>(
ctx: Context,
eventType: EventType,
reportData?:
Expand Down
4 changes: 2 additions & 2 deletions src/types/types-create.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CheckUndefined } from './types-adapter.js';
import { GetSafeRealEventTypes } from './types-adapter.js';
import { TrackLogger } from './types-logger.js';
import { TrackEventDataBase } from './types-track.js';

Expand Down Expand Up @@ -64,7 +64,7 @@ export type TrackAdapterOptions<
*/
setup?: <EventType extends keyof EventData>(
ctx: Context,
eventType: CheckUndefined<RealEventData, EventData>,
eventType: GetSafeRealEventTypes<RealEventData, EventData>,
eventData: EventData[EventType]
) => any | Promise<any>;
};
2 changes: 1 addition & 1 deletion src/types/types-track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ export type TrackAdapterMap<
Context extends TrackContext<any>,
EventData extends TrackEventDataBase,
> = {
[name: string]: TrackAdapter<Context, EventData, any, EventData>;
[name: string]: TrackAdapter<Context, EventData, any, any>;
};
16 changes: 7 additions & 9 deletions tests/test-real-transform.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createAdapterBuilder } from '../src/adapter/create-adapter-builder.js';
import { createTrackBuilder } from '../src/index.js';
import { TrackContext } from '../src/types/types-create.js';
import { AnalyzerAdapter } from './test-utils/adapter/analyzer-adapter.js';
import { ConsoleLogger } from './test-utils/console-logger.js';
Expand Down Expand Up @@ -331,6 +332,11 @@ describe('test-real-transform.spec', () => {
eventData.timeStamp
);

const trackBuilder = createTrackBuilder<
TrackContext<TrackData>,
EventDataOption
>();

expect(isTrackableFun.mock.lastCall).toBeDefined();
expect(isTrackableFun.mock.lastCall?.[0]).toMatchObject({
data: trackData,
Expand Down Expand Up @@ -358,15 +364,7 @@ describe('test-real-transform.spec', () => {
analyzerAdapter.testIsEventOfReportDataEqual(
'_registry',
eventData?.registry,
['_registry']
)
).toBeTruthy();

expect(
analyzerAdapter.testIsEventOfReportDataEqual(
'_registry',
eventData?.registry,
['_addCart']
'_addCart'
)
).toBe(false);
});
Expand Down
8 changes: 4 additions & 4 deletions tests/test-track-execute-select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,16 +247,16 @@ describe('test-track-execute-select.spec', () => {
expect(print.mock.calls).toHaveLength(4);
expect(print.mock.results).toHaveLength(4);
expect(print.mock.results?.[0].value).toBe(
'Adapter is not trackable: analyzerAdapter'
'Adapter analyzerAdapter: is not trackable'
);
expect(print.mock.results?.[1].value).toBe(
'Adapter is not trackable: reportAdapter'
'Adapter reportAdapter: is not trackable'
);
expect(print.mock.results?.[2].value).toBe(
'Adapter is not trackable: logAdapter'
'Adapter logAdapter: is not trackable'
);
expect(print.mock.results?.[3].value).toBe(
'Adapter is not trackable: businessAdapter'
'Adapter businessAdapter: is not trackable'
);
});
});
2 changes: 1 addition & 1 deletion tests/test-track-parallel-pipeline.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ const mockAdapter = (
): [
Mock<(ctx: any, reportData: any, setupData: any) => any>,
Mock<(adapterName: string) => string>,
TrackAdapter<TrackContext<TrackData>, EventDataOption, any, EventDataOption>,
TrackAdapter<TrackContext<TrackData>, EventDataOption, any, any>,
] => {
const reportAdapterBuilder = createAdapterBuilder<
TrackContext<TrackData>,
Expand Down
24 changes: 11 additions & 13 deletions tests/test-utils/adapter/analyzer-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
AdapterReportData,
BaseAdapter,
CheckUndefined,
GetSafeRealEventTypes,
} from '../../../src/index.js';
import { TrackContext } from '../../../src/types/types-create.js';
import { AdapterRealOptions } from '../types/type-adapter-options.js';
Expand All @@ -19,21 +19,21 @@ export class AnalyzerAdapter extends BaseAdapter<
RealEventDataOption
> {
testIsEventOfReportDataEqual<
EventType extends
| CheckUndefined<RealEventDataOption, EventDataOption>
| CheckUndefined<RealEventDataOption, EventDataOption>[],
EventType extends GetSafeRealEventTypes<
RealEventDataOption,
EventDataOption
>,
>(
eventType: CheckUndefined<RealEventDataOption, EventDataOption>,
eventType: GetSafeRealEventTypes<RealEventDataOption, EventDataOption>,
reportData:
| RealEventDataOption[keyof RealEventDataOption]
| EventDataOption[keyof EventDataOption],
realEventType: EventType
): reportData is EventType extends CheckUndefined<
): reportData is AdapterReportData<
RealEventDataOption,
EventDataOption
>[]
? AdapterReportData<RealEventDataOption, EventDataOption, EventType[number]>
: AdapterReportData<RealEventDataOption, EventDataOption, EventType> {
EventDataOption,
EventType
> {
return this.isEventOfReportDataEqual(eventType, reportData, realEventType);
}

Expand All @@ -47,9 +47,7 @@ export class AnalyzerAdapter extends BaseAdapter<
>
| undefined
): boolean | Promise<boolean> {
return !this.isEventOfReportDataEqual(eventType, reportData, [
'_timeStamp',
]);
return !this.isEventOfReportDataEqual(eventType, reportData, '_timeStamp');
}

report<EventType extends keyof RealEventDataOption>(
Expand Down
4 changes: 2 additions & 2 deletions tests/test-utils/types/type-adapter-options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CheckUndefined } from '../../../src/index.js';
import { GetSafeRealEventTypes } from '../../../src/index.js';

export interface AdapterOptions<Context, EventData> {
setup?: <EventType extends keyof EventData>(
Expand All @@ -15,7 +15,7 @@ export interface AdapterOptions<Context, EventData> {
export type AdapterRealOptions<Context, EventData, RealEventData> = {
setup?: <EventType extends keyof EventData>(
ctx: Context,
eventType: CheckUndefined<RealEventData, EventData>,
eventType: GetSafeRealEventTypes<RealEventData, EventData>,
eventData: EventData[EventType]
) => {
name: 'setup' | 'setup1' | 'setup2';
Expand Down
2 changes: 1 addition & 1 deletion website/docs/api/adapter-builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ The `after` hook is executed after the event has been reported. This is where yo
adapterBuilder.after(
(
ctx: Context,
eventType: CheckUndefined<RealEventData, EventData>,
eventType: GetSafeRealEventTypes<RealEventData, EventData>,
reportData?:
| AdapterReportData<RealEventData, EventData>
| Awaited<AdapterReportData<RealEventData, EventData>>
Expand Down
10 changes: 6 additions & 4 deletions website/docs/api/base-adapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@ export abstract class BaseAdapter<
> implements TrackAdapter<Context, EventData, AdapterOptions, RealEventData>
{
abstract isTrackable<
EventType extends CheckUndefined<RealEventData, EventData>,
EventType extends GetSafeRealEventTypes<RealEventData, EventData>,
>(
ctx: Context,
eventType: CheckUndefined<RealEventData, EventData>,
eventType: GetSafeRealEventTypes<RealEventData, EventData>,
reportData?:
| AdapterReportData<RealEventData, EventData, EventType>
| Awaited<AdapterReportData<RealEventData, EventData, EventType>>
): boolean | Promise<boolean>;

protected report<EventType extends CheckUndefined<RealEventData, EventData>>(
protected report<
EventType extends GetSafeRealEventTypes<RealEventData, EventData>,
>(
ctx: Context,
eventType: CheckUndefined<RealEventData, EventData>,
eventType: GetSafeRealEventTypes<RealEventData, EventData>,
reportData?:
| AdapterReportData<RealEventData, EventData, EventType>
| Awaited<AdapterReportData<RealEventData, EventData, EventType>>,
Expand Down

0 comments on commit fa472d2

Please sign in to comment.