View lazySelectionSort.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function chunkedSelectionSort(arr, callback) { | |
let i = 0; | |
function sortStep() { | |
let lowestIdx = i; | |
// Find lowest index | |
for (let j = i + 1; j < arr.length; j++) { | |
if (arr[j] < arr[lowestIdx]) { | |
lowestIdx = j; |
View stateGenerator.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create store accepts reducers map and initial state | |
function* createStore(reducers, initialState = {}) { | |
// Initialize state | |
let state = initialState; | |
// Extract the reducer keys upfront | |
const reducerKeys = Object.keys(reducers); | |
// Combine all reducers into one reducing function | |
const combinedReducer = (state, action) => { | |
return reducerKeys.reduce((newState, key) => { | |
// Pass slice of state into corresponding reducer |
View cacher.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function cacher<T>(thunk: () => Promise<T>, ttlSeconds = 60) { | |
const noCache: unique symbol = Symbol("cache"); | |
let cache: Promise<T> | symbol = noCache; | |
return function execThunk(): Promise<T> { | |
if (typeof cache !== "symbol") { | |
return cache; | |
} | |
cache = thunk(); |
View MyPlotFigureChart.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as Plot from "@observablehq/plot"; | |
import PlotFigureChart from "./PlotFigureChart.tsx" | |
export default function MyPlotFigureChart( | |
{ filteredRequests }: { filteredRequests: Readonly<any[]> } | |
) { | |
return ( | |
<PlotFigureChart | |
options={{ | |
inset: 10, |
View useEventBus.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { | |
createContext, | |
useContext, | |
useEffect, | |
useRef, | |
} from "react" | |
// Create the event bus context | |
const EventBusContext = createContext({}) | |
// Custom hook to access the event bus context |
View useCustomPollingExample.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from 'react'; | |
import usePolling from './usePolling'; | |
import ... | |
const useCustomPolling = () => { | |
const [state, setState] = useState() | |
const cb = async (cancelPolling) => { | |
// do some logics here... whatever... | |
View useFetch.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useReducer, useRef } from "react"; | |
const initialState = { | |
isLoading: false, | |
prevData: undefined, | |
data: undefined, | |
error: undefined, | |
}; | |
const reducer = (state, action) => { |
View ReactForm.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import useFormValidation from './useFormValidation'; | |
const initialValues = { | |
name: '', | |
email: '', | |
password: '', | |
}; | |
// You should put this config in a separate file! |
View ComponentReader.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script lang="ts"> | |
/// ... | |
import { customStoreDebouncer /*, storeDebouncer */ } from "./stores/stringStore"; | |
const stringStoreDebounced = customStoreDebouncer(400); // debounced store read | |
$: { | |
const text = $stringStoreDebounced; // resolved value, after debounce timeout | |
/// ... | |
} | |
</script> |
View dl.deno.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Timeout, TimeoutError } from "https://deno.land/x/timeout/mod.ts" | |
const delay = (ms) => new Promise((res) => setTimeout(res, ms)); | |
function newThrottler({ isBusy, lock, unlock, waitMs, size }) { | |
async function throttler(cb, ...args) { | |
size(1); | |
await Promise.resolve(); | |
while (!!isBusy()) { | |
await delay(waitMs()); // waits in event loop queue, until it interrupts for another attempt! |
NewerOlder