Skip to content

Instantly share code, notes, and snippets.

View lifeart's full-sized avatar
🐹
Working from home

Alex Kanunnikov lifeart

🐹
Working from home
View GitHub Profile
import Controller from '@ember/controller';
import { inject as service } from '@ember/service';
import { action, notifyPropertyChange, set, computed } from '@ember/object';
export default class ApplicationController extends Controller {
appName = 'Ember Twiddle';
@service state;
@action update() {
@lifeart
lifeart / index.ts
Last active March 2, 2025 13:58
TypeScript implementation of an ElasticHashMap class
class ElasticHashMap<K, V> {
private totalSize: number; // total number of entries in the map
private maxLoadFactor: number; // load factor (e.g., 0.75 means 25% slack, δ = 0.25)
private levels: { table: any[], size: number }[]; // sub-arrays for open addressing
private salts: { base: number, step: number }[]; // random salts for hashing per level
// Doubly-linked list for insertion order iteration:
private head: { key: K, value: V, next: any, prev: any } | null;
private tail: { key: K, value: V, next: any, prev: any } | null;
// Tombstone marker for deleted slots:
private static TOMBSTONE = Symbol("TOMBSTONE");
@lifeart
lifeart / info.md
Created February 20, 2025 19:24
proxy.md

Using a Proxy as the property descriptor itself in JavaScript allows you to intercept and customize how the engine interacts with the descriptor's attributes (like value, get, set, writable, etc.) during property definition. This approach provides dynamic control over the descriptor's behavior at the moment the property is defined. Here's how you can leverage this technique and its benefits:


Key Use Cases & Benefits

1. Dynamic Descriptor Attributes

Intercept access to descriptor properties (e.g., value, get, set) and compute them on-the-fly during property definition.

@lifeart
lifeart / index.d.ts
Last active July 2, 2024 17:45
Telegram WebAPP API typings
declare namespace Telegram {
interface EventHandlers {
[eventType: string]: Function[];
}
interface InitParams {
tgWebAppData?: string;
tgWebAppThemeParams?: string;
tgWebAppVersion?: string;
tgWebAppPlatform?: string;
// one of possible signals implementations
const USED_SIGNALS: Set<Signal> = new Set();
const RELATED_WATCHERS: WeakMap<Computed, Set<Watcher>> = new WeakMap();
const COMPUTED_SIGNALS: WeakMap<Signal, Set<Computed>> = new WeakMap();
class Signal {
value: any;
get() {
USED_SIGNALS.add(this);
@lifeart
lifeart / test.ts
Created December 27, 2023 08:33
test-vm
// @ts-check
// https://codepen.io/lifeart/pen/abMzEZm?editors=0110
// https://github.com/glimmerjs/glimmer-vm/issues/1540
/*
This is a proof of concept for a new approach to reactive programming.
It's related to Glimmer-VM's `@tracked` system, but without invalidation step.
We explicitly update DOM only when it's needed and only if tags are changed.
*/
let rowId = 1;
@lifeart
lifeart / index.js
Created November 2, 2023 11:38
resolve item selector on page with list of items
var classes = {};
var cnt = 1;
function hashForClass(rawClassName) {
const className = rawClassName.trim();
if (className in classes) {
classes[className].cnt++;
return classes[className].index;
} else {
classes[className] = {
@lifeart
lifeart / index.ts
Last active October 16, 2023 13:36
Lazy Services
class Bar {
doSomething() {
console.log('do something');
}
name: string;
}
type PromisifyProps<T> = {
[P in keyof T]: T[P] extends (...args: infer A) => infer R ? (...args: A) => Promise<R> : Promise<T[P]>;
};
@lifeart
lifeart / index.ts
Created August 8, 2023 12:28
Squares calculator
type Square = {
x: number;
width: number;
}
const squares: Square[] = [
{
x: 0,
width: 10,
},
{
@lifeart
lifeart / oauth2.ts
Last active July 21, 2023 19:02
React Oauth 2.0
// inspired by https://github.com/simplabs/ember-simple-auth/blob/master/packages/ember-simple-auth/addon/authenticators/oauth2-password-grant.js
import { LOGOUT_503_MESSAGE } from '@auth/messages';
/*
Idea is to have Authentication hub (HUB)
HUB could have multiple providers, our own provider is oauth2.0
Provider responsible for: