Skip to content

Commit

Permalink
refactor: move concepts to their own files
Browse files Browse the repository at this point in the history
sendLogstream and link_poll_event have been moved to their respective
files
  • Loading branch information
Guillaume Malette committed Feb 26, 2021
1 parent 33ead86 commit a73a18a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 49 deletions.
23 changes: 23 additions & 0 deletions src/notification/link_poll_event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {Link, Store} from '../store/model';

type RequestFailed = {
failureReason: 'request_failed';
statusCode: number;
};
type Captcha = {failureReason: 'captcha'};
type MaxPriceExceeded = {failureReason: 'max_price'; maxPrice: number};
type OutOfStock = {failureReason: 'out_of_stock'};
type BannedSeller = {failureReason: 'banned_seller'};
type LinkPollError = {result: 'failure'} & (
| RequestFailed
| Captcha
| MaxPriceExceeded
| OutOfStock
| BannedSeller
);
type LinkPollSuccess = {result: 'in_stock'};

export type LinkPollEvent = (LinkPollError | LinkPollSuccess) & {
link: Link;
store: Store;
};
30 changes: 30 additions & 0 deletions src/notification/logstream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {Print, logger} from '../logger';
import {LinkPollEvent} from './link_poll_event';

function assertNever(x: never): never {
throw new Error('Unexpected object: ' + x);
}

export function sendLogstream(pollEvent: LinkPollEvent) {
const {link, store} = pollEvent;
if (pollEvent.result === 'failure') {
switch (pollEvent.failureReason) {
case 'captcha':
logger.warn(Print.captcha(link, store, true));
return;
case 'banned_seller':
logger.warn(Print.bannedSeller(link, store, true));
return;
case 'out_of_stock':
logger.info(Print.outOfStock(link, store, true));
return;
case 'max_price':
logger.info(Print.maxPrice(link, store, pollEvent.maxPrice, true));
return;
case 'request_failed':
return;
default:
assertNever(pollEvent);
}
}
}
54 changes: 6 additions & 48 deletions src/notification/notification.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {Link, Store} from '../store/model';
import {adjustPhilipsHueLights} from './philips-hue';
import {playSound} from './sound';
import {sendDesktopNotification} from './desktop';
Expand All @@ -17,57 +16,16 @@ import {sendTwitchMessage} from './twitch';
import {updateRedis} from './redis';
import {activateSmartthingsSwitch} from './smartthings';
import {sendStreamLabsAlert} from './streamlabs';
import {Print, logger} from '../logger';

type RequestFailed = {
failureReason: 'request_failed';
statusCode: number;
};
type Captcha = {failureReason: 'captcha'};
type MaxPriceExceeded = {failureReason: 'max_price'; maxPrice: number};
type OutOfStock = {failureReason: 'out_of_stock'};
type BannedSeller = {failureReason: 'banned_seller'};
type LinkPollError = {result: 'failure'} & (
| RequestFailed
| Captcha
| MaxPriceExceeded
| OutOfStock
| BannedSeller
);
type LinkPollSuccess = {result: 'in_stock'};

export type LinkPollEvent = (LinkPollError | LinkPollSuccess) & {
link: Link;
store: Store;
};
export type SendNotification = (pollEvent: LinkPollEvent) => void;

function assertNever(x: never): never {
throw new Error('Unexpected object: ' + x);
}
import {sendLogstream} from './logstream';
import {LinkPollEvent} from './link_poll_event';

export function sendNotification(pollEvent: LinkPollEvent) {
const {link, store} = pollEvent;
sendLogstream(pollEvent);
if (pollEvent.result === 'failure') {
switch (pollEvent.failureReason) {
case 'captcha':
logger.warn(Print.captcha(link, store, true));
return;
case 'banned_seller':
logger.warn(Print.bannedSeller(link, store, true));
return;
case 'out_of_stock':
logger.info(Print.outOfStock(link, store, true));
return;
case 'max_price':
logger.info(Print.maxPrice(link, store, pollEvent.maxPrice, true));
return;
case 'request_failed':
return;
default:
assertNever(pollEvent);
}
return;
}

const {link, store} = pollEvent;
// Priority
playSound();
sendDiscordMessage(link, store);
Expand Down
4 changes: 3 additions & 1 deletion src/store/lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ import {fetchLinks} from './fetch-links';
import {filterStoreLink} from './filter';
import open from 'open';
import {processBackoffDelay} from './model/helpers/backoff';
import {SendNotification} from '../notification';
import useProxy from '@doridian/puppeteer-page-proxy';
import {LinkPollEvent} from "../notification/link_poll_event";

const inStock: Record<string, boolean> = {};

const linkBuilderLastRunTimes: Record<string, number> = {};

export type SendNotification = (pollEvent: LinkPollEvent) => void;

function nextProxy(store: Store) {
if (!store.proxyList) {
return;
Expand Down

0 comments on commit a73a18a

Please sign in to comment.