Skip to content

Instantly share code, notes, and snippets.

View DimitryDushkin's full-sized avatar
😀
RxJS!

Dimitry DimitryDushkin

😀
RxJS!
View GitHub Profile
@DimitryDushkin
DimitryDushkin / update-size-while-dragging.js
Last active March 4, 2018 17:48
Code from Yandex Zen narrative editor
type DraggerPosition = 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';
const computeGeometryWhileDragging = (
position: DraggerPosition,
preserveAspectRatio: boolean,
initialGeometry: SlideElementGeometry,
currentGeometry: SlideElementGeometry,
dx: number,
dy: number
): SlideElementGeometry => {
@DimitryDushkin
DimitryDushkin / promise-retry.js
Last active April 6, 2018 11:42
Retry function that returns promise. Flowtyped.
// @flow
export async function retryPromise<T: Array<*>>(fn: (...T) => Promise<*>, args: T, tryCount: number = 3) {
try {
return await fn(...args);
} catch (e) {
console.log(`Promise retry error. Attempts left: ${tryCount}`, e);
if (tryCount > 1) {
return await retryPromise(fn, args, tryCount - 1);
} else {
@DimitryDushkin
DimitryDushkin / values.ts
Last active November 2, 2022 14:15
$Values from Flow in Typescript
type $Values<O extends object> = O[keyof O];
const obj = {
RU: 'ru',
EN: 'en',
} as const;
type vals = $Values<typeof obj>; // vals === 'ru' | 'en';
@DimitryDushkin
DimitryDushkin / types-for-redux.ts
Last active October 5, 2018 21:30
Typescript + redux + redux-thunk + react-navigation typings example
import {Action as ReduxAction, Middleware as ReduxMiddleware, Store as ReduxStore} from 'redux';
import {NavigationState, NavigationScreenProp} from 'react-navigation';
import {ThunkAction, ThunkDispatch} from 'redux-thunk';
import {StateType} from 'typesafe-actions';
import {reducers} from 'store/reducers/root.reducer';
export type AppState = StateType<typeof reducers>;
export interface Action<T = any, P extends object = any, M = any> extends ReduxAction<T> {
payload?: P;
@DimitryDushkin
DimitryDushkin / tsconfig.json
Created July 31, 2018 19:37
tsconfig for react native
{
"compileOnSave": true,
"compilerOptions": {
"skipLibCheck": true,
/* Basic Options */
"target": "es2015", /* 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": ["es2017", "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. */
@DimitryDushkin
DimitryDushkin / curry-map.ts
Created September 27, 2018 19:34
Curry functions in map for Typescript
import {Provider, Consumer} from 'react';
// tslint:disable-next-line
type Curry<F extends Function> = F extends (arg1: infer T1, arg2: infer T2, arg3: infer T3) => any
? (arg3: T3) => ReturnType<F>
: never;
type RVSActionCreator<S, A> = (state: S, actions: A, arg: any) => any;
type ActionCreatorsMap<S, A> = {[actionName: string]: RVSActionCreator<S, A>};
@DimitryDushkin
DimitryDushkin / configs.js
Created October 2, 2018 14:19
React Native 0.57 + Babel 7 + Typescript + Jest
// babel.config.js
module.exports = {
"presets": [
"module:metro-react-native-babel-preset",
],
"plugins": [
["module-resolver", {
"root": ["./src"],
"extensions": [".js", ".ts", ".tsx", ".ios.js", ".android.js"]
}],
@DimitryDushkin
DimitryDushkin / singleton.ts
Created November 16, 2018 09:25
TypeScript singleton (swift style)
export default class SomeSingleton {
private static privateShared: Api;
public static get shared(): Api {
if (!SomeSingleton.privateShared) {
SomeSingleton.privateShared = new SomeSingleton();
}
return SomeSingleton.privateShared;
}
@DimitryDushkin
DimitryDushkin / react-native-typescrpt-templates.json
Created December 11, 2018 12:26
React Native Typescript Visual Studio Code
{
"react statefull component": {
"prefix": "react-native-statefull",
"body": [
"import React from 'react';",
"import {View, StyleSheet} from 'react-native';",
"",
"type Props = {",
"",
"};",
@DimitryDushkin
DimitryDushkin / babel.config.js
Created February 3, 2019 15:31
"Modern" build for react-native application for iOS 12+ as target. Native async-await, classes and much more!
const isLegacyBuild = typeof process.env.BUNDLE_FOR_IOS === 'undefined';
const iosVersion = process.env.BUNDLE_FOR_IOS || 12;
const modernBuild = {
presets: [
[
'@babel/preset-env',
{
targets: `iOS >= ${iosVersion}`,
},
],