Skip to content

Instantly share code, notes, and snippets.

@oculus42
oculus42 / README.md
Last active November 22, 2023 22:14
Starting intervals at the edge of the second can help expose timer/interval defects.

JavaScript Timeouts and Intervals can experience hard to reproduce defects. This script tries to help.

Browser Variation

Chrome attempts to provide the timer with the "closest to original interval" without going under, so the interval is inconsistent. This causes jitter.

const ChromeTimings = [
  { second: 17, timestamp: 37998 },
@oculus42
oculus42 / requestLatest.js
Last active May 17, 2023 00:25
Allow multiple requests to share a response, even if the response is from a request that has not been made yet.
/**
* requestLatest allows multiple requests to share resolutions from the past or future.
* This allows multiple requestors to receive the most up-to-date response without
* causing promise rejections through canceled requests.
* This can reduce the complexity of consumer logic by eliminating many checks for
* cancellation or stale data.
*
* We start with makeRequestContainer to share responses to a single endpoint.
* This stores a last request (currentPromise) to avoid making identical requests. The parameter
* `isSamePayload` tells us if we can re-use currentPromise or should make a new request.
// Mock Axios with support for Abort Signals to test the behavior of consuming code.
jest.mock('axios', () => ({
__esModule: true, // this property makes it work
default: {
// Incrementing requestCount for validating if a request is the same.
requestCount: 0,
post(url, payload, options) {
// Options is basically for the AbortController signal
const signal = options?.signal;
@oculus42
oculus42 / mov-to-gif.sh
Created February 16, 2022 20:06
Simple ffmpeg wrapper for making QuickTime Screen Recordings into gifs
#!/bin/bash
# Based on http://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html
# Created: 2016/09/06, Samuel Rouse
# Last edit: 2022/02/16
# Takes one to three arguments: input [size] [output]
# mov-to-gif input.mov output.gif
# mov-to-gif input.mov 1280 output.gif
# For easy upload of QuickTime Player Screen Recordings (and any other sources)
@oculus42
oculus42 / machine.js
Last active November 15, 2019 13:34
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@oculus42
oculus42 / machine.js
Created November 13, 2019 13:58
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@oculus42
oculus42 / machine.js
Last active November 12, 2019 22:04
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@oculus42
oculus42 / backbone-hooks.js
Last active July 13, 2019 21:53
BackboneJS Hooks for React
// Updated versions at https://github.com/oculus42/react-backbone-hooks
// npm i react-backbone-hooks
// 0.2.1
import { useState } from 'react';
const useModel = (model) => {
const [data, setState] = useState(model.toJSON());
// TODO: Should this update only changedAttributes for change?
@oculus42
oculus42 / jquery.bannimate.js
Created June 18, 2019 13:53
My first jQuery Plugin, created to fix a resize issue with animation and page resize for Xanity Design.
/*
* Bannimate jQuery Plug-in
* http://armyofnone.com/bannimate
*
* Copyright (c) 2009 Samuel Rouse
* Dual licensed under the MIT and GPL licenses.
* http://docs.jquery.com/License
*
* Date: 2009-10-26
* Revision: 1
@oculus42
oculus42 / hook.js
Created February 24, 2019 00:18
Basic Concept of a Hooks Library
/*
* Based on a very broad idea of React Hooks,
* I created a very small script to provide the
* basic behavior of a Hook.
*/
const Hook = function(){};
const makeHook = (val) => {
let state = val;