Skip to content

Instantly share code, notes, and snippets.

@snakeUni
Forked from ryanflorence/createResource.js
Created December 18, 2019 02:19
Show Gist options
  • Select an option

  • Save snakeUni/8949f8453b6f5fa467806bd581e1acec to your computer and use it in GitHub Desktop.

Select an option

Save snakeUni/8949f8453b6f5fa467806bd581e1acec to your computer and use it in GitHub Desktop.

Revisions

  1. @ryanflorence ryanflorence revised this gist Dec 18, 2019. 1 changed file with 17 additions and 10 deletions.
    27 changes: 17 additions & 10 deletions createResource.js
    Original file line number Diff line number Diff line change
    @@ -4,12 +4,17 @@ import { Link } from "react-router-dom";
    export function createResource(getPromise) {
    let cache = {};
    let inflight = {};
    let errors = {};

    function load(key) {
    inflight[key] = getPromise(key).then(val => {
    delete inflight[key];
    cache[key] = val;
    });
    inflight[key] = getPromise(key)
    .then(val => {
    delete inflight[key];
    cache[key] = val;
    })
    .catch(error => {
    errors[key] = error;
    });
    return inflight[key];
    }

    @@ -21,13 +26,13 @@ export function createResource(getPromise) {
    function read(key) {
    if (cache[key] !== undefined) {
    return cache[key];
    }

    if (inflight[key]) {
    } else if (errors[key]) {
    throw errors[key];
    } else if (inflight[key]) {
    throw inflight[key];
    } else {
    throw load(key);
    }

    throw load(key);
    }

    function clear(key) {
    @@ -40,7 +45,9 @@ export function createResource(getPromise) {

    function ResourceLink({ cacheKey, ...props }) {
    const _preload = () => preload(cacheKey);
    return <Link onMouseEnter={_preload} onFocus={_preload} {...props} />;
    return (
    <Link onMouseEnter={_preload} onFocus={_preload} {...props} />
    );
    }

    return { preload, read, clear, Link: ResourceLink };
  2. @ryanflorence ryanflorence revised this gist Dec 17, 2019. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions createResource.js
    Original file line number Diff line number Diff line change
    @@ -40,9 +40,7 @@ export function createResource(getPromise) {

    function ResourceLink({ cacheKey, ...props }) {
    const _preload = () => preload(cacheKey);
    return (
    <Link onMouseEnter={_preload} onFocus={_preload} {...props} />
    );
    return <Link onMouseEnter={_preload} onFocus={_preload} {...props} />;
    }

    return { preload, read, clear, Link: ResourceLink };
  3. @ryanflorence ryanflorence revised this gist Dec 17, 2019. No changes.
  4. @ryanflorence ryanflorence created this gist Dec 17, 2019.
    49 changes: 49 additions & 0 deletions createResource.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    import React from "react";
    import { Link } from "react-router-dom";

    export function createResource(getPromise) {
    let cache = {};
    let inflight = {};

    function load(key) {
    inflight[key] = getPromise(key).then(val => {
    delete inflight[key];
    cache[key] = val;
    });
    return inflight[key];
    }

    function preload(key) {
    if (cache[key] !== undefined || inflight[key]) return;
    load(key);
    }

    function read(key) {
    if (cache[key] !== undefined) {
    return cache[key];
    }

    if (inflight[key]) {
    throw inflight[key];
    }

    throw load(key);
    }

    function clear(key) {
    if (key) {
    delete cache[key];
    } else {
    cache = {};
    }
    }

    function ResourceLink({ cacheKey, ...props }) {
    const _preload = () => preload(cacheKey);
    return (
    <Link onMouseEnter={_preload} onFocus={_preload} {...props} />
    );
    }

    return { preload, read, clear, Link: ResourceLink };
    }