Skip to content

Instantly share code, notes, and snippets.

View DIY0R's full-sized avatar
☢️
Radiation

Sobirov Diyorbek DIY0R

☢️
Radiation
View GitHub Profile
@DIY0R
DIY0R / streams.js
Created September 13, 2024 10:20
Cancelling streams with AbortController
import fs from "fs";
import { addAbortSignal } from "stream";
import { setTimeout as delay } from "timers/promises";
const controller = new AbortController();
setTimeout(() => controller.abort(), 50);
const inputStream = addAbortSignal(
controller.signal,
fs.createReadStream("text.txt")
@DIY0R
DIY0R / chain.js
Created September 12, 2024 15:49
function chain(prev = null) {
const cur = () => {
if (cur.prev) {
cur.prev.next = cur;
cur.prev();
} else {
cur.forward();
}
};
let obj = {};
let weakRef = new WeakRef(obj);
let finalizationRegistry = new FinalizationRegistry(() => {
console.log('Object has been collected');
});
finalizationRegistry.register(obj);
obj = null;
class AsyncTaskQueue {
private tasks: (() => Promise<void>)[] = [];
private isRunning = false;
public addTask(task: () => Promise<void>, signal?: AbortSignal): Promise<void> {
return new Promise((resolve, reject) => {
const wrappedTask = async () => {
if (signal?.aborted) return reject(new Error('Task aborted'));
try {
await task();
const promisify = (fn) => (...args) => {
const promise = new Promise((resolve, reject) => {
const callback = (err, data) => {
if (err) reject(err);
else resolve(data);
};
fn(...args, callback);
});
return promise;
};
{
"editor.formatOnSave": true,
"editor.suggestSelection": "first",
"terminal.integrated.fontFamily": "'Cascadia Code',sans-serif",
"terminal.integrated.fontSize": 14,
"workbench.iconTheme": "catppuccin-perfect-latte",
"editor.fontFamily": "'Cascadia Code',sans-serif",
// "editor.fontFamily": " 'Fira Code', sans-serif",
// "editor.fontFamily": "Operator Mono Medium",
// "synthwave84.brightness": 0.45,
@DIY0R
DIY0R / convert.js
Last active July 29, 2024 10:56
JavaScript
function convertQueryToMap(query) {
const result = {};
if (!query) return result;
query.split('&').forEach((pair) => {
const [pathway, value] = pair.split('=');
const pathwaySplit = pathway.split('.');
let obj = result;
pathwaySplit.forEach((prop, index) => {
if (index === pathwaySplit.length - 1) {
obj[prop] = decodeURIComponent(value);
Reset = "\x1b[0m%s\x1b[0m"
Bright = "\x1b[1m%s\x1b[0m"
Dim = "\x1b[2m%s\x1b[0m"
Underscore = "\x1b[4m%s\x1b[0m"
Blink = "\x1b[5m%s\x1b[0m"
Reverse = "\x1b[7m%s\x1b[0m"
Hidden = "\x1b[8m%s\x1b[0m"
FgBlack = "\x1b[30m%s\x1b[0m"
FgRed = "\x1b[31m%s\x1b[0m"