Skip to content

Instantly share code, notes, and snippets.

@mwmcode
mwmcode / vanilla-observable.ts
Created January 16, 2024 15:36 — forked from SgtPooki/vanilla-observable.ts
Typescript version of a simple Observable. This was copied from https://stackoverflow.com/a/62002044/592760, translated to TypeScript, and improved.
/**
* Modified by Russell Dempsey on 2021 DEC 15
* @see https://stackoverflow.com/a/62002044/592760
*/
type Subscriber<T> = (value: T) => void;
class Observable<T> {
private subscribers = new Set<Subscriber<T>>();
constructor(private value: T) {}
// TODO: make `pages` optional and measure the div when unspecified, this will
// allow more normal document flow and make it easier to do both mobile and
// desktop.
import {
createContext,
useCallback,
useContext,
useEffect,
useMemo,
useRef,
@mwmcode
mwmcode / PromptToInstall.md
Last active May 29, 2021 10:50
Prompt to install PWA Reac Provider

Prompt to install a React PWA

A React Provider/Consumer (hook) to capture beforeinstallprompt and display Install app? message to the user at a later time/event.

PropmptToInstall and usePromptToInstall

// PropmptToInstall.tsx
import React, { createContext, useState, useEffect, useContext, useCallback, ReactNode } from 'react';
import { IBeforeInstallPromptEvent, PromptCtx } from './types';

const PromptToInstall = createContext<PromptCtx>({deferredEvt: null});
@mwmcode
mwmcode / MultiSigWallet.sol
Created October 11, 2020 12:18
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.7.3+commit.9bfce1f6.js&optimize=undefined&gist=
pragma solidity ^0.7.3;
pragma experimental ABIEncoderV2;
contract MultiSigWallet {
uint minApprovers;
address payable beneficiary;
address payable owner;
mapping (address => bool) approvedBy;
mapping (address => bool) isApprover;
uint approvalsCount;
@mwmcode
mwmcode / Persist.js
Last active December 18, 2019 09:42
formik persist state (sessionStorage | localStorage) using hooks
import { useRef, useEffect } from 'react';
import useExtractFormikState from './useExtractFormikState';
type PersistProps = {
name: string;
type?: 'local' | 'session';
}
export default function FormikPersist(props: PersistProps): null {
const { name, type = 'session' } = props;
@mwmcode
mwmcode / algorithms.md
Created April 1, 2019 12:09
simple algorithms explanation (bubble sort, merge sort, quick sort and binary search)

Sorting

Buble sort

  • Compares two numbers and swaps them if the 2nd > 1st
  • Keeps repeating the process (up to length - 1 each iteration)
  • Simple but not a practical solution due to its heavy performance
@mwmcode
mwmcode / webpack.config.js
Created February 10, 2019 20:29
webpack config
// common
const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const Dotenv = require('dotenv-webpack');
const rootpath = path.resolve(__dirname, '..');
const fontRegex = /.(eot|ttf|woff|otf|woff2)$/;
const imgRegex = /.(png|jpg|gif|svg)$/;
const fileRegex = new RegExp(`(${fontRegex.source})|(${imgRegex.source})`);
{
"Ansi 5 Color" : {
"Red Component" : 0.8587691187858582,
"Color Space" : "Calibrated",
"Blue Component" : 0.8668023347854614,
"Alpha Component" : 1,
"Green Component" : 0.4679538607597351
},
"Tags" : [
@mwmcode
mwmcode / Elevator.js
Last active March 26, 2021 02:24
Simple elevator algorithm (return total stops of elevator)
// A [] queue of ppl and their weights
// B [] floors each person is going to
// M maximum floors of building
// X maximum people on elv
// Y maximum wight on elv
function solution(A, B, M, X, Y) {
// if queue had one person and is going to a reachable floor
if (A.length === 1 && B[0] <= M) {
return 2;