Skip to content

Instantly share code, notes, and snippets.

@mgtitimoli
mgtitimoli / createCancellable.js
Last active April 5, 2018 00:13
Yet another cancellation promise module
// @flow
type CancellablePromise<Result> = {
promise: Promise<Result>,
cancel: (reason?: string) => void
};
// $FlowFixMe: can not add custom properties to Error
const setCancelled = (error, cancelled) => Object.assign(error, {cancelled});
allprojects {
ext {
buildTools = "27.0.3"
compileSdk = 27
googlePlayServicesVersion = "11.8.0"
minSdk = 16
targetSdk = 23
}
configurations.all {
@mgtitimoli
mgtitimoli / set-state.js
Last active March 3, 2018 12:54
Promisified Conditional setState
// @flow
import {curry} from "flow-static-land/lib/Fun";
import type {ComponentType, ElementRef} from "react";
type State = Object;
type Updater<Props: Object> = (prevState: State, props: Props) => State;
@mgtitimoli
mgtitimoli / await-timeout.js
Last active January 28, 2018 10:54
A clean timeout implementation using promises + bonus track: waitUntil
import createDeferred from "./createDeferred";
const awaitTimeout = (miliseconds, onTimeout) => {
const deferred = createDeferred();
const timeoutId = setTimeout(
() => onTimeout(deferred.resolve, deferred.reject),
miliseconds
);
@mgtitimoli
mgtitimoli / updateIfPropsChanged.js
Last active May 4, 2017 00:48
shouldComponentUpdate and componentWIllReceiveProps done well
// @flow
import shallowEqual from "../shallowEqual";
type OnPropsChanged<Props> = (
props: Props,
component: React$Component<*, Props, *>
) => mixed;
const updateIfPropsChanged = <Props: Object, State: void | Object>(
@mgtitimoli
mgtitimoli / object-contains.js
Last active April 13, 2017 06:25
Checks recursively if an object contains another
const {hasOwnProperty} = Object.prototype;
const isObject = thing => thing !== null && typeof thing === "object";
const objectContains = (object, otherObject) => {
const objectIsArray = Array.isArray(object);
const otherObjectIsArray = Array.isArray(otherObject);
if (objectIsArray && !otherObjectIsArray) {
@mgtitimoli
mgtitimoli / create-event-emitter.js
Last active March 23, 2017 20:28
Yet Another EventEmitter
// @flow
import mapValues from 'lodash/fp/mapValues';
type Config = {
allowed?: Array<string>
};
type ListenerRelatedMethod = (eventName: string, listener: Function) => void;
@mgtitimoli
mgtitimoli / example.js
Last active February 2, 2017 03:41
Cancellable Promises (an implementation without cancellation tokens)
// API:
//
// Promise.prototype.cancel(...args):
// | Promise<resolved> // if promise was cancellable and no error happened
// | Promise<rejected> // if promise was not cancellable or some error happened
//
// type CancellationHandler = (...args) =>
// | Promise<rejected> // can be returned manually or automatically generated by throwing an error
// | Promise<resolved> // can be returned manually or returning without throwing an error
//
import compose from "lodash/fp/compose";
import curry from "lodash/fp/curry";
const mappableWith = curry((compose, apply) => Object.assign(
map => mappableWith(compose, compose(map, apply)),
{valueOf: () => apply()}
));
const mappable = mappableWith(compose);
@mgtitimoli
mgtitimoli / async-queue.js
Created November 23, 2016 01:02
Async Queue
import createCancellationToken from './createCancellationToken';
export default class AsyncQueue {
process = createCancellationToken()
_tasks = [];
enqueue(...newTasks) {
this._tasks.push(...newTasks);