Skip to content

Instantly share code, notes, and snippets.

Avatar
🌎
I may be slow to respond.

Sergey S. Volkov js2me

🌎
I may be slow to respond.
View GitHub Profile
@js2me
js2me / status.ts
Created Jun 18, 2021
Effector status of effects
View status.ts
import {
Effect,
Store,
combine,
guard,
createEvent,
restore,
sample,
} from "effector";
import { status as patronumStatus } from "patronum";
@js2me
js2me / undoRedoEffector.ts
Created Mar 22, 2021
Undo redo effecto draft
View undoRedoEffector.ts
import { combine, createEvent, createStore, Event, guard, sample, StoreValue } from "effector";
export const createUndoRedo = <S extends unknown>({ defaultState, filter, limit, name }: UndoRedoOptions<S>) => {
const storeName = name || "unknown-store";
const $present = createStore(defaultState, { name: `history/${storeName}/present` });
const limitPerTime = Math.round(limit / 2);
const $past = createStore<S[]>([], { name: `history/${storeName}/past` });
const $future = createStore<S[]>([], { name: `history/${storeName}/future` });
@js2me
js2me / launch.json
Last active Feb 16, 2021
ts-node debugger for vs code
View launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
// https://github.com/wclr/ts-node-dev/issues/9
"version": "0.2.0",
"configurations": [
{
"name":"ts-node (src/index.ts)",
"type":"node",
@js2me
js2me / utils.ts
Last active Feb 4, 2021
Effector utils for own usage
View utils.ts
import { Effect, Event, Store, createEvent, combine } from "effector";
import { status } from "patronum";
import { EffectState } from "patronum/status";
export interface FetchingState {
loading: boolean;
done: boolean;
fail: boolean;
}
@js2me
js2me / useFocused.ts
Created Jul 13, 2020
React hook with focused element
View useFocused.ts
import { useState, RefObject, useEffect } from "react";
export const useFocused = (inputRef: RefObject<HTMLInputElement>): boolean => {
const [isFocused, setFocused] = useState(false);
useEffect(() => {
if (inputRef.current) {
const handleFocus = (): void => setFocused(true);
const handleBlur = (): void => setFocused(false);
View createMemoContainer.ts
/**
*
* @param initialValue {T} initial value
* @param callback {function} used to update value
*/
export const createMemoContainer = <T, ExtraArgs extends unknown[] = []>(
initialValue: T,
callback: (currentValue: T, ...args: ExtraArgs) => T | null,
): ((...args: ExtraArgs) => T) => {
@js2me
js2me / utils.d.ts
Last active Jun 14, 2021
My used TypeScript advanced types
View utils.d.ts
type Nullable<T> = T | null;
type Maybe<T> = Nullable<T> | undefined;
/**
* Позволяет переопределить тип свойства {PropertyName} у типа {T} на новое значение {NewValue}
*/
type Override<T, PropertyName extends keyof T, NewValue> = Omit<T, PropertyName> &
{ [K in keyof T]: K extends PropertyName ? NewValue : T[K] };
@js2me
js2me / copy.ts
Last active Sep 3, 2020
deep copy js
View copy.ts
export const clone = <O extends unknown | unknown[]>(value: O, history?: Set<unknown>): O => {
if (value == null || typeof value !== "object") return value;
const stack = history || new Set();
if (stack.has(value)) {
return value;
}
stack.add(value);
@js2me
js2me / string.js
Created Mar 1, 2020
get hash from string
View string.js
String.prototype.hashCode = function() {
var hash = 0, i, chr;
if (this.length === 0) return hash;
for (i = 0; i < this.length; i++) {
chr = this.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};
@js2me
js2me / launch.json
Created Feb 16, 2020 — forked from cecilemuller/launch.json
Run ts-node in VSCode Debugger
View launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Node Inspector",
"type": "node",
"request": "launch",
"args": ["${workspaceRoot}/src/service.ts"],
"runtimeArgs": ["-r", "ts-node/register"],
"cwd": "${workspaceRoot}",