{"name":"use-asset","version":"2.0.0-alpha-00","description":"A data fetching strategy for React Suspense","main":"dist/index.cjs","module":"dist/index.js","types":"dist/index.d.ts","sideEffects":false,"keywords":["react","suspense","resource","asset"],"author":{"name":"Paul Henschel"},"license":"MIT","repository":{"type":"git","url":"git+https://github.com/pmndrs/use-asset.git"},"homepage":"https://github.com/pmndrs/use-asset#readme","husky":{"hooks":{"pre-commit":"lint-staged"}},"lint-staged":{"*.{js,jsx,ts,tsx}":["eslint --fix"]},"scripts":{"build":"rollup -c","postbuild":"tsc --emitDeclarationOnly","prepublishOnly":"npm run build","test":"echo no tests yet"},"devDependencies":{"@babel/core":"7.15.5","@babel/plugin-proposal-class-properties":"^7.14.5","@babel/plugin-transform-modules-commonjs":"7.15.4","@babel/plugin-transform-parameters":"7.15.4","@babel/plugin-transform-runtime":"7.15.0","@babel/plugin-transform-template-literals":"7.14.5","@babel/preset-env":"7.15.4","@babel/preset-react":"7.14.5","@babel/preset-typescript":"^7.15.0","@rollup/plugin-babel":"^5.3.0","@rollup/plugin-node-resolve":"^13.0.4","@types/jest":"^27.0.1","@types/node":"^16.7.13","@types/react":"^17.0.20","@types/react-dom":"^17.0.9","@types/react-test-renderer":"^17.0.1","@typescript-eslint/eslint-plugin":"^4.31.0","@typescript-eslint/parser":"^4.31.0","eslint":"^7.32.0","eslint-config-prettier":"^8.3.0","eslint-import-resolver-alias":"^1.1.2","eslint-plugin-import":"^2.24.2","eslint-plugin-jest":"^24.4.0","eslint-plugin-prettier":"^4.0.0","eslint-plugin-react":"^7.25.1","eslint-plugin-react-hooks":"^4.2.0","husky":"^7.0.2","lint-staged":"^11.1.2","prettier":"^2.3.2","react":"^0.0.0-experimental-031abd24b-20210907","rollup":"^2.56.3","rollup-plugin-size-snapshot":"^0.12.0","rollup-plugin-terser":"^7.0.2","typescript":"^4.4.2"},"peerDependencies":{"react":">=18.0"},"dependencies":{"fast-deep-equal":"^3.1.3"},"readme":"<p align=\"left\">\n  <a id=\"cover\" href=\"#cover\"><img src=\"img/cover.svg\" alt=\"This library allows you to create cached assets, which can be promises, async functions or even dynamic imports. These assets then have the ability to suspend the component in which they are read. This makes it easier to orchestrate async tasks and gives you the ability to set up fallbacks and error-handling declaratively.\" /></a>\n</p>\n\n[![Build Size](https://img.shields.io/bundlephobia/min/use-asset?label=bunlde%20size&style=flat&colorA=000000&colorB=000000)](https://bundlephobia.com/result?p=use-asset)\n[![Build Status](https://img.shields.io/travis/pmndrs/use-asset/master?style=flat&colorA=000000&colorB=000000)](https://travis-ci.org/pmndrs/use-asset)\n[![Version](https://img.shields.io/npm/v/use-asset?style=flat&colorA=000000&colorB=000000)](https://www.npmjs.com/package/use-asset)\n[![Downloads](https://img.shields.io/npm/dt/use-asset.svg?style=flat&colorA=000000&colorB=000000)](https://www.npmjs.com/package/use-asset)\n\n<p align=\"left\">\n  <a id=\"async-assets\" href=\"#async-assets\"><img src=\"img/async-assets.svg\" alt=\"Dealing with async assets\" /></a>\n</p>\n\n<p align=\"left\">\n  <a id=\"using-assets\" href=\"#using-assets\"><img src=\"img/createAsset.svg\" alt=\"Types\" /></a>\n</p>\n\nEach asset you create comes with its own cache. When you request something from it, the arguments that you pass will act as cache-keys. If you request later on using the same keys, it won't have to re-fetch but serves the result that it already knows.\n\n```jsx\nimport React, { Suspense } from \"react\"\nimport { createAsset } from \"use-asset\"\n\n// Create a cached source\nconst asset = createAsset(async (id, version) => {\n  // Any async task can run in here, fetch requests, parsing, workers, promises, ...\n  const res = await fetch(`https://hacker-news.firebaseio.com/${version}/item/${id}.json`)\n  return await res.json()\n})\n\nfunction Post({ id }) {\n  // Then read from it ...\n  const { by, title } = asset.read(id, \"v0\") // As many cache keys as you need\n  // By the time we're here the async data has resolved\n  return <div>{title} by {by}</div>\n}\n\nfunction App() {\n  <Suspense fallback={<div>loading...</div>}>\n    <Post id={10000} />\n  </Suspense>\n}\n```\n\n#### Preloading assets\n\n```jsx\n// You can preload assets, these will be executed and cached immediately\nasset.preload(\"/image.png\")\n```\n\n#### Cache busting strategies\n\n```jsx\n// This asset will be removed from the cache in 15 seconds\nconst asset = createAsset(promiseFn, 15000)\n// Clear all cached entries\nasset.clear()\n// Clear a specific entry\nasset.clear(\"/image.png\")\n```\n\n#### Peeking into entries outside of suspense\n\n```jsx\n// This will either return the value (without suspense!) or undefined\nasset.peek(\"/image.png\")\n```\n\n<p align=\"left\">\n  <a id=\"hooks-global-cache\" href=\"#hooks-global-cache\"><img src=\"img/hooks-global-cache.svg\" alt=\"Hooks and global cache\" /></a>\n</p>\n\n<p align=\"left\">\n  <a id=\"cover\" href=\"#hooks-and-global-cache\"><img src=\"img/useAsset.svg\" alt=\"Types\" /></a>\n</p>\n\nYou can also use the `useAsset` hook, which is modelled after [react-promise-suspense](https://github.com/vigzmv/react-promise-suspense). This makes it possible to define assets on the spot instead of having to define them externally. They use a global cache, anything you request at any time is written into it.\n\n```jsx\nimport { useAsset } from \"use-asset\"\n\nfunction Post({ id }) {\n  const { by, title } = useAsset(async (id, version) => {\n    // Any async task can run in here, fetch requests, parsing, workers, promises, ...\n    const res = await fetch(`https://hacker-news.firebaseio.com/${version}/item/${id}.json`)\n    return await res.json()\n  }, id, \"v0\") // As many cache keys as you need\n  // By the time we're here the async data has resolved\n  return <div>{title} by {by}</div>\n}\n\nfunction App() {\n  <Suspense fallback={<div>loading...</div>}>\n    <Post id={1000} />\n```\n\n#### Cache busting, preload and peeking\n\nThe hook has the same API as any asset:\n\n```jsx\n// Bust cache in 15 seconds\nuseAsset.lifespan = 15000\nuseAsset(promiseFn, \"/image.png\")\n// Clear all cached entries\nuseAsset.clear()\n// Clear a specific entry\nuseAsset.clear(\"/image.png\")\n// Preload entries\nuseAsset.preload(promiseFn, \"/image.png\")\n// This will either return the value (without suspense!) or undefined\nuseAsset.peek(\"/image.png\")\n```\n\n<p align=\"left\">\n  <a id=\"recipes\" href=\"#recipes\"><img src=\"img/recipes.svg\" alt=\"Recipes\" /></a>\n</p>\n\n#### Simple data fetching\n\nFetching posts from hacker-news: [codesandbox](https://codesandbox.io/s/use-asset-demo-forked-ji8ky)\n\n#### Infinite load on scroll\n\nFetching HN posts infinitely: [codesandbox](https://codesandbox.io/s/use-asset-forked-ouzkc)\n\n#### Async dependencies\n\nComponent A waits for the result of component B: [codesandbox](https://codesandbox.io/s/use-asset-dependency-70908)\n","readmeFilename":"readme.md","gitHead":"8651d5be3f1e3aed1bbd1c9c5ca44efe0c788847","bugs":{"url":"https://github.com/pmndrs/use-asset/issues"},"_id":"use-asset@2.0.0-alpha-00","_nodeVersion":"12.18.2","_npmVersion":"6.14.5","dist":{"integrity":"sha512-PdXDkyDacwvgsBzL2MLqK1UXNnLBULtIAUB5Hv8HpoIdtv/5nIvLEeZiy6dfrui0GbPNL/RjEo3Ciav0gE7tvg==","shasum":"8a9be97a035fb48dacc38fd2bd21c3b0e867c0c1","tarball":"http://registry-npm.ywwl.com/use-asset/download/use-asset-2.0.0-alpha-00.tgz","fileCount":18,"unpackedSize":185647,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhOIUbCRA9TVsSAnZWagAARUcP/1yMS6KV7KZHxum2sfbl\neTuTe/d/qSRpABgFClYbE8RRlWmaQJIisXpkJOpT+hD901vEYla5GMpAju0Y\nGFvKp+rhzKEqa+r7GZoc3SVHPcEnUlNdUDCqFJEzNFFhsL3XNPMP1JQlMezq\nZ7+PldbGkns9mAubVvWwKD3kdFSGe7tXrZkZLEoLuyG4yyBLanGTn7poLQQE\nZQXu/YfLLffhCfsXypjIcZ84dEhZ+On5aA0eWZUjL4NKHx+KLFMRP4SgYUat\nH3r3n6377v0sv/PB6Umf5+6q6oWEGgYKlU/EWomwmlA7+/JsyigZW99Ov12C\nkzRBJl4Z7ZDMhSJWfBa9EwgFdOvsMI+pRwVrNmqcpEgNz/g6khxI6RvmMlKv\n/WQKVrr/TaEGVZ/+jiDz20q57ZEeEoUN0so81kjCapzGmu4zqwSqYqyYT2PV\nexRz26msVBIlgpB19PzqUIODTRRTABlX6JOWCSzVONzHbVuWUQm/ZMeW0yxb\nZ1kCDdLg5ajoptztsSm0M2IUDwGSdYRIzuIet8Ra7BcEuO0YlE9TeHErU1nP\n9K1VsvzkvE1J3z5hwpD0Nu0MCLU5CwlplgZtDM8o5rEGpi271fezPor+JnLx\npte5WN91SHkjZLY6pru47juQpCEAePurKuZMfzjyF8SdVTGUtaFpCip27Pt1\nGASY\r\n=74LJ\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDdqaXobTLec1OpZXinZs1fihgreU1Yr2rO8XVKmwDQ1QIhAPagEW5d3usDm7y+ackRMXpLGwO4EfyDTJ6j3rb7PBOg"}],"size":65684,"noattachment":false,"key":"/use-asset/-/use-asset-2.0.0-alpha-00.tgz"},"_npmUser":{"name":"drcmda","email":"drcmda@gmail.com"},"directories":{},"maintainers":[{"name":"drcmda","email":"drcmda@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/use-asset_2.0.0-alpha-00_1631094043780_0.19979233013646458"},"_hasShrinkwrap":false,"publish_time":1631094043916,"_cnpm_publish_time":1631094043916,"dist-tags":{"alpha":"2.0.0-alpha-00","latest":"1.0.4"}}