UNPKG

simple-cache-provider

Version:

A basic cache for React applications

14 lines (12 loc) 1.67 kB
/** @license React v16.5.2 * simple-cache-provider.production.min.js * * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict';Object.defineProperty(exports,"__esModule",{value:!0});var k=require("react"); function l(h){function g(a,c){var b=f.get(a);void 0===b&&(b={map:new Map,head:null,tail:null,size:0},f.set(a,b));var d=b.map;a=d.get(c);if(void 0===a){if(a={status:0,suspender:null,key:c,value:null,error:null,next:null,previous:null},d.set(c,a),500<=b.size&&(c=b.tail,null!==c)){for(var e=0;50>e&&null!==c;e++)--b.size,d.delete(c.key),c=c.previous;b.tail=c;null!==c&&(c.next=null)}}else d=a.previous,c=a.next,null!==d?d.next=c:b.head=c,null!==c?c.previous=d:b.tail=d,--b.size;d=b.head;b.head=a;a.previous= null;a.next=d;null!==d?d.previous=a:b.tail=a;b.size+=1;return a}function e(a,c){a.status=1;a.suspender=c;c.then(function(b){a.status=2;a.suspender=null;a.value=b},function(b){a.status=3;a.suspender=null;a.error=b})}var f=new Map;return{invalidate:function(){h()},preload:function(a,c,b,d){a=g(a,c);switch(a.status){case 0:b=b(d),e(a,b)}},read:function(a,c,b,d){a=g(a,c);switch(a.status){case 0:throw b=b(d),e(a,b),b;case 1:throw a.suspender;case 2:return a.value;default:throw a.error;}}}} var m=l(function(){}),n=k.createContext(m);exports.createCache=l;exports.createResource=function(h,g){var e={read:function(f,a){if(void 0===g)return f.read(e,a,h,a);var c=g(a);return f.read(e,c,h,a)},preload:function(f,a){if(void 0===g)f.preload(e,a,h,a);else{var c=g(a);f.preload(e,c,h,a)}}};return e};exports.SimpleCache=n;