Skip to content

Instantly share code, notes, and snippets.

View OlegLustenko's full-sized avatar

Oleg Lustenko OlegLustenko

View GitHub Profile
import * as React from 'react';
const useIsFirstRender = (): boolean => {
const isFirst = React.useRef(true);
if (isFirst.current) {
isFirst.current = false;
return true;
} else {
"use client";
import { cache, unstable_postpone } from "react";
import { preload } from "react-dom";
const loadImage = cache((src: string) => {
return new Promise<void>((resolve, reject) => {
const img = new Image();
img.src = src;
@kfox
kfox / README.md
Last active December 4, 2023 11:08
TCP echo server for Node.js

TCP echo server for Node.js

Usage

  1. Make sure you have a modern-ish version of Node.js installed.
  2. Type npx https://gist.github.com/kfox/1280c2f0ee8324067dba15300e0f2fd3
  3. Connect to it from a client, e.g. netcat or similar: nc localhost 9000
@Andarist
Andarist / createLongLastingContext.ts
Created December 5, 2022 13:22
Hide value initialization in a React-oriented helper using context and conditional server/browser values
function createLongLastingContext<T>(factory: () => T) {
const defaultValue = typeof document !== "undefined" ? factory() : null;
const ctx = React.createContext<T | null>(defaultValue);
return {
useContext: () => {
const value = React.useContext(ctx);
if (!value) {
throw new Error("Context not initialized.");
}
return value;
<%*
const editor = app.workspace.activeLeaf.view.editor;
if(editor.somethingSelected() === true) {
let selectedText = editor.getSelection();
let splitLines = selectedText.split('\n');
let finalOutput = '';
let listItem = false;
let codeBlock = false;
splitLines.forEach(eachLine => {
/*!
* ExternalTemplateRemotesPlugin
* License: MIT (https://mit-license.org/)
*/
const extractUrlAndGlobal = require('webpack/lib/util/extractUrlAndGlobal');
const { RawSource } = require('webpack-sources');
const PLUGIN_NAME = 'ExternalTemplateRemotesPlugin';
@developit
developit / *constant-locals-loader.md
Last active February 4, 2022 17:15
Inline Webpack CSS Modules classNames, reducing bundle size. https://npm.im/constant-locals-loader

constant-locals-loader for Webpack

This loader optimizes the output of mini-css-extract-plugin and/or css-loader, entirely removing the potentially large CSS classname mappings normally inlined into your bundle when using CSS Modules.

Run npm install constant-locals-loader, then make these changes in your Webpack config:

module.exports = {
 module: {
@busypeoples
busypeoples / validation.ts
Created February 10, 2020 11:52
Validation in TS
type ValidationResult<T, U> = Partial<{ [Key in keyof T]: U }>;
type Validation<T, U> = (input: T) => ValidationResult<T, U>;
const mergeAll = <T, U>(
results: ValidationResult<T, U>[]
): ValidationResult<T, U> =>
results.reduce((acc, a) => Object.assign(acc, a), {});
const validate = <T, U = boolean | string>(
validations: Validation<T, U>[]
@swyxio
swyxio / createCtx-noNullCheck.tsx
Last active May 4, 2023 02:15
better createContext APIs with setters, and no default values, in Typescript. this is documented in https://github.com/typescript-cheatsheets/react-typescript-cheatsheet/blob/master/README.md#context
// create context with no upfront defaultValue
// without having to do undefined check all the time
function createCtx<A>() {
const ctx = React.createContext<A | undefined>(undefined)
function useCtx() {
const c = React.useContext(ctx)
if (!c) throw new Error("useCtx must be inside a Provider with a value")
return c
}
return [useCtx, ctx.Provider] as const
@pedronauck
pedronauck / useHotkeys.ts
Created December 11, 2018 02:03
usePopper and useHotkeys hooks
import { useEffect } from 'react'
import hotkeys from 'hotkeys-js'
export const useHotkeys = (key: string, cb: () => any, inputs?: any[]) => {
useEffect(() => {
hotkeys(key, cb)
return () => hotkeys.unbind(key)
}, inputs)
}