Skip to content

Commit

Permalink
Cleanup tracker events
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitryAstafyev committed Oct 23, 2024
1 parent dc3c44a commit 68e6052
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 29 deletions.
20 changes: 12 additions & 8 deletions application/apps/rustcore/ts-bindings/src/api/tracker.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ interface ISessionEventsInterfaces {
Ticks: {
self: 'object';
uuid: 'string';
progress: [
{ self: 'object'; total: 'number'; count: 'number' },
{ self: 'object'; type: 'string' },
];
progress: {
self: 'object';
total: ['number', 'undefined'];
count: 'number';
state: ['string', 'undefined'];
};
};
}

Expand All @@ -55,10 +57,12 @@ const SessionEventsInterfaces: ISessionEventsInterfaces = {
Ticks: {
self: 'object',
uuid: 'string',
progress: [
{ self: 'object', total: 'number', count: 'number' },
{ self: 'object', type: 'string' },
],
progress: {
self: 'object',
total: ['number', 'undefined'],
count: 'number',
state: ['string', 'undefined'],
},
},
};

Expand Down
24 changes: 17 additions & 7 deletions application/apps/rustcore/ts-bindings/src/provider/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface IOrderStat {
duration: number;
}

export type Decoder = (buf: number[] | Buffer) => any;
export type Decoder = (buf: number[] | Buffer) => any | Error;

export abstract class Computation<TEvents, IEventsSignatures, IEventsInterfaces> {
private _destroyed: boolean = false;
Expand Down Expand Up @@ -256,6 +256,12 @@ export abstract class Computation<TEvents, IEventsSignatures, IEventsInterfaces>
try {
if (data instanceof Array || data instanceof Buffer) {
event = this._decoder(data);
if (event instanceof Error) {
this.debug().emit.error(
this.logger.error(`Fail to parse message: ${event.message}`),
);
return;
}
} else if (typeof data === 'string') {
try {
event = JSON.parse(data);
Expand All @@ -268,9 +274,11 @@ export abstract class Computation<TEvents, IEventsSignatures, IEventsInterfaces>
} else if (typeof data === 'object' && data !== null) {
event = data;
} else {
const msg: string = `Unsupported format of event data: ${typeof data} / ${data}.\nExpecting type (JSON string): { [type: string]: string | undefined }`;
this.debug().emit.error(msg);
this.logger.error(msg);
this.debug().emit.error(
this.logger.error(
`Unsupported format of event data: ${typeof data} / ${data}.\n Expecting type (JSON string): { [type: string]: string | undefined }`,
),
);
return;
}
if (typeof event === 'string') {
Expand All @@ -280,9 +288,11 @@ export abstract class Computation<TEvents, IEventsSignatures, IEventsInterfaces>
event === null ||
Object.keys(event).length !== 1
) {
const msg: string = `Has been gotten incorrect event data: ${data} (type: ${typeof data}). No any props field found.\nExpecting type (JSON string): { [type: string]: string | undefined }`;
this.debug().emit.error(msg);
this.logger.error(msg);
this.debug().emit.error(
this.logger.error(
`Has been gotten incorrect event data: ${data} (type: ${typeof data}). No any props field found.\nExpecting type (JSON string): { [type: string]: string | undefined }`,
),
);
} else {
const type: string = Object.keys(event)[0];
const body: any = event[type];
Expand Down
61 changes: 47 additions & 14 deletions application/apps/rustcore/ts-bindings/src/util/convertor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,31 +218,64 @@ export function toObserveOptions(source: IObserve): ty.ObserveOptions {
return { origin: { origin_oneof }, parser };
}

export function decodeLifecycleTransition(buf: number[] | Buffer): any {
export function decodeLifecycleTransition(buf: number[] | Buffer):
| {
Started: { uuid: string; alias: string };
}
| {
Stopped: string;
}
| {
Ticks: {
uuid: string;
progress: {
count: number;
state: string | undefined;
total: number | undefined;
};
};
}
| Error {
const event: ty.LifecycleTransition = proto.LifecycleTransition.decode(Uint8Array.from(buf));
if (!event.transition_oneof) {
return {};
return new Error(`Field "transition_oneof" isn't found in LifecycleTransition`);
}
const inner: ty.TransitionOneof = event.transition_oneof;
if ('Started' in inner) {
return { Started: { uuid: inner.Started?.uuid, alias: inner.Started?.alias } };
if (!inner.Started) {
return new Error(
`Has been recieved LifecycleTransition.Started without even definition`,
);
}
return { Started: { uuid: inner.Started.uuid, alias: inner.Started.alias } };
} else if ('Ticks' in inner) {
const ticks = inner.Ticks?.ticks;
if (!inner.Ticks) {
return new Error(`Has been recieved LifecycleTransition.Ticks without even definition`);
}
const ticks = inner.Ticks.ticks;
return {
Ticks: {
uuid: inner.Ticks?.uuid,
progress:
ticks === null || ticks === undefined
? {}
: {
count: Number(ticks.count),
state: ticks.state,
total: Number(ticks.total),
},
uuid: inner.Ticks.uuid,
progress: !ticks
? {
count: 0,
state: undefined,
total: undefined,
}
: {
count: Number(ticks.count),
state: ticks.state,
total: Number(ticks.total),
},
},
};
} else if ('Stopped' in inner) {
return { Stopped: inner.Stopped?.uuid };
if (!inner.Stopped) {
return new Error(
`Has been recieved LifecycleTransition.Stopped without even definition`,
);
}
return { Stopped: inner.Stopped.uuid };
} else {
throw new Error(`Fail to parse event: ${JSON.stringify(event)}`);
}
Expand Down

0 comments on commit 68e6052

Please sign in to comment.