Skip to content

Instantly share code, notes, and snippets.

View Battlesquid's full-sized avatar
🌧️

Oluwamayowa Esan Battlesquid

🌧️
View GitHub Profile
@Battlesquid
Battlesquid / payload_queue.ts
Created March 18, 2025 21:47
Supabase Realtime helper class for batching events
export type OnPayloadQueueFlush<T> = (items: T[]) => void | Promise<void>;
export interface PayloadQueueOptions<T> {
/**
* The interval at which the queue should be flushed
*/
flushTimeout?: number;
/**
* The callback function to call when items are flished
@Battlesquid
Battlesquid / multisorts.ts
Last active June 21, 2024 02:03
Functions for applying multiple sorting conditions at a time
type SortFunction<T> = (a: T, b: T) => number;
interface SortRule<T> {
sort: SortFunction<T>;
asc?: boolean;
}
const multisort = <T>(items: T[], sorts: SortFunction<T>[], asc?: boolean) => {
const copy = [...items];
copy.sort((a, b) => {
@Battlesquid
Battlesquid / smelter.lua
Created May 31, 2024 00:27
smelter.lua
local args = { ... }
if #args ~= 2 then
print("usage: furnace <input inventory id> <output inventory id>")
return
end
MAX_SMELTING_STACK_SIZE = 16
local depots = { peripheral.find("create:depot") }
type TryFunction<T> = () => Promise<T>;
type TryResultSuccess<T> = {
ok: true;
result: T;
error: null;
}
type TryResultFailure = {
ok: false;
@Battlesquid
Battlesquid / layout_new_sidebar.fxml
Last active February 14, 2024 19:12
New layout with vertical menu
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Accordion?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TitledPane?>
@Battlesquid
Battlesquid / layout.fxml
Created February 14, 2024 17:47
New Layout
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Accordion?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TitledPane?>
@Battlesquid
Battlesquid / background.js
Created February 5, 2024 17:26
Response time service worker
const timings = {};
chrome.webRequest.onSendHeaders.addListener(
function (details) {
timings[details.url] = Date.now();
},
{ urls: ["<all_urls>"], types: ["xmlhttprequest"] }
);
chrome.webRequest.onHeadersReceived.addListener(
@Battlesquid
Battlesquid / partitionSettledPromises.ts
Created January 25, 2024 15:52
Partitions the results from Promise.allSettled into resolved and rejected lists
export const partitionSettledPromises = <ResolvedType, OriginalType>(original: OriginalType[], settled: PromiseSettledResult<ResolvedType>[]): [ResolvedType[], OriginalType[]] => {
const resolved: ResolvedType[] = [];
const rejected: OriginalType[] = [];
for (let i = 0; i < settled.length; i++) {
const job = settled[i];
if (job.status === "fulfilled") {
resolved.push(job.value);
} else {
rejected.push(original[i]);
}
@Battlesquid
Battlesquid / categorize.ts
Last active January 25, 2024 15:50
categorize function
export type Category = {
value: string;
count: number;
}
export const categorize = <T extends string>(items: T[]) => {
const results: Category[] = [];
items.forEach(item => {
const index = results.findIndex(r => r.value === item);
index === -1
@Battlesquid
Battlesquid / chunk.ts
Created January 24, 2024 16:41
lodash chunk but with reduce
const chunk = <T>(items: T[], size: number) => {
return items.reduce<T[][]>((chunks, curr, i) => {
(chunks[Math.floor(i / size)] ??= []).push(curr);
return chunks;
}, []);
}