Skip to content

Instantly share code, notes, and snippets.

View dawiidio's full-sized avatar

Dawid Wojda dawiidio

View GitHub Profile
@dawiidio
dawiidio / higrometr.ino
Last active February 5, 2023 20:00
Higrometr Szymona
/**************************************************************************************
This is example for ClosedCube HDC1080 Humidity and Temperature Sensor breakout booard
Initial Date: 13-May-2016
Hardware connections for Arduino Uno:
VDD to 3.3V DC
SCL to A5
SDA to A4
GND to common ground
Written by AA for ClosedCube
MIT License
@dawiidio
dawiidio / masonryClipPath.ts
Created April 22, 2022 12:30
generate masonry clip path
type Last<T extends string[]> = T extends [...infer _, infer Last] ? Last : never;
type ConcatPrevious<Values extends string, T extends any[]> = Last<T> extends string ? `${Last<T>}${Values}` : never
type Mapped<N extends number,
Values extends string,
Result extends Array<unknown> = [Values],
> =
(Result['length'] extends N
? Result
@dawiidio
dawiidio / configString.ts
Created April 20, 2022 20:22
Typed string in TypeScript with specified chars and max length
type Last<T extends string[]> = T extends [...infer _, infer Last] ? Last : never;
type ConcatPrevious<Values extends string, T extends any[]> = Last<T> extends string ? `${Last<T>}${Values}` : never
type Mapped<N extends number,
Values extends string,
Result extends Array<unknown> = [Values],
> =
(Result['length'] extends N
? Result
@dawiidio
dawiidio / map.js
Created January 25, 2022 21:10
Re-maps a number from one range to another
function map(n, start1, stop1, start2, stop2, withinBounds) {
const newval = (n - start1) / (stop1 - start1) * (stop2 - start2) + start2;
if (!withinBounds) {
return newval;
}
if (start2 < stop2) {
return Math.max(Math.min(newval, stop2), start2);
} else {
return Math.max(Math.min(newval, start2), stop2);
}
@dawiidio
dawiidio / GameRunner.ts
Last active January 24, 2022 19:41
Game runner with delta time smoothing
export type ListenerFn<E = any> = (event: E) => void;
export class EventEmitter {
private listeners: Map<string, Set<ListenerFn>> = new Map();
on<E = any>(eventName: string, listener: ListenerFn<E>): (() => void) {
if (!this.listeners.has(eventName)) {
this.listeners.set(eventName, new Set());
}
@dawiidio
dawiidio / EventEmitter.ts
Last active July 7, 2022 19:31
Base event emitter class in TypeScript
export type ListenerFn<E = any> = (eventName: string, event: E) => void;
export type OffListener = () => void;
export class EventEmitter {
private listeners: Map<string, Set<ListenerFn>> = new Map();
private locked = false;
on(eventName: string | '*', listener: ListenerFn): OffListener {
if (!this.listeners.has(eventName)) {
this.listeners.set(eventName, new Set());
@dawiidio
dawiidio / Cache.ts
Last active January 16, 2022 13:02
Small in-memory cache implementation
export interface CacheEntry<T> {
data: T;
expirationDate: number;
}
export class Cache<T, K = string> {
private cacheMap = new Map<K, CacheEntry<T>>();
constructor(private ttl: number) {
}
class Counter {
private child: Counter[] = [];
private value: number = 0;
private destroyed: boolean = false;
increase(): number {
if (this.destroyed)
throw new Error(`destroyed counter can not be increased`);
return ++this.value;
@dawiidio
dawiidio / factory.ts
Created September 16, 2021 20:23
Factory typings for TypeScript
interface Input <T = number> {
getValue(): T
}
class Input1 implements Input<{ value: string }> {
getValue() {
return {
value: 'input value'
}
@dawiidio
dawiidio / README.md
Last active April 5, 2021 18:24
Porównanie typów komponentów w React.js

Porównanie typów komponentów w React.js

Twórcy React.js zapowiedzieli wprowadzenie tzw. Server Components które zmienią podejście do tworzenia aplikacji webowych z użyciem reacta. W skrócie, mają one pomóc zmniejszyć bundle size przez to, że operacje potrzebne do wyrenderowania SC będą wykonywane na serwerze więc ich zależności także będą mogły tam pozostać (nie będą przesyłane do klienta) a do klienta przesłane zostaną tylko dane konieczne do poprawnego wyświetlenia komponentu, pozwoli to także na przyspieszenie samego procesu renderowania, ponieważ nie będziemy "zabierać" React'owi czasu z głównego wątku przeglądarki na przetwarzanie danych. Będziemy przesyłać (streamować) tylko dane potrzebne do pokazania po stronie klienta a React zajmie się już procesem łączenia obu drzew (reconcilation).

Powyższy opis tylko w bardzo abstrakcyjny sposób pokazuje jakie zmiany niesie za sobą wprowadzenie SC, jeśli chcesz dowiedzieć się więcej to tutaj link do posta i filmu zapowiadjącego SC [link do posta](https://react