This file contains 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
// Gaussian random number generator based on Box-Muller transformation | |
export function* randomNormal(mu, sigma) { | |
let x = null; | |
let y = null; | |
let s = null; | |
while (true) { | |
do { | |
x = 2 * Math.random() - 1; | |
y = 2 * Math.random() - 1; |
This file contains 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 { create, act } from 'react-test-renderer'; | |
import { useDerivedState } from './useDerivedState'; | |
function TestComponent({ value }) { | |
let [state, setState] = useDerivedState(value); | |
let increment = jest.fn(() => setState(state + 1)); | |
return <button onClick={increment}>{state}</button>; | |
} |
This file contains 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, { createRef, forwardRef, useLayoutEffect, useMemo } from 'react'; | |
export let Dialog = forwardRef((props, ref) => { | |
let dialogRef = useMemo(() => ref || createRef(), []); | |
useLayoutEffect( | |
() => { | |
dialogRef.current.showModal(); | |
}, | |
[dialogRef] | |
); |
This file contains 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 LinearScale(domain, range) { | |
let scale = v => interpolate(range, normalize(domain, v)); | |
let inverse = v => interpolate(domain, normalize(range, v)); | |
return Object.assign(scale, { inverse }); | |
} | |
function normalize(range, x) { | |
let k = range[1] - range[0]; | |
let b = range[0]; | |
return (x - b) / k; |
This file contains 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 useRedux(selector = x => x, ...input) { | |
// достаем стор через контекст. возможно, понадобится реактовый, без react-redux | |
let store = useContext(ReduxContext); | |
// геттер для стейта с селектором. используется в нескольких местах ниже | |
let getState = () => selector(store.getState(), ...input); | |
// локальное состояние хука, содержит результат селектора от текущего состояния стора | |
let [state, setState] = useState(() => getState()); | |
This file contains 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
// React side | |
let currentFiber = null; | |
export function useState() { | |
// doing something with currentFiber | |
} | |
export function render(component) { | |
currentFiber = { ... }; | |
let content = component(); |
This file contains 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'; | |
class Counter extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { counter: 0 }; | |
this.increment = this.increment.bind(this); | |
} | |
increment() { |
This file contains 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
let ReactInternals = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED; | |
function useContext(context, observedBits) { | |
let dispatcher = ReactInternals.ReactCurrentOwner.currentDispatcher; | |
return dispatcher.readContext(context, observedBits); | |
} |
This file contains 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 Coroutine from 'react-coroutine'; | |
import Deferred from './Deferred.js'; | |
export default Coroutine.create(Counter); | |
async function* Counter() { | |
let counter = 0; | |
while (true) { | |
let event = new Deferred(); |
This file contains 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 { zoom, event } from 'd3'; | |
class Zoomable extends React.Component { | |
state = { x: 0, y: 0, k: 1 }; | |
rootRef = React.createRef(); | |
zoomHandler = zoom() | |
.scaleExtent([0.5, 1.5]) | |
.translateExtent([[0, 0], [2000, 2000]]); |