Skip to content

Instantly share code, notes, and snippets.

View DimitryDushkin's full-sized avatar
😀
RxJS!

Dimitry DimitryDushkin

😀
RxJS!
View GitHub Profile
@DimitryDushkin
DimitryDushkin / single-task-queue.ts
Last active March 14, 2019 07:27
Helpful wrapper to make sure that function body is working once per multiple calls
export const SINGLE_TASK_BLOCKED = Symbol('task blocked');
export type SingleTaskBlocked = typeof SINGLE_TASK_BLOCKED;
export const singleTaskQueue = <TReturn, TArgs extends Array<any>>(func: (...args: TArgs) => Promise<TReturn>) => {
let isWorking = false;
return async (...args: TArgs): Promise<TReturn | SingleTaskBlocked> => {
if (isWorking) {
return SINGLE_TASK_BLOCKED;
}
@DimitryDushkin
DimitryDushkin / babel.config.js
Last active April 30, 2020 03:32
RxJS + babel 7 to reduce bundle size
const rxjs = {
Observable: "internal/Observable",
ConnectableObservable: "internal/observable/ConnectableObservable",
GroupedObservable: "internal/operators/groupBy",
observable: "internal/symbol/observable",
Subject: "internal/Subject",
BehaviorSubject: "internal/BehaviorSubject",
ReplaySubject: "internal/ReplaySubject",
AsyncSubject: "internal/AsyncSubject",
asapScheduler: "internal/scheduler/asap",
@DimitryDushkin
DimitryDushkin / animated-fade.tsx
Last active January 8, 2024 10:29
React Native animated fade-in and fade-out
import React from 'react';
import {StyleProp, ViewStyle} from 'react-native';
import * as Animatable from 'react-native-animatable';
type Props = {
isVisible: boolean,
children: React.ReactNode,
style?: StyleProp<ViewStyle>,
};
@DimitryDushkin
DimitryDushkin / use-draggable-example.ts
Created June 5, 2019 10:49
useDraggable for React based on RxJS
import React, { useRef, useLayoutEffect } from "react";
import { fromEvent, Observable } from "rxjs";
import { takeUntil, mergeMap, map } from "rxjs/operators";
import ReactDOM from "react-dom";
import "./styles.css";
function App() {
return (
<div className="App">
export type DragEvent = { x: number; y: number };
export function createDragObservable<T extends PointerEvent>(
up$: Observable<T>,
down$: Observable<T>,
move$: Observable<T>
): Observable<DragEvent> {
let startPosition: DragEvent;
return down$.pipe(
mergeMap(e => {
startPosition = startPosition || { x: e.pageX, y: e.pageY };
return down$.pipe(
mergeMap(e => {
startPosition = startPosition || { x: e.pageX, y: e.pageY };
return move$.pipe(
takeUntil(up$),
map(e => ({
x: e.pageX - startPosition.x,
y: e.pageY - startPosition.y
}))
);
import { marbles } from "rxjs-marbles/jest";
import { createDragObservable } from "./use-draggable";
const data = {
d: new PointerEvent('mousedown'),
m: new PointerEvent('mousedown'),
u: new PointerEvent('mousedown'),
}
describe("useDraggable", () => {
export function useDraggable(draggableRef: React.RefObject<HTMLElement>) {
const drag$ = useRef<Observable<DragEvent>>();
useLayoutEffect(() => {
if (!draggableRef.current) {
return () => {};
}
const down$ = fromEvent<PointerEvent>(draggableRef.current, "pointerdown");
const move$ = fromEvent<PointerEvent>(document, "pointermove");
const up$ = fromEvent<PointerEvent>(document, "pointerup");
drag$.current = createDragObservable(up$, down$, move$);
function DraggableComponent() {
const draggableDivRef = useRef<HTMLDivElement>();
const drag$ = useDraggable(draggableDivRef);
useEffect(() => {
if (!drag$.current) {
return () => {};
}
const dragSubscription = drag$.current.subscribe(e => {
@DimitryDushkin
DimitryDushkin / tsconfig.json
Created July 27, 2019 07:02
Example tsconfig for RN application
{
"compileOnSave": true,
"compilerOptions": {
"skipLibCheck": true,
/* Basic Options */
"target": "es2018" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */,
"module": "es2015" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
"lib": ["es2018", "dom"] /* Specify library files to be included in the compilation. */,
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */