Skip to content

Instantly share code, notes, and snippets.

@monotykamary
monotykamary / autorun.ts
Last active October 28, 2024 05:52
Autorun RxJS with practices from Signals
import { Fragment, FunctionComponent, ReactNode, createElement, useEffect, useState } from "react";
import { asyncScheduler, BehaviorSubject, combineLatest, Observable } from "rxjs";
import { debounceTime, distinctUntilChanged, map, startWith } from "rxjs/operators";
type ObservableMap<T> = Map<Observable<T>, number>;
type ProxyHandlerType<K extends string | symbol, V> = { get(target: V, prop: K): V };
interface ProxyInterface {
$: <T>(obs: Observable<T>) => T;
}
@monotykamary
monotykamary / createRxDataLoader.ts
Created November 13, 2024 06:55
RxJS Dataloader
import { asapScheduler, Observable, ObservableInput, SchedulerLike, Subject } from 'rxjs';
import { bufferTime, first, map, mergeMap, share } from 'rxjs/operators';
export type BatchLoadFn<K = any, V = any> = (keys: K[]) => ObservableInput<V>;
export type Options = {
bufferTimeSpan?: number
bufferCreationInterval?: number | null
maxBufferSize?: number
scheduler?: SchedulerLike
}
@monotykamary
monotykamary / merge.sql
Created November 13, 2024 09:23
Merge Statement Postgres
-- Create the deposit_interest_rates table
CREATE TABLE deposit_interest_rates (
id UUID PRIMARY KEY,
bank_id UUID NOT NULL,
term INTEGER NOT NULL,
interest_rate DECIMAL(5,2) NOT NULL,
inserted_at TIMESTAMP WITHOUT TIME ZONE NOT NULL,
updated_at TIMESTAMP WITHOUT TIME ZONE NOT NULL
);
@monotykamary
monotykamary / decorator.exs
Created November 19, 2024 08:00
FLAME Elixir as a Decorator
defmodule DiscordCollector.FLAMEDecorator do
@moduledoc """
Provides FLAME function decorators that wrap functions to run in FLAME pools.
## Usage
defmodule MyModule do
use DiscordCollector.FLAMEDecorator
@decorate flame(MyApp.Pool)
@monotykamary
monotykamary / 0_library.ts
Last active November 21, 2024 16:06
Elixir Protocols in TypeScript - Type Safest Version
function createDataType<T extends { type: string }>() {
return new Proxy(
{},
{
get(_, type: string) {
return (data = {}) => ({ type, ...data });
},
}
) as {
[K in T['type']]: (
@monotykamary
monotykamary / exactly_once.ex
Created November 26, 2024 00:27
Exactly Once Processing
defmodule ExactlyOnce do
use GenServer
require Logger
# State will store processed event IDs and their status
defmodule State do
defstruct processed_events: %{},
processing_timeouts: %{},
retry_interval: 5_000,
processing_timeout: 30_000
@monotykamary
monotykamary / devbox.json
Created November 30, 2024 16:02
Devbox env setup for Zed editor contribution
{
"packages": [
"rustup@latest",
"libiconv@latest",
"rustc-wasm32@latest",
"wasmtime@latest",
"cmake@latest"
],
"shell": {
"init_hook": [
@monotykamary
monotykamary / landing_disk_log.ex
Last active December 14, 2024 18:06
Hot Landing Zone with Single-Node Disk Log
defmodule Nghenhan.LandingZone do
@moduledoc """
Hot Landing Zone implementation with fast recovery using segmented logs,
separate index files, and checkpoints.
"""
use GenServer
require Logger
@checkpoint_interval 300_000 # 5 minutes
@monotykamary
monotykamary / distribute_totals_through_x.excel
Created December 18, 2024 11:26
Excel/Sheets distribute value through Indirect count and substitution
=ARRAYFORMULA(
SUM(
IF(
INDIRECT(SUBSTITUTE(ADDRESS(1,COLUMN(),4), "1", "") & "2:" & SUBSTITUTE(ADDRESS(1,COLUMN(),4), "1", "") & COUNTA($A:$A)) = "x",
INDIRECT("$R2:$R" & COUNTA($A:$A)) /
INDIRECT("$Q2:$Q" & COUNTA($A:$A)),
0
)
)
)
@monotykamary
monotykamary / validator.ex
Created December 18, 2024 11:33
Validator for multi-sourced Hot Landing Zone
defmodule Nghenhan.MarketDataValidator do
@moduledoc """
Validates market data completeness and quality by reading from the landing zone.
Runs as a separate service that can identify data quality issues without
affecting raw data collection.
"""
use GenServer
require Logger