Skip to content

Instantly share code, notes, and snippets.

🏄
9-5, just to make a living

Conor Hastings conorhastings

🏄
9-5, just to make a living
Block or report user

Report or block conorhastings

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View machine.js
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
View type-boundary.js
export const userType = `
type User {
id: Int!
title: String!
}
`;
export const resolveUserData() {
return someFunctionThatGetsData();
}
View restructure.js
export default function(babel) {
const { types: t } = babel;
return {
visitor: {
VariableDeclaration(path) {
const node = path.node;
if (node.declarations && node.declarations.length === 1) {
const [{ init }] = node.declarations;
if (t.isTemplateLiteral(init)) {
View schedule.json
{
"1": [
[1, 2],
[1, 3],
[1, 9],
[1, 6],
[1, 3],
[1, 9],
[1, 6],
[1, 5],
View routeql.js
import gql from 'graphql-tag';
import { pick } = 'lodash'
function routeql({ query = {}, params = [], method, apiPrefix = "" }) {
const ast = gql`
${query}
`;
if (
ast.definitions.length > 1 ||
ast.definitions[0].selectionSet.selections.length > 1
View subscribable-request.js
export default function subscribableRequest({ baseUrl }) {
let subscriptions = {};
return {
fetch: ({ endpoint, ...rest }) => (
fetch(`${baseUrl}/${endpoint}`, ...rest)
.then(res => res.json())
.then(res => {
subscriptions[endpoint] &&
subscriptions[endpoint].forEach(sub => sub(res));
return res;
@conorhastings
conorhastings / state-component.js
Last active Mar 30, 2017
you can enact most of the behavior of redux with a simple component
View state-component.js
/* this lacks subscribe behavior or ability to dispatch from outside of component tree but that is generally not neccesary */
class State extends React.Component {
constructor(props) {
super(props);
this.state = YOUR_INITIAL_STATE;
}
reducer = (action, state, props) => {...newState};
/* setState takes an object of new state as first arg or a function of props and state that returns new state
* which we will use here
* we pass dispatch around and use it similarly to redux dispatch
View cache-component.js
function PromiseComponent(initializeComponent, loadingComponent) {
let cachedComponent;
return class PromiseComponent extends React.Component {
state = { Component: cachedComponent || loadingComponent || () => null };
componentDidMount() {
if (!cachedComponent) {
initializeComponent.then(component => {
cachedComponent = component.default;
this.setState({ Component: cachedComponent });
});
You can’t perform that action at this time.