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
@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:
@lifeart
lifeart / index.js
Created July 5, 2023 08:24
QOI - The "Quite OK Image" format for fast, lossless image compression
// https://raw.githubusercontent.com/phoboslab/qoi/master/qoi.h
class QoiRGBA {
constructor(r = 0, g = 0, b = 0, a = 255) {
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
}
@lifeart
lifeart / basic.js
Last active June 29, 2023 10:08
Typescript custom diagnostics
const ts = require('typescript');
const fs = require('fs');
const path = require('path');
function getFileInfo(file, start) {
const { fileName } = file;
const { line, character } = file.getLineAndCharacterOfPosition(start);
return { fileName, line: line + 1, character: character + 1 };
}
@lifeart
lifeart / Polygon.ts
Last active April 28, 2023 14:38
Superannotate types
type JsonData = {
meta: Meta;
parameters: Parameter[];
}[];
interface Meta {
type: string; // The dictionary is a Polygon.
classId: number; // Class ID (one of the class IDs in “classes.json”). If the instance has an undefined class, the value should be -1 (classId": -1)
className?: string; // Class name. If the instance has an undefined class, the key should not be in the JSON.
createdBy?: User; // Information about the user who created the Polygon.