Skip to content

Instantly share code, notes, and snippets.

View mendes5's full-sized avatar
📉
[object Object]

mendes5

📉
[object Object]
View GitHub Profile
@mendes5
mendes5 / useExport.js
Created August 22, 2023 19:12
Allows to print a JSX subtree, usefull for export as PDF functionality.
const useExport = (tree, documentTitle, ready) => {
const containerRef = useRef(null);
const frameRef = useRef();
const [readyToExport, setReadyToExport] = useState(false);
useEffect(() => {
if (!ready) return;
const sheet = new ServerStyleSheet();
#include <math.h>
double converge(double current, double target, double step_size) {
double direction = target - current;
direction = (direction > 0) - (direction < 0);
double remaining = target - current;
double step = direction * step_size;
return current + fmin(step, direction * remaining);
}
const MAX_HEAP_SIZE = 5000;
let HEAP_BASE = 0;
const HEAP = new ArrayBuffer(MAX_HEAP_SIZE);
const seal = () => HEAP.slice(0, HEAP_BASE);
const reset = () => {
HEAP_BASE = 0;
new Uint8Array(HEAP).fill(0);
@mendes5
mendes5 / hack-2.js
Created July 7, 2023 13:49
Detect and debug WHY there are null bytes on es5-ext
const fs = require('fs');
const inspector = require('node:inspector');
const { statSync } = fs;
Object.assign(fs, {
statSync() {
if (arguments[0].includes('\x00')) {
console.log('YOU ACTIVATED MY TRAP CARD!!!', arguments[0].split(''));
inspector.open();
inspector.waitForDebugger();
const fs = require('fs');
const { stat, statfs, statSync, statfsSync } = fs;
Object.assign(fs, {
stat(){
console.log('Running stat', arguments[0], new Error().stack);
return stat.call(fs, ...[...arguments]);
},
statfs(){
@mendes5
mendes5 / portal.jsx
Created February 16, 2023 19:33
React portals as they were in my head before learning how createPortal actually works
import { render } from "react-dom";
import { atom, useAtom } from "jotai";
import { useEffect, useState } from "react";
const portalGun = () => {
const Children = atom(null);
const EntryPortal = ({ children }) => {
const [, setChildren] = useAtom(Children);
#![feature(local_key_cell_methods)]
use std::{cell::RefCell, collections::HashMap, rc::Rc};
/// Traits are separated into RuntimeManaged and ParentManaged because
/// in the COMPONENTS thread_local we need to erase the prop type since we
/// don't know it there (and we don't need props there too).
///
/// Luckly for us Rust automatically downcasts a `Rc<RefCell<Component<T>>>` to `Rc<RefCell<dyn RuntimeManaged>>`
/// when we clone the a component to the thread_local, while still keeping the original concrete type
@mendes5
mendes5 / traits.rs
Created February 7, 2023 13:58
Traits, sub-traits and dyn Trait
use std::{rc::Rc, vec};
trait Unsafe {
// where Self: Sized allows us to hold
// values implementing this in arrays
// and pass it as function arguments as &dyn Unsafe
// or Box<dyn Unsafe> without not object-safe errors.
fn create() -> Self where Self: Sized;
}
@mendes5
mendes5 / let-him-cook.rs
Created February 2, 2023 04:04
Can reactive declarative programming in rust be easier?
#![feature(local_key_cell_methods)]
use std::{cell::RefCell, collections::HashMap};
// Core idea: Rust lets you do this:
fn create_closure(initial: u32) -> Box<dyn FnMut()> {
let mut captured = initial;
Box::new(move || {
captured += 1;
fn mount<T, P, B: FnMut(&mut P)>(
component: fn(this: Option<T>, props: P, block: B) -> T,
props: P,
block: B,
) {
component(None, props, block);
}
struct Props<T: FnMut()> {
on_click: T,