Skip to content

Instantly share code, notes, and snippets.

View kubk's full-sized avatar

Egor Gorbachev kubk

View GitHub Profile
interface Observer<T> {
next(value: T)
complete()
error(error: unknown)
}
class Observable<T> {
constructor(public subscribe: (observer: Observer<T>) => void) {}
map(mapper: (x: T) => T): Observable<T> {
@kubk
kubk / mobx.js
Last active November 6, 2020 10:10
Mobx VS Reselect comparison
const store = makeAutoObservable({
shop: {
taxPercent: 8,
items: [
{ name: 'apple', value: 1.20 },
{ name: 'orange', value: 0.95 },
]
},
get subtotal() {
return this.shop.items.reduce((acc, item) => acc + item.value, 0);
import { assert } from "ts-essentials";
export class Container {
private services = new Map<string, object>();
private factories = new Map<string, (container: Container) => object>();
set(key: string, factory: (container: Container) => object) {
this.factories.set(key, factory);
}
class ListStore<T> {
isLoading = false;
list: T[] = [];
constructor(loadList: () => Promise<T[]>) {
makeAutoObservable(this)
}
loadList() {
this.isLoading = true;
@kubk
kubk / gist:d874ad358bf7b124f725e0abef2132d0
Created March 9, 2021 11:38
Почему React context не замена стейт-менеджеру
Ссылки:
- Лишний ререндер: https://codesandbox.io/s/usecontext-problem-b6evb
- Переизобретают Mobx с массой ограничений: https://habr.com/ru/post/546124/
- Разработчики из Atlassian не смогли переписать react-beautiful-dnd с Redux на контекст из-за проблем с перформансом: https://github.com/atlassian/react-beautiful-dnd/issues/1576#issuecomment-549643226
@kubk
kubk / mobx-6-cache.js
Created April 20, 2021 09:26
Simple Mobx 6 cache example
class UsersStore {
isLoaded = false;
users = [];
constructor() {
makeAutoObservable(this)
}
loadUsers() {
if (this.isLoaded) {
@kubk
kubk / rxjs-plain.ts
Last active April 27, 2021 07:44
Counter state using different JS libraries
// RxJS - FRP
const increment = new Subject<void>();
const decrement = new Subject<void>();
const add = new Subject<number>();
type State = number;
const count$ = merge(
increment.pipe(mapTo((state: State) => state + 1))
decrement.pipe(mapTo((state: State) => state - 1)),
@kubk
kubk / classes.ts
Last active May 3, 2021 12:25
Mobx 6 + TypeScript
export class PlayerStore {
song?: Song;
isPlaying = false;
constructor() {
makeAutoObservable(this);
}
playSong(song: Song) {
this.song = song;
@kubk
kubk / counter.test.ts
Created September 7, 2021 14:16
Mobx test timers
import { TimeTrackerStore } from './time-tracker-store';
import { action, makeAutoObservable } from 'mobx';
class Counter {
value = 0;
intervalId?: NodeJS.Timer;
constructor() {
makeAutoObservable(this);
}
@kubk
kubk / install-fish.sh
Last active September 14, 2021 10:37
Ubuntu fresh install
# Fix inotify issue: https://stackoverflow.com/a/56156015