Skip to content

Instantly share code, notes, and snippets.

View CMCDragonkai's full-sized avatar
🚀
Lightspeed

CMCDragonkai

🚀
Lightspeed
View GitHub Profile
@CMCDragonkai
CMCDragonkai / reload_dns_cache_network_manager.md
Created June 19, 2022 06:39
Reload the DNS Cache in Network Manager

Reload the DNS Cache in Network Manager

You may be using NetworkManager with dnsmasq as the DNS plugin. If so, it's being used as a local caching nameserver.

When its data is outdated, you can force it to reload its DNS cache with:

Use:

nmcli general reload dns-full
@CMCDragonkai
CMCDragonkai / compound_growth_rate.md
Created February 10, 2022 00:45
Compound Growth (Interest) Rate

Compound Growth (Interest) Rate

Suppose something grew 300% over 20 years, and you want to know what was the annual growth rate.

Use the compound interest rate formula:

A = P(1 + r/n)^(n*t)
@CMCDragonkai
CMCDragonkai / parse.ts
Last active February 1, 2022 01:06
General Data Validation #typescript #javascript
import { CustomError } from 'ts-custom-error';
class ErrorParse extends CustomError {
public readonly errors: Array<ErrorRule>;
constructor(errors: Array<ErrorRule>) {
const message = errors.map((e) => e.message).join('; ');
super(message);
this.errors = errors;
}
}
@CMCDragonkai
CMCDragonkai / EventBus.ts
Created January 16, 2022 08:47
EventBus with `emitAsync` #javascript #eventemitter
import { EventEmitter } from 'events';
class EventBus extends EventEmitter {
protected kCapture: symbol;
constructor (...args: ConstructorParameters<typeof EventEmitter>) {
super(...args);
// EventEmitter's captureRejections option is only accessible through a private symbol
// Here we augment the construction and save it as a property
@CMCDragonkai
CMCDragonkai / random_bytes_bits.ts
Created October 11, 2021 03:47
Random bytes and bits for Node.js #javascript #typescript
import crypto from 'crypto';
/**
* Gets random bytes as Uint8Array
*/
function randomBytes(size: number): Uint8Array {
return crypto.randomBytes(size);
}
/**
@CMCDragonkai
CMCDragonkai / range.ts
Last active October 10, 2021 09:23
Generators and Iterators in JavaScript #javascript #typescript
function* range(start: number, stop?: number, step = 1): Generator<number> {
if (stop == null) {
stop = start;
start = 0;
}
for (let i = start; step > 0 ? i < stop : i > stop; i += step) {
yield i;
}
}
@CMCDragonkai
CMCDragonkai / object_map.ts
Created October 5, 2021 03:58
Singleton Object Map using Locking #typescript #javascript
import { Mutex, MutexInterface } from 'async-mutex';
type ObjectId = string;
type Object = number;
type ObjectMap = Map<ObjectId, {
object?: Object;
lock: MutexInterface;
}>;
@CMCDragonkai
CMCDragonkai / rwlock-read.ts
Last active September 26, 2024 17:07
Read Write Lock #javascript #typescript #concurrency
import type { MutexInterface } from 'async-mutex';
import { Mutex } from 'async-mutex';
/**
* Single threaded read-preferring read write lock
*/
class RWLock {
protected _readerCount: number = 0;
protected _writerCount: number = 0;
protected lock: Mutex = new Mutex();
@CMCDragonkai
CMCDragonkai / concurrent_coalesced_async_construction.ts
Created September 28, 2021 03:45
Concurrently Coalesced Asynchronous Construction #typescript
import { Mutex, MutexInterface } from 'async-mutex';
type ObjectId = string;
type Object = number;
type ObjectMap = Map<ObjectId, {
object?: Object;
lock: MutexInterface;
}>;
@CMCDragonkai
CMCDragonkai / hybrid_methods.ts
Last active October 19, 2021 06:06
Hybrid Promise and Callback Methods #javascript #typescript
import callbackify from 'util-callbackify';
type Callback<P extends Array<any> = [], R = any, E extends Error = Error> = {
(e: E, ...params: Partial<P>): R;
(e?: null | undefined, ...params: P): R;
};
async function maybeCallback<T>(
f: () => Promise<T>,
callback?: Callback<[T]>