Skip to content

Instantly share code, notes, and snippets.

View baptistemanson's full-sized avatar
🌼
Blooming

Baptiste Manson baptistemanson

🌼
Blooming
View GitHub Profile
// SOA
import { AppAccount, Collaborator } from "@prisma/client";
// list of all row type in the app
enum TableRowTypeString {
Collaborator,
AppAccount,
}
// list of all fields for app accounts
// AOS
import { AppAccount, Collaborator } from "@prisma/client";
// list of all row type in the app
enum TableRowType {
Collab,
AppAcc,
}
type Spec<A, B> = {
@baptistemanson
baptistemanson / history.tsx
Created April 11, 2024 16:39
history provider for next
import { atomWithStorage, createJSONStorage } from "jotai/utils";
const storage = createJSONStorage(() => sessionStorage);
export const historyAtom = atomWithStorage(
"history",
[window.location.href],
storage
);
// will give you access to the history in the component you wish
interface Jsonable {
[x: string]: string | number | boolean | Date | Jsonable | Jsonable[];
}
// errors that can be discriminated based on a type,
// contain a message
// and can have context extra info that are serializable to json.
interface IdeaError<ErrorType> {
type: ErrorType;
message: string;
extra: Jsonable;
@baptistemanson
baptistemanson / exampleErrorHandling.ts
Created February 16, 2023 16:58
An example of how error handling could look like in TS
// Better error handling in TS
// Exceptions in TS have a few issues:
// - they can't be typechecked
// - they can't be easily documented (this may fail)
// - the "catch" code is usually not colocated to what may fail, adding cognitive load
// - it's hard to differentiate between different errors
// - they are costly in performance
// - they can propagate all the way from the bottom to the top without the intermediate layers to be warned it may fail
// - anything can be thrown
@baptistemanson
baptistemanson / gist:d38c260b68acd64cbbb2cb764b00721d
Last active January 5, 2023 16:27
example hack for service worker
useEffect(() => {
if (!navigator.serviceWorker)
throw new Error("We only support browsers with service workers enabled");
// The trickery below appears to be necessary to register a compiled ServiceWorker with nextjs
// Webpack only compiles workers imported via the pattern `new Worker(new URL("script_to_be_compiled"))`
// So we need to use that exact expression and have it register a service worker instead
const RealWorker = window.Worker;
(
window as any
const DIM = 5;
/**
* -1-DIM -DIM +1-DIM
* -1 i +1
* -1+DIM +DIM +1+DIM
*/
const _compute = (m, out) => {
for (let x = 1; x < DIM; x++) {
for (let y = 1; y < DIM; y++) {
@baptistemanson
baptistemanson / struct.js
Created February 10, 2021 20:37
demo hashmap to structs
const fs = require("fs");
function dateCompress(d) {
return d.replace(/\.\d{3}\+\d{2}:\d{2}$/g, "Z");
}
function getSize(d) {
return Math.ceil(d.length / 1024).toLocaleString() + "kB";
}
thread 'main' panicked at 'assertion failed: `(left == right)`
left: `Ok(())`,
right: `Err(ERROR_DEVICE_LOST)`', C:\Users\Baptiste\.cargo\git\checkouts\gfx-e86e7f3ebdbc4218\0244e34\src\backend\vulkan\src\lib.rs:1476:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'main' panicked at 'assertion failed: `(left == right)`
left: `1`,
right: `0`: Allocator dropped before all sets were deallocated', C:\Users\Baptiste\.cargo\git\checkouts\gpu-descriptor-a05b99db0270c787\df74fd8\gpu-descriptor\src\allocator.rs:117:9
stack backtrace:
0: 0x7ff7e7edea0e - std::backtrace_rs::backtrace::dbghelp::trace
at /rustc/e1884a8e3c3e813aada8254edfa120e85bf5ffca\/library\std\src\..\..\backtrace\src\backtrace\dbghelp.rs:108
@baptistemanson
baptistemanson / gist:b2e1dd24022b493511203a217a215c39
Last active July 9, 2020 17:45
How many collision can I test in JS per frame.js
function isInside(pos, rect) {
return (
pos[0] > rect[0] && pos[1] > rect[1] && pos[0] < rect[2] && pos[1] < rect[2]
);
}
const rect1 = new Uint16Array([10, 10, 100, 1000]);
const rect2 = new Uint16Array([0, 0, 10, 10]);
const pos1 = new Uint16Array([250, 50]);
const pos2 = new Uint16Array([20, 20]);