-
-
Save snakeUni/8949f8453b6f5fa467806bd581e1acec to your computer and use it in GitHub Desktop.
Revisions
-
ryanflorence revised this gist
Dec 18, 2019 . 1 changed file with 17 additions and 10 deletions.There are no files selected for viewing
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 charactersOriginal 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; }) .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]; } else if (errors[key]) { throw errors[key]; } else if (inflight[key]) { throw inflight[key]; } else { 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 { preload, read, clear, Link: ResourceLink }; -
ryanflorence revised this gist
Dec 17, 2019 . 1 changed file with 1 addition and 3 deletions.There are no files selected for viewing
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 charactersOriginal 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 { preload, read, clear, Link: ResourceLink }; -
ryanflorence revised this gist
Dec 17, 2019 . No changes.There are no files selected for viewing
-
ryanflorence created this gist
Dec 17, 2019 .There are no files selected for viewing
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 charactersOriginal 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 }; }