This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function debounce(fn, delay = 1000) { | |
let timeoutId = null; | |
return () => { | |
if (timeoutId) { | |
timeoutId = null; | |
} | |
timeoutId = setTimeout(fn, delay); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I bounced into this debounce implementation today and | |
// think it's actually quite easy and simple | |
function debounce(fn, ms = 1000) { | |
let timeout = null; | |
return (...args) => { | |
if (timeout) { | |
clearTimeout(timeout); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createObserver(listeners = {}) { | |
function addListener(eventType, cb) { | |
if (listeners.hasOwnProperty(eventType)) { | |
listeners[eventType].push(cb); | |
} else { | |
listeners[eventType] = [cb]; | |
} | |
return function removeListener() { | |
const index = listeners[eventType].indexOf(cb); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import ReactDOM from "react-dom"; | |
class App extends React.Component { | |
state = { | |
pointer: -1, | |
stateQueue: [], | |
currentState: { count: 0 } | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is a stupid example of using render props when I was | |
// reading the doc of React.children | |
const React = require('react'); | |
class CountSiblings extends React.Component { | |
render() { | |
const {children} = this.props; | |
return children( | |
React.Children.count(children), | |
); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// cannot force call the callback when it times out though | |
var requestIdleCallback = window.requestIdleCallback || function(callback, {timeout = Infinity}) { | |
let startTime = Date.now(); | |
return setTimeout(function() { | |
// 50 -> duration per frame | |
var timeRemaining = Math.max(0, 50 - (Date.now() - startTime)); | |
callback({ | |
didTimeout: timeRemaining === 0, | |
timeRemaining, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
yarn prettier && yarn linc && yarn flow dom |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function debouce(fn, wait = 99) { | |
let timeout = null; | |
function process(args) { | |
fn(args); | |
timeout = null; | |
} | |
return function() { | |
if (!timeout) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function debounce(fn){ | |
var id, timestamp; | |
var wait = 99; | |
var check = function(){ | |
var last = (Date.now()) - timestamp; | |
if (last < wait) { | |
id = setTimeout(check, wait - last); | |
} else { | |
id = null; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
export default function asyncComponent(importComponent) { | |
class AsyncComponent extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
component: null | |
}; |
NewerOlder