Skip to content

Instantly share code, notes, and snippets.

View NoTimeForHero's full-sized avatar

NoTimeForHero

View GitHub Profile
@NoTimeForHero
NoTimeForHero / atom.ts
Created December 26, 2022 22:24
React Simple Atom Storage
// import { useEffect, useState } from 'react';
import { useEffect, useState } from 'preact/compat';
interface Atom<T> {
key: Symbol
}
interface StoreValue {
value: any,
listeners: Set<() => void>
}
@NoTimeForHero
NoTimeForHero / application_record.rb
Created March 3, 2023 09:26
Реализация Bulk Insert для PostgreSQL в Rails 5
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
def self.bulk_insert(data)
# @type [PG::Connection] conn
conn = self.connection.raw_connection
raise NotImplementedError.new 'Database Driver not supported!' unless conn.class.to_s == 'PG::Connection'
raise ArgumentError "Data not an Array of Hashes!" unless data.is_a?(Array) && data.all? {|rec| rec.is_a? Hash }
return nil unless data.count > 0
@NoTimeForHero
NoTimeForHero / entrypoint.sh
Created March 11, 2023 13:32
Run WebPack with primary service (like a php-fpm or rails puma)
#!/bin/bash
echo "Docker Entrypoint is called!"
cd /website
if [[ -f "/website/public/hot" ]]; then
echo "Removing HOT_RELOAD file"
rm /website/public/hot
fi
type PollingConfig = {
timeout?: number;
interval?: number;
initialTimeout?: number
};
type RequestFnResult <T> = { pending: true } | { data : T };
type Callback <T> = () => Promise<RequestFnResult<T>>;
export const TimeoutError = Symbol('TimeoutError!');
@NoTimeForHero
NoTimeForHero / replace.ts
Created February 9, 2024 20:08
Замена функции в объекте с полной поддержкой типов
// https://stackoverflow.com/questions/49752151/typescript-keyof-returning-specific-type
type KeyOfType<T, V> = keyof {
[P in keyof T as T[P] extends V ? P : never]: any;
};
type InferArgs<TValueType> = TValueType extends (...args: infer U) => any ? U : never;
type InferReturn<TValueType> = TValueType extends (...args: any) => infer U ? U : never;
const replaceFunction = <
TArgs extends InferArgs<TObject[TKey]>,
TReturn extends InferReturn<TObject[TKey]>,
TCallable extends (...args: any) => any,
import { getCurrentScope, onScopeDispose } from 'vue';
type AnyFn = (...args: any[]) => any;
interface UseDebounceFnReturn<T extends AnyFn> {
(...args: Parameters<T>): void;
cancel: () => void;
flush: () => void;
}