Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | 1x 1x 1x 1x 1x 4x 7x 8x 4x 4x 4x 4x 4x 4x 4x 4x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 4x 4x | import { v4 as uuidv4 } from 'uuid';
import { ApplicationConfiguration } from './configuration';
import { QueueEntity } from './queue-entity';
import { CreateTransactionPayload } from './protos/types/transaction';
const EVENTS = {
STARTED: Symbol(),
FINISHED: Symbol(),
CANCELLED: Symbol(),
};
export const TRANSACTION_EVENTS = EVENTS;
export interface TransactionMetadata {
[key: string]: any;
}
export class Transaction extends QueueEntity {
public parent: Transaction;
public name: string;
public trackingKey: string;
public developerKey: string;
public metadata: TransactionMetadata = {};
public startedAt: Date;
public finishedAt: Date;
public cancelledAt: Date;
private config: ApplicationConfiguration;
get isFinished(): boolean {
return this.startedAt !== undefined && this.finishedAt !== undefined;
}
get isCancelled(): boolean {
return this.cancelledAt !== undefined;
}
constructor(name: string, config: ApplicationConfiguration) {
super();
this.config = config;
this.trackingKey = uuidv4();
this.name = name;
}
public start(): Transaction {
Iif (this.isCancelled) {
throw new Error('Transaction already cancelled at ' + this.cancelledAt);
}
this.startedAt = new Date();
this.emit(EVENTS.STARTED);
return this;
}
public finish(): Transaction {
Iif (this.isCancelled) {
throw new Error('Transaction already cancelled at ' + this.cancelledAt);
}
Iif (this.isFinished) {
throw new Error('Transaction already finished at ' + this.finishedAt);
}
Iif (!this.startedAt) {
throw new Error('Transaction was never started');
}
this.finishedAt = new Date();
this.emit(EVENTS.FINISHED);
this.enqueue();
return this;
}
public cancel(): this {
Iif (this.isFinished) {
throw new Error('Transaction already finished at ' + this.finishedAt);
}
this.cancelledAt = this.cancelledAt || new Date();
this.emit(EVENTS.CANCELLED);
super.cancel();
return this;
}
public withDeveloperKey(developerKey: string): Transaction {
this.developerKey = developerKey;
return this;
}
public withMetadata(metadata: TransactionMetadata): Transaction {
this.metadata = {
...this.metadata,
...metadata
};
return this;
}
public fromTransaction(transaction: Transaction): Transaction {
this.parent = transaction;
return this;
}
public toMessage(): CreateTransactionPayload {
return {
applicationKey: this.config.key,
developerKey: this.developerKey,
name: this.name,
trackingKey: this.trackingKey,
parentTrackingKey: this.parent
? this.parent.trackingKey
: '',
startedAt: this.startedAt.getTime(),
finishedAt: this.finishedAt.getTime(),
metadata: this.metadata
};
}
}
|