Skip to content

Instantly share code, notes, and snippets.

View michaelrambeau's full-sized avatar

Michael Rambeau michaelrambeau

View GitHub Profile
@michaelrambeau
michaelrambeau / machine.js
Last active February 4, 2020 12:36
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@michaelrambeau
michaelrambeau / for-await-loop.js
Last active July 6, 2018 14:48
Async iteration with `for await` loop, an example for https://weekly.bestofjs.org/issues/5
/*
Script to run in the latest version of node.js (tested in v10.5.0)
to check the behavior of asynchronous iterators.
*/
// Return a Promise that resolves after 0.5 seconds, to simulate an API request
async function fetchUser(i) {
return new Promise(resolve => {
setTimeout(resolve, 500, `User #${i}`)
})
// Compound Components
import React from 'react'
import {Switch} from '../switch'
class Toggle extends React.Component {
// you can create function components as static properties!
// for example:
// static Candy = (props) => <div>CANDY! {props.children}</div>
// Then that could be used like: <Toggle.Candy />
@michaelrambeau
michaelrambeau / await-async-errors.js
Last active September 12, 2018 01:56
Tricks about error handling and promises
/*
Context: we have an API that throws an error (not wrapped in a Promise).
We want to catch that error at the upper level, without making fail the main function
that should end normally.
*/
function apiRequest() {
throw new Error('Big Bug')
}
async function action() {
@michaelrambeau
michaelrambeau / DevelopersTemplate.js
Last active December 9, 2017 05:27
Add data to Gatsby pages using GraphQL
import React from 'react'
import SalariesBlock from '../blocks/SalariesBlock'
import ExperienceBlock from '../blocks/ExperienceBlock'
import Meta from '../elements/Meta'
const DevelopersTemplate = props => (
<div className="template">
<Meta section={props.section} subSection="developers" />
<SalariesBlock {...props} />
<ExperienceBlock {...props} />
import React from 'react'
import ReactDOM from 'react-dom'
const withMouse = (Component) => {
return class extends React.Component {
state = { x: 0, y: 0 }
handleMouseMove = (event) => {
this.setState({
x: event.clientX,
@michaelrambeau
michaelrambeau / overview.md
Last active July 15, 2017 04:12
How the web application `bestofjs-webui` works

BestOfJS Overview

bestofjs-webui is a Single-Page-Application made with React, Redux for the state management, and React-Router.

It can be hosted on any static hosting server.

We use GitHub pages for the production version to take advantage of js.org domain.

Start up

@michaelrambeau
michaelrambeau / micro.js
Created May 21, 2017 06:10
Micro-services
require('dotenv').config()
const { send } = require('micro')
const mongoose = require('mongoose')
const Restaurant = require('./models/Restaurant')
require('./models/Tag')
mongoose.Promise = global.Promise
module.exports = (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*')
@michaelrambeau
michaelrambeau / Routes.js
Created April 13, 2017 07:53
React Router v4
import React from 'react'
import Home from './Home'
import Header from './Header'
import ProjectList from './ProjectList'
import ProjectItem from './ProjectItem'
import { Route, Switch } from 'react-router-dom'
const NoMatch = () => (<div>No match!</div>)
const ProjectItemReadme = () => (<div>This is a wonderfull project!</div>)
@michaelrambeau
michaelrambeau / README.md
Last active April 2, 2017 09:50
To curry or not curry, that is the question

Comparing 2 implementations of the same function, with and without currying.

To be run from Ramda playground: http://ramdajs.com/repl/