Skip to content

Instantly share code, notes, and snippets.

View ddoronin's full-sized avatar
🏠
In The Matrix

Dima Doronin ddoronin

🏠
In The Matrix
View GitHub Profile
@ddoronin
ddoronin / gist:61e9bfea1d62b0052403
Last active August 29, 2015 14:23
Require Light
(function(global) {
var serviceRegistrar = {};
function resolve(serviceName) {
if (typeof serviceRegistrar[serviceName] === 'undefined') {
throw new Error('Service "' + serviceName + '" cannot be instantiated.');
}
return serviceRegistrar[serviceName];
}
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="log"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
<script src="http://underscorejs.org/underscore.js"></script>
@ddoronin
ddoronin / gist:8f940639ff15f7589777cf06ad8cc055
Created April 3, 2018 03:37
TypeScript Redux Reducer Store
import { Reducer } from 'redux';
export type ReducerState<S> = {
[P in keyof S]: Reducer<S[P]>
};
/* Example
interface IState {
files: string[]
}
@ddoronin
ddoronin / gist:bb7af2ebb6b831588e53f01b1bf1059c
Created April 14, 2018 02:39
Conditional types aka scala monads to infer request/response types from url and method
interface IRequest {
id: string
}
interface IResponse {
name: string
}
type Method = 'GET' | 'POST' | 'PUT' | 'DELETE';
type None = void
type Extends<T, TT, Get, Else> = T extends TT ? Get : Else;
@ddoronin
ddoronin / useRx.ts
Last active September 18, 2019 17:39
useRx and useRxTap React Hooks for RxJS
import { useEffect, useState } from "react";
import { Observable, Subscription } from "rxjs";
import { tap } from 'rxjs/operators';
/**
* Reactive Hook that returns a tuple of resolved value and error.
* @param { Observable<T> } observable$
* @param { T } defaultValue
*/
export function useRx<T>(
import { Subject } from "rxjs";
import { Option } from "monas";
export interface IRequest {
uri: string;
headers: { [key: string]: string };
time: number;
}
export default class RequestComposer {
@ddoronin
ddoronin / AppState.ts
Created April 1, 2019 02:17
Application State
import * as React from "react";
import RequestComposer from "src/models/request-composer";
import {
overHistory,
IHistoricRequest,
overHistoryLatest5
} from "src/functions/history";
import { overHttp } from "src/functions/http";
import { Observable } from "rxjs";
import { Option } from "monas";
@ddoronin
ddoronin / overHistory.ts
Last active April 1, 2019 13:37
5 Latest
import { IRequest } from "src/models/request-composer";
import { scan } from "rxjs/operators";
export const overHistoryLatest5 =
scan((acc: IRequest[], r: IRequest) => [r, ...acc].slice(0, 5), []);
@ddoronin
ddoronin / History.tsx
Created April 1, 2019 02:34
History React Component
import * as React from "react";
import { useRx } from "src/useRx";
import { AppStateContext } from "src/state/AppState";
import { some } from "monas";
import styles from "./styles.module.scss";
import { IRequest } from "src/models/request-composer";
export default function History() {
const appState = React.useContext(AppStateContext);
const [requests] = useRx(appState.last5$, []);
@ddoronin
ddoronin / http.ts
Created April 1, 2019 02:49
overHttp
import { ajax } from "rxjs/ajax";
import { switchMap, map, merge, catchError } from "rxjs/operators";
import { BehaviorSubject, of } from "rxjs";
import { Option, some, none } from "monas";
import { IRequest } from "src/models/request-composer";
export const overHttp = switchMap((_: Option<IRequest>) =>
_.map(req =>
new BehaviorSubject({ isLoading: true, req: _, resp: none }).pipe(
merge(