Skip to content

Instantly share code, notes, and snippets.

@sidola
sidola / Config.java
Last active April 12, 2024 01:52
SPA / Spring wildcard resource serve - This is how you configure Spring Boot to work with for example react-router. It will serve the `index.html` file on all routes that aren't used by REST controllers.
import java.io.IOException;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.resource.PathResourceResolver;
@Configuration
public class Config implements WebMvcConfigurer {

React Rendering Cheat Sheet

Last updated: 2023-10-13 (React v18)

Personal cheat sheet for react rendering details. Most of the stuff here is sourced from Mark Erikson's great blogpost. Don't trust anything you read here, go read his post instead.

Code examples can be found here: website, repo


@sidola
sidola / pubsub.ts
Last active January 24, 2023 05:13
Basic typesafe pub-sub implementation in Typescript
/* ------------------------------------------
Alternative impl. as a class can be found here: https://gist.github.com/sidola/eaf987d8c4c7e8445b61dc07c33a842f
Has a way smaller footprint and less typescript magic to grasp.
------------------------------------------ */
/**
* Defines the function type of the publish function.
*
@sidola
sidola / DownloadToString.ahk
Created May 3, 2014 17:10
AHK - Download HTML data to a variable
DownloadToString(url, encoding = "utf-8")
{
static a := "AutoHotkey/" A_AhkVersion
if (!DllCall("LoadLibrary", "str", "wininet") || !(h := DllCall("wininet\InternetOpen", "str", a, "uint", 1, "ptr", 0, "ptr", 0, "uint", 0, "ptr")))
return 0
c := s := 0, o := ""
if (f := DllCall("wininet\InternetOpenUrl", "ptr", h, "str", url, "ptr", 0, "uint", 0, "uint", 0x80003000, "ptr", 0, "ptr"))
{
while (DllCall("wininet\InternetQueryDataAvailable", "ptr", f, "uint*", s, "uint", 0, "ptr", 0) && s > 0)
{
@sidola
sidola / PostClick.ahk
Created June 6, 2014 08:20
AHK - Send click to hidden window
PostClick(x, y, class, title)
{
lParam := x & 0xFFFF | (y & 0xFFFF) << 16
PostMessage, 0x201, 1, %lParam%, %class%, %title% ;WM_LBUTTONDOWN
PostMessage, 0x202, 0, %lParam%, %class%, %title% ;WM_LBUTTONUP
}
@sidola
sidola / assume-defined.ts
Created July 5, 2022 07:45
Ensures a given property is defined and changes its type accordingly.
/**
* Creates a new type where prop T on object K is required and
* non-nullable.
*/
type DefinedProp<T, K extends keyof T> = (
Omit<T, K> &
{ [key in K]-?: Exclude<T[key], undefined> }
)
/**
@sidola
sidola / gitconfig.properties
Last active May 18, 2022 11:49
Misc git aliases
[alias]
# Undo the latest commit without deleting the files
pop-head = reset HEAD~1
# Force push with a lease
# https://git-scm.com/docs/git-push#Documentation/git-push.txt---no-force-with-lease
push-lease = push --force-with-lease
# Rename the previous commit
rename = commit --amend
# Lists all branches
branches = branch -l
@sidola
sidola / pub-sub-class.ts
Last active May 14, 2022 11:23
Experimental PubSub impl. as a class
class PubSub<T> {
private handlers: { [key: string]: any[] } = {}
public subscribe<E extends keyof T & string>(
event: E,
callback: (message: T[E]) => void
) {
const list = this.handlers[event] ?? []
list.push(callback)
/**
* Calls the given list of promises in sequence, ensuring a minimum
* delay of `staggerMs` between calls.
*
* @param funcs Array of promises to call.
* @param staggerMs The minimum time between promise calls. If a
* promise runs longer than this, the next one is fired as soon as the
* previous is done.
*/
export async function staggerCalls(funcs: (() => Promise<any>)[], staggerMs: number) {
@sidola
sidola / typescriptreact.json
Created January 5, 2022 09:37
TSX Snippets VSCode
{
// https://snippet-generator.app/
"React component": {
"prefix": "rcomp",
"body": [
"type ${1:Component}Props = {",
"",
"}",
"",
"export const ${1:Component} = (props: ${1:Component}Props): JSX.Element | null => {",