Skip to content

Instantly share code, notes, and snippets.

View OliverJAsh's full-sized avatar

Oliver Joseph Ash OliverJAsh

View GitHub Profile
import { defer, Observable } from "rxjs";
import { finalize, tap } from "rxjs/operators";
export const log = <T>(source: Observable<T>, name: string) => defer(() => {
console.log(`${name}: subscribed`);
return source.pipe(
tap({
next: value => console.log(`${name}: ${value}`),
complete: () => console.log(`${name}: complete`)
}),
@Magellol
Magellol / ts-custom-intrinsic-elements.tsx
Created December 9, 2018 18:28
Override react types to exclusively allow a set of custom intrinsic elements without inheriting from HTML.
// By overriding a subset of the interfaces from the `@types/react` package,
// we're able to strictly type check what our JSX must look like and avoid inheriting from HTML elements (since they're not valid in our environment for instance).
import * as React from 'react';
declare module 'react' {
namespace JSX {
interface ElementChildrenAttribute {
children: {};
}
@joshburgess
joshburgess / newtypes-redux-api-actions.ts
Last active January 26, 2019 06:51
Using newtype-ts newtypes to dynamically generate multiple API Action types representing API request states
import { Predicate } from 'fp-ts/lib/function'
import { Prism } from 'monocle-ts'
import { Newtype, iso, prism, unsafeCoerce } from 'newtype-ts'
interface Optimistic<A extends string>
extends Newtype<
{
readonly Optimistic: unique symbol
readonly phantom: A
},
@dsherret
dsherret / find-unused-exports.ts
Last active December 22, 2023 00:03
Searches files for any exported declarations that aren't used in other files.
// this could be improved... (ex. ignore interfaces/type aliases that describe a parameter type in the same file)
import { Project, TypeGuards, Node } from "ts-morph";
const project = new Project({ tsConfigFilePath: "tsconfig.json" });
for (const file of project.getSourceFiles()) {
file.forEachChild(child => {
if (TypeGuards.isVariableStatement(child)) {
if (isExported(child))
child.getDeclarations().forEach(checkNode);
@bvaughn
bvaughn / index.md
Last active May 4, 2024 11:25
How to use profiling in production mode for react-dom

React recently introduced an experimental profiler API. This page gives instructions on how to use this API in a production release of your app.

Table of Contents

Profiling in production

React DOM automatically supports profiling in development mode for v16.5+, but since profiling adds some small additional overhead it is opt-in for production mode. This gist explains how to opt-in.

@bvaughn
bvaughn / index.md
Last active April 3, 2024 07:41
Interaction tracing with React

This API was removed in React 17


Interaction tracing with React

React recently introduced an experimental profiler API. After discussing this API with several teams at Facebook, one common piece of feedback was that the performance information would be more useful if it could be associated with the events that caused the application to render (e.g. button click, XHR response). Tracing these events (or "interactions") would enable more powerful tooling to be built around the timing information, capable of answering questions like "What caused this really slow commit?" or "How long does it typically take for this interaction to update the DOM?".

With version 16.4.3, React added experimental support for this tracing by way of a new NPM package, scheduler. However the public API for this package is not yet finalized and will likely change with upcoming minor releases, so it should be used with caution.

@bvaughn
bvaughn / React.unstable_Profiler.md
Last active May 7, 2024 05:38
Notes about the in-development React <Profiler> component

Profiler

React 16.4 will introduce a new Profiler component (initially exported as React.unstable_Profiler) for collecting render timing information in order to measure the "cost" of rendering for both sync and async modes.

Profiler timing metrics are significantly faster than those built around the User Timing API, and as such we plan to provide a production+profiling bundle in the future. (The initial release will only log timing information in DEV mode, although the component will still render its children- without timings- in production mode.)

How is it used?

Profiler can be declared anywhere within a React tree to measure the cost of rendering that portion of the tree. For example, a Navigation component and its descendants:

@joeporpeglia
joeporpeglia / app.jsx
Last active February 26, 2019 18:45
React Redux with the new React Context API. I didn't test this at all...
import React from 'react';
import Counter from './counter';
const mapProps = ({ state, dispatch }) => ({
count: state,
increment: () => dispatch({
type: 'increment',
}),
decrement: () => dispatch({
@rupertlssmith
rupertlssmith / GameState.elm
Last active July 21, 2023 01:09
Exploring State Machines with phantom types in Elm
module GameState
exposing
( Game(..)
, GameDefinition
, PlayState
, loading
, updateGameDefinition
, updatePlayState
, updateScore
, toReady
@alexandru
alexandru / web-crawler.ts
Last active January 7, 2021 12:42
Web crawler that downloads HTML content (for analysis) from a list of websites, exporting content as JSON lines
#!/usr/bin/env node
import { IO, Try, Success, Failure, Either, Left, Right, Cancelable, Duration } from "funfix"
import { RequestResponse } from "request"
import * as fs from "fs"
import * as request from "request"
import * as Url from "url"
import * as cheerio from "cheerio"
import * as minimist from "minimist"