Skip to content

Instantly share code, notes, and snippets.

View freddi301's full-sized avatar

Frederik Batuna freddi301

View GitHub Profile
@freddi301
freddi301 / styled-components-react.d.ts
Last active May 12, 2023 09:34
typescript style-components lite
import type { CSSProp } from "styled-components/index";
declare module "react" {
interface Attributes {
css?: CSSProp;
css?: any // if compilation times becomes too high
}
}
// import "styled-components/macro" must be imported in every file that uses css={`color: red`}
@freddi301
freddi301 / teams-for-linux.sh
Created March 13, 2020 12:39
teams for linux in docker
#!/usr/bin/env bash
cat > Dockerfile << EOF
FROM ubuntu
RUN apt-get update
RUN apt-get install -y wget
RUN wget -O teams-for-linux.deb --quiet "https://teams.microsoft.com/downloads/desktopurl?env=production&plat=linux&arch=x64&download=true&linuxArchiveType=deb"
@freddi301
freddi301 / notes-p2p-implementation.md
Created March 12, 2020 18:07
Notes about implementing p2p

docker run --rm -it $(docker build -q .)

docker build -t gobi301/p2p-network-rust . && docker push gobi301/p2p-network-rust:latest

Features

  • nat traversal
  • dht
  • lan discovery
@freddi301
freddi301 / rust_hooks.rs
Created February 6, 2020 16:30
Rust hooks
use std::assert;
use std::rc::Rc;
mod rust_hooks {
use std::cell::{Cell, RefCell};
use std::rc::Rc;
pub trait Notifiable {
fn notify(&self);
}
@freddi301
freddi301 / getBroadcastAddress.ts
Last active January 25, 2020 14:54
calculate ipv4 broadcast address typescript
function getBroadcastAddress({ address, netmask }: NetworkInterfaceInfo) {
const addressBytes = address.split(".").map(Number);
const netmaskBytes = netmask.split(".").map(Number);
const subnetBytes = netmaskBytes.map(
(_, index) => addressBytes[index] & netmaskBytes[index]
);
const broadcastBytes = netmaskBytes.map(
(_, index) => subnetBytes[index] | (~netmaskBytes[index] + 256)
);
return broadcastBytes.map(String).join(".")
@freddi301
freddi301 / channel.ts
Last active October 18, 2019 10:25
Channel based coroutines with await syntax and backpressure
/*
Channel based coroutines with await syntax and backpressure
`await send(item);` blocks execution until a corresponding `const item = await receive()` is called
`const item = await receive();` blocks execution until a corresponding `await send(item);` is called
the send function should be used by a single producer (for code readability)
the receive function should be used by a single consumer (for code readabilty)
see examples at bottom
a channel is single use (if multiple threads subscribe, every thread will get an item at round robin)
if multicast semantics are needed, this could be accomodated with an operator
backpressure is for free, is send and receive are awaited
@freddi301
freddi301 / useLocalStorage.ts
Last active October 11, 2019 19:21
useLocalStorage #react #hook
import { useState } from "react";
export function useLocalStorage<T>(key: string, initialValue: T) {
const [storedValue, setStoredValue] = useState(() => {
try {
const item = window.localStorage.getItem(key);
return item ? JSON.parse(item) : initialValue;
} catch (error) {
return initialValue;
}
@freddi301
freddi301 / useScheduledEffect.tsx
Created October 11, 2019 10:01
useScheduledEffect #react #hook
import { useEffect, useRef } from "react";
type TimeoutCallback = (timestamp: number) => void;
export function useScheduledEffect(
callback: TimeoutCallback,
timestamp: number | null,
) {
const savedCallback = useRef<TimeoutCallback | null>(callback);
@freddi301
freddi301 / useReferentialCallback.tsx
Created October 11, 2019 10:00
useReferentialCallback: do not rerender on callback intance change #react #hook
import React from "react";
export function useReferentialCallback<F extends (...args: any[]) => any>(
fn: F,
deps?: any[],
): F {
const fnRef = React.useRef<F>(fn);
React.useLayoutEffect(() => {
fnRef.current = fn;
}, deps); // eslint-disable-line
@freddi301
freddi301 / usePersistentState.tsx
Created October 11, 2019 09:59
usePersistentState LocalStorage/SessionStorage #react #hook
import React, { Dispatch, SetStateAction } from "react";
type ViolationMessage = string | (() => string);
function useStableValue<V>(
value: V,
message: ViolationMessage = "Violation: cannot change value between re-renders",
) {
const ref = React.useRef(value);