Skip to content

Commit

Permalink
Ensures Ping on Server Worker Startup
Browse files Browse the repository at this point in the history
In case Chrome's scheduling isn't quite there.
  • Loading branch information
Step7750 committed May 22, 2024
1 parent 6282ff6 commit bc62c8b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import {Handle} from './lib/bridge/server';
import {InternalResponseBundle} from './lib/bridge/types';
import MessageSender = chrome.runtime.MessageSender;
import {alarmListener, registerTradeAlarmIfPossible} from './lib/alarms/setup';
import {pingTradeStatus} from './lib/alarms/csfloat_trade_pings';
import {gStore} from './lib/storage/store';
import {StorageKey} from './lib/storage/keys';

function unifiedHandler(request: any, sender: MessageSender, sendResponse: (response?: any) => void) {
Handle(request, sender)
Expand Down Expand Up @@ -51,3 +54,15 @@ async function checkAlarmState() {
}

checkAlarmState();

// Ping trade status upon service worker wake-up
// Why do this even though there's an alarm? Well, some people turn on their device briefly to send a trade offer
// then close it quickly, it's hard to rely on Chrome's scheduling in that case
async function checkTradeStatus() {
const lastPing = await gStore.getWithStorage<number>(chrome.storage.local, StorageKey.LAST_TRADE_PING_ATTEMPT);
if (!lastPing || lastPing < Date.now() - 3 * 60 * 1000) {
// Last ping was over 3 minutes ago
pingTradeStatus();
}
}
checkTradeStatus();
4 changes: 4 additions & 0 deletions src/lib/alarms/csfloat_trade_pings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import {pingCancelTrades, pingSentTradeOffers} from './trade_offer';
import {HasPermissions} from '../bridge/handlers/has_permissions';
import {PingExtensionStatus} from '../bridge/handlers/ping_extension_status';
import {AccessToken, getAccessToken} from './access_token';
import {gStore} from '../storage/store';
import {StorageKey} from '../storage/keys';

export const PING_CSFLOAT_TRADE_STATUS_ALARM_NAME = 'ping_csfloat_trade_status_alarm';

export async function pingTradeStatus() {
await gStore.setWithStorage(chrome.storage.local, StorageKey.LAST_TRADE_PING_ATTEMPT, Date.now());

const hasPermissions = await HasPermissions.handleRequest(
{
permissions: [],
Expand Down
1 change: 1 addition & 0 deletions src/lib/storage/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export enum StorageKey {
ITEM_FILTERS = 'expressions',
GLOBAL_FILTERS = 'global',
ACCESS_TOKEN = 'access_token',
LAST_TRADE_PING_ATTEMPT = 'last_trade_ping_attempt',
}

export type DynamicStorageKey = string;
Expand Down

0 comments on commit bc62c8b

Please sign in to comment.