Skip to content

Instantly share code, notes, and snippets.

@edongashi
edongashi / react-scope.tsx
Last active December 21, 2022 23:56
Simple wrapper around react context for dynamic initialization.
import React, { useContext, useState } from 'react';
export type Scope<TValue, TInitialState = undefined> = {
Provider: React.FunctionComponent<TInitialState extends undefined ? {} : { initialState: TInitialState }>;
useValue: () => TValue;
RawContext: React.Context<TValue>;
};
export function createScope<TValue, TInitialState = undefined>(
factory: (initialState: TInitialState) => TValue,
// ==UserScript==
// @name BGG Sumary Cards
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Show game stats on link hover.
// @author Edon Gashi
// @match https://boardgamegeek.com/*
// @icon https://www.google.com/s2/favicons?domain=boardgamegeek.com
// @grant none
// @require https://code.jquery.com/jquery-3.6.0.min.js
@edongashi
edongashi / measure.ex
Created July 17, 2020 07:38
Measure execution of a block
defmacro measure(name, do: block) do
quote do
IO.puts("Starting #{unquote(name)}...")
{time, result} =
:timer.tc(fn ->
unquote(block)
end)
IO.puts("#{unquote(name)} done in #{time / 1_000} ms")
@edongashi
edongashi / wretch.js
Created May 5, 2020 08:10
Node wretch setup
// npm install wretch node-fetch form-data
const wretch = require('wretch')
wretch().polyfills({
fetch: require('node-fetch'),
FormData: require('form-data'),
URLSearchParams: require('url').URLSearchParams
})
// This icon file is generated by build/generateIcons.ts
// tslint:disable
export { default as AlertFill } from '@ant-design/icons/lib/fill/AlertFill'
export { default as AlipayCircleFill } from '@ant-design/icons/lib/fill/AlipayCircleFill'
export { default as AlipaySquareFill } from '@ant-design/icons/lib/fill/AlipaySquareFill'
export { default as AmazonCircleFill } from '@ant-design/icons/lib/fill/AmazonCircleFill'
export { default as AliwangwangFill } from '@ant-design/icons/lib/fill/AliwangwangFill'
export { default as AmazonSquareFill } from '@ant-design/icons/lib/fill/AmazonSquareFill'
export { default as AccountBookFill } from '@ant-design/icons/lib/fill/AccountBookFill'
export { default as ApiFill } from '@ant-design/icons/lib/fill/ApiFill'
/**
* Wraps a synchronous function to also return execution time using process.hrtime().
* @param fn - function to measure
*/
function measure(fn) {
return function (...args) {
const [startSec, startNanoSec] = process.hrtime()
const result = fn(...args)
const [finishSec, finishNanoSec] = process.hrtime()
const ms = (finishSec - startSec) * 1000 + (finishNanoSec - startNanoSec) / 1000000
@edongashi
edongashi / FileWatcher.cs
Last active January 21, 2018 19:00
Tracks the content of a single file.
/// <summary>
/// Tracks the content of a single file.
/// </summary>
public sealed class FileWatcher : IDisposable, INotifyPropertyChanged
{
private class Watcher : IDisposable
{
private readonly List<FileWatcher> listeners;
private readonly FileSystemWatcher fileSystemWatcher;
private readonly string filePath;