Skip to content

Instantly share code, notes, and snippets.

Avatar
😀
RxJS!

Dimitry DimitryDushkin

😀
RxJS!
View GitHub Profile
@DimitryDushkin
DimitryDushkin / switch.js
Created Aug 17, 2021
JS assign a variable to switch result
View switch.js
const result = (function () {
switch (step) {
case Step.One:
return {one: 1};
case Step.Two:
return {two: 2};
case Step.Three:
return {three: 4};
}
})();
View scheduleTasks.ts
export const scheduleTasks = (tasks: Task[], today?: Date) => {
const graph = makeGraphFromTasks(tasks);
const tasksById = tasks.reduce((map, task) => {
map[task.id] = task;
return map;
}, {} as { [id: string]: Task });
// @TODO: 0. Detect cycles, if present throw error
// 1. Make reverse graph, to detect sinks and sources
View makeReverseGraph.ts
export const makeReverseGraph = (graph: Graph): Graph => {
const reverseGraph: Graph = new Map();
for (const [id, parentId] of dfs(graph, { withParentId: true })) {
const prerequesitions = reverseGraph.get(id) ?? new Set();
if (parentId) {
prerequesitions.add(parentId);
}
reverseGraph.set(id, prerequesitions);
}
View graph-task-type.ts
export type Task = {
id: ID;
title: string;
start: Date;
end: Date;
duration: number;
position: number;
progress: number;
resourceId: ID;
dependencies?: ID[];
View makeGraphFromTasks.ts
/**
* Graph respects exlicit dependecies
* and implicit by resources (via positions)
*/
export const makeGraphFromTasks = (tasks: Task[]): Graph => {
const graph: Graph = new Map();
const resources = new Map<ID, Task[]>();
// add edges for deps by resourceId and explicit dependencits
for (const t of tasks) {
View gist:9020b6f5639fbbd46bc0746c5bfc2586

Create patch file

git diff > patch.diff

Apply path file

patch -p1 < patch.diff

@DimitryDushkin
DimitryDushkin / tsconfig.json
Created Jul 27, 2019
Example tsconfig for RN application
View tsconfig.json
{
"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. */
View use-draggable-example.5.tsx
function DraggableComponent() {
const draggableDivRef = useRef<HTMLDivElement>();
const drag$ = useDraggable(draggableDivRef);
useEffect(() => {
if (!drag$.current) {
return () => {};
}
const dragSubscription = drag$.current.subscribe(e => {
View use-draggable-example.4.ts
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$);
View use-draggable-example.3.ts
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", () => {