Skip to content

Commit

Permalink
Pipeline Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Mar 20, 2021
1 parent 3e36126 commit a94ff8c
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 41 deletions.
5 changes: 3 additions & 2 deletions app/src/trading/logic/calculatePrices.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { OfferContext, Next } from '../pipeline';
import { NextFunction } from '../../util/middleware';
import { OfferContext } from '../types'
import { getAllItemsPrice } from '../../steam/market';
import { ItemPrice } from '../types';
import { AccountOptions } from '../../account/account';

export default async function middleware(context: OfferContext, next: Next) {
export default async function middleware(context: OfferContext, next: NextFunction) {
const { processor, offer } = context;

context.receiveItemsPrices = await getAllItemsPrice(offer.itemsToReceive);
Expand Down
5 changes: 3 additions & 2 deletions app/src/trading/logic/checkOverpay.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { OfferContext, Next } from '../pipeline';
import { NextFunction } from '../../util/middleware';
import { OfferContext } from '../types';

export default function middleware(context: OfferContext, next: Next) {
export default function middleware(context: OfferContext, next: NextFunction) {
context.profit = context.receivePrice - context.givePrice;

const { processor, offer, profit } = context;
Expand Down
5 changes: 3 additions & 2 deletions app/src/trading/logic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import checkSides from './checkSides';
import unmarketable from './unmarketable';
import calculatePrices from './calculatePrices';
import checkOverpay from './checkOverpay';
import { Middleware, OfferContext } from '../pipeline';
import { Middleware } from '../../util/middleware';
import { OfferContext } from '../types';

function thenAccept(context: OfferContext) {
const { processor, offer } = context;
processor.accept(offer);
}

const logic: Middleware[] = [glitched, isOwner, checkSides, unmarketable, calculatePrices, checkOverpay, thenAccept];
const logic: Middleware<OfferContext>[] = [glitched, isOwner, checkSides, unmarketable, calculatePrices, checkOverpay, thenAccept];

export default logic;
35 changes: 0 additions & 35 deletions app/src/trading/pipeline.ts

This file was deleted.

12 changes: 12 additions & 0 deletions app/src/trading/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Processor from './processor';

export interface Partner {
getSteamID64(): string;
}
Expand Down Expand Up @@ -32,3 +34,13 @@ export interface Offer {
export interface Community {
checkConfirmations: (callback?: (err: null | Error) => void) => void;
}

export type OfferContext = {
readonly processor: Processor;
readonly offer: Offer;
receiveItemsPrices: ItemPrice[];
receivePrice: number;
giveItemsPrices: ItemPrice[];
givePrice: number;
profit: number,
};
28 changes: 28 additions & 0 deletions app/src/util/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

export type NextFunction = () => Promise<void> | void;

export type Middleware<T> = (context: T, next: NextFunction) => void;

export default class Pipeline<T> {

constructor(private middlewares: Middleware<T>[] = []) {}

use(...middlewares: Middleware<T>[]) {
this.middlewares = middlewares;
}

add(...middlewares: Middleware<T>[]) {
this.middlewares.push(...middlewares);
}

async execute(context: T) {
await this.run(context, [...this.middlewares]);
}

private async run(context: T, middlewares: Middleware<T>[]) {
if (middlewares.length !== 0) {
const next = middlewares.shift();
next && await next(context, async () => await this.run(context, middlewares));
}
}
}

0 comments on commit a94ff8c

Please sign in to comment.