Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

use-asset

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-asset - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

.eslintrc.json

14

.size-snapshot.json
{
"index.js": {
"bundled": 2694,
"minified": 1217,
"gzipped": 543,
"bundled": 2827,
"minified": 1300,
"gzipped": 575,
"treeshaked": {

@@ -12,3 +12,3 @@ "rollup": {

"webpack": {
"code": 1788
"code": 1843
}

@@ -18,6 +18,6 @@ }

"index.cjs.js": {
"bundled": 6006,
"minified": 3284,
"gzipped": 1250
"bundled": 6129,
"minified": 3361,
"gzipped": 1278
}
}

@@ -1,1 +0,172 @@

"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var r=require("react");function e(r,e){var t;if("undefined"==typeof Symbol||null==r[Symbol.iterator]){if(Array.isArray(r)||(t=function(r,e){if(!r)return;if("string"==typeof r)return n(r,e);var t=Object.prototype.toString.call(r).slice(8,-1);"Object"===t&&r.constructor&&(t=r.constructor.name);if("Map"===t||"Set"===t)return Array.from(r);if("Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t))return n(r,e)}(r))||e&&r&&"number"==typeof r.length){t&&(r=t);var o=0;return function(){return o>=r.length?{done:!0}:{done:!1,value:r[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}return(t=r[Symbol.iterator]()).next.bind(t)}function n(r,e){(null==e||e>r.length)&&(e=r.length);for(var n=0,t=new Array(e);n<e;n++)t[n]=r[n];return t}var t=[],o=function(r,e){return r.length===e.length&&r.every((function(r,n){return r===e[n]}))};function i(r,n,t,i,a){void 0===i&&(i=0),void 0===a&&(a=!1);for(var u,f=e(n);!(u=f()).done;){var l=u.value;if(o(t,l.args)){if(a)return;if(l.error)throw l.error;if(l.response)return l.response;throw l.promise}}var c={args:t,promise:r.apply(void 0,t).then((function(r){return c.response=null==r||r})).catch((function(r){return c.error=r})).then((function(){i>0&&setTimeout((function(){var r=n.indexOf(c);-1!==r&&n.splice(r,1)}),i)}))};if(n.push(c),!a)throw c.promise}function a(r){for(var e=arguments.length,n=new Array(e>1?e-1:0),t=1;t<e;t++)n[t-1]=arguments[t];if(void 0===n)r.splice(0,r.length);else{var i=r.find((function(r){return o(n,r.args)}));if(i){var a=r.indexOf(i);-1!==a&&r.splice(a,1)}}}function u(e){for(var n=arguments.length,o=new Array(n>1?n-1:0),a=1;a<n;a++)o[a-1]=arguments[a];return r.useMemo((function(){return i(e,t,o,u.lifespan)}),o)}u.lifespan=0,u.clear=function(){for(var r=arguments.length,e=new Array(r),n=0;n<r;n++)e[n]=arguments[n];return a.apply(void 0,[t].concat(e))},u.preload=function(r){for(var e=arguments.length,n=new Array(e>1?e-1:0),o=1;o<e;o++)n[o-1]=arguments[o];i(r,t,n,u.lifespan,!0)},u.peek=function(){for(var r,e=arguments.length,n=new Array(e),i=0;i<e;i++)n[i]=arguments[i];return null==(r=t.find((function(r){return o(n,r.args)})))?void 0:r.response},exports.createAsset=function(r,e){void 0===e&&(e=0);var n=[];return{read:function(){for(var t=arguments.length,o=new Array(t),a=0;a<t;a++)o[a]=arguments[a];return i(r,n,o,e)},preload:function(){for(var t=arguments.length,o=new Array(t),a=0;a<t;a++)o[a]=arguments[a];i(r,n,o,e,!0)},clear:function(){for(var r=arguments.length,e=new Array(r),t=0;t<r;t++)e[t]=arguments[t];return a.apply(void 0,[n].concat(e))},peek:function(){for(var r,e=arguments.length,t=new Array(e),i=0;i<e;i++)t[i]=arguments[i];return null==(r=n.find((function(r){return o(t,r.args)})))?void 0:r.response}}},exports.useAsset=u;
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var react = require('react');
function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } it = o[Symbol.iterator](); return it.next.bind(it); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
var globalCache = [];
var deepEqual = function deepEqual(a, b) {
return a === b || a.length === b.length && a.every(function (arg, index) {
if (Array.isArray(arg) && Array.isArray(b[index])) return deepEqual(arg, b[index]);else return arg === b[index];
});
};
function handleAsset(fn, cache, args, lifespan, preload) {
if (lifespan === void 0) {
lifespan = 0;
}
if (preload === void 0) {
preload = false;
}
for (var _iterator = _createForOfIteratorHelperLoose(cache), _step; !(_step = _iterator()).done;) {
var _entry = _step.value;
// Find a match
if (deepEqual(args, _entry.args)) {
// If we're pre-loading and the element is present, just return
if (preload) return; // If an error occurred, throw
if (_entry.error) throw _entry.error; // If a response was successful, return
if (_entry.response) return _entry.response; // If the promise is still unresolved, throw
throw _entry.promise;
}
} // The request is new or has changed.
var entry = {
args: args,
promise: // Make the promise request.
fn.apply(void 0, args) // Response can't be undefined or else the loop above wouldn't be able to return it
// This is for promises that do not return results (delays for instance)
.then(function (response) {
return entry.response = response != null ? response : true;
})["catch"](function (e) {
return entry.error = e;
}).then(function () {
if (lifespan > 0) {
setTimeout(function () {
var index = cache.indexOf(entry);
if (index !== -1) cache.splice(index, 1);
}, lifespan);
}
})
};
cache.push(entry);
if (!preload) throw entry.promise;
}
function _clear(cache) {
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
if (args === undefined) cache.splice(0, cache.length);else {
var entry = cache.find(function (entry) {
return deepEqual(args, entry.args);
});
if (entry) {
var index = cache.indexOf(entry);
if (index !== -1) cache.splice(index, 1);
}
}
}
function createAsset(fn, lifespan) {
if (lifespan === void 0) {
lifespan = 0;
}
var cache = [];
return {
/**
* @throws Suspense Promise if asset is not yet ready
* @throws Error if the promise rejected for some reason
*/
read: function read() {
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
return handleAsset(fn, cache, args, lifespan);
},
preload: function preload() {
for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
args[_key3] = arguments[_key3];
}
return void handleAsset(fn, cache, args, lifespan, true);
},
clear: function clear() {
for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
args[_key4] = arguments[_key4];
}
return _clear.apply(void 0, [cache].concat(args));
},
peek: function peek() {
var _cache$find;
for (var _len5 = arguments.length, args = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
args[_key5] = arguments[_key5];
}
return (_cache$find = cache.find(function (entry) {
return deepEqual(args, entry.args);
})) == null ? void 0 : _cache$find.response;
}
};
}
function useAsset(fn) {
for (var _len6 = arguments.length, args = new Array(_len6 > 1 ? _len6 - 1 : 0), _key6 = 1; _key6 < _len6; _key6++) {
args[_key6 - 1] = arguments[_key6];
}
return react.useMemo(function () {
return handleAsset(fn, globalCache, args, useAsset.lifespan);
}, args);
}
useAsset.lifespan = 0;
useAsset.clear = function () {
for (var _len7 = arguments.length, args = new Array(_len7), _key7 = 0; _key7 < _len7; _key7++) {
args[_key7] = arguments[_key7];
}
return _clear.apply(void 0, [globalCache].concat(args));
};
useAsset.preload = function (fn) {
for (var _len8 = arguments.length, args = new Array(_len8 > 1 ? _len8 - 1 : 0), _key8 = 1; _key8 < _len8; _key8++) {
args[_key8 - 1] = arguments[_key8];
}
return void handleAsset(fn, globalCache, args, useAsset.lifespan, true);
};
useAsset.peek = function () {
var _globalCache$find;
for (var _len9 = arguments.length, args = new Array(_len9), _key9 = 0; _key9 < _len9; _key9++) {
args[_key9] = arguments[_key9];
}
return (_globalCache$find = globalCache.find(function (entry) {
return deepEqual(args, entry.args);
})) == null ? void 0 : _globalCache$find.response;
};
exports.createAsset = createAsset;
exports.useAsset = useAsset;

@@ -1,1 +0,88 @@

import{useMemo as e}from"react";const r=[],n=(e,r)=>e.length===r.length&&e.every((e,n)=>e===r[n]);function o(e,r,o,s=0,t=!1){for(const e of r)if(n(o,e.args)){if(t)return;if(e.error)throw e.error;if(e.response)return e.response;throw e.promise}const i={args:o,promise:e(...o).then(e=>i.response=null==e||e).catch(e=>i.error=e).then(()=>{s>0&&setTimeout(()=>{const e=r.indexOf(i);-1!==e&&r.splice(e,1)},s)})};if(r.push(i),!t)throw i.promise}function s(e,...r){if(void 0===r)e.splice(0,e.length);else{const o=e.find(e=>n(r,e.args));if(o){const r=e.indexOf(o);-1!==r&&e.splice(r,1)}}}function t(e,r=0){const t=[];return{read:(...n)=>o(e,t,n,r),preload:(...n)=>{o(e,t,n,r,!0)},clear:(...e)=>s(t,...e),peek:(...e)=>{var r;return null==(r=t.find(r=>n(e,r.args)))?void 0:r.response}}}function i(n,...s){return e(()=>o(n,r,s,i.lifespan),s)}i.lifespan=0,i.clear=(...e)=>s(r,...e),i.preload=(e,...n)=>{o(e,r,n,i.lifespan,!0)},i.peek=(...e)=>{var o;return null==(o=r.find(r=>n(e,r.args)))?void 0:o.response};export{t as createAsset,i as useAsset};
import { useMemo } from 'react';
const globalCache = [];
const deepEqual = (a, b) => a === b || a.length === b.length && a.every((arg, index) => {
if (Array.isArray(arg) && Array.isArray(b[index])) return deepEqual(arg, b[index]);else return arg === b[index];
});
function handleAsset(fn, cache, args, lifespan = 0, preload = false) {
for (const entry of cache) {
// Find a match
if (deepEqual(args, entry.args)) {
// If we're pre-loading and the element is present, just return
if (preload) return; // If an error occurred, throw
if (entry.error) throw entry.error; // If a response was successful, return
if (entry.response) return entry.response; // If the promise is still unresolved, throw
throw entry.promise;
}
} // The request is new or has changed.
const entry = {
args,
promise: // Make the promise request.
fn(...args) // Response can't be undefined or else the loop above wouldn't be able to return it
// This is for promises that do not return results (delays for instance)
.then(response => entry.response = response != null ? response : true).catch(e => entry.error = e).then(() => {
if (lifespan > 0) {
setTimeout(() => {
const index = cache.indexOf(entry);
if (index !== -1) cache.splice(index, 1);
}, lifespan);
}
})
};
cache.push(entry);
if (!preload) throw entry.promise;
}
function clear(cache, ...args) {
if (args === undefined) cache.splice(0, cache.length);else {
const entry = cache.find(entry => deepEqual(args, entry.args));
if (entry) {
const index = cache.indexOf(entry);
if (index !== -1) cache.splice(index, 1);
}
}
}
function createAsset(fn, lifespan = 0) {
const cache = [];
return {
/**
* @throws Suspense Promise if asset is not yet ready
* @throws Error if the promise rejected for some reason
*/
read: (...args) => handleAsset(fn, cache, args, lifespan),
preload: (...args) => void handleAsset(fn, cache, args, lifespan, true),
clear: (...args) => clear(cache, ...args),
peek: (...args) => {
var _cache$find;
return (_cache$find = cache.find(entry => deepEqual(args, entry.args))) == null ? void 0 : _cache$find.response;
}
};
}
function useAsset(fn, ...args) {
return useMemo(() => handleAsset(fn, globalCache, args, useAsset.lifespan), args);
}
useAsset.lifespan = 0;
useAsset.clear = (...args) => clear(globalCache, ...args);
useAsset.preload = (fn, ...args) => void handleAsset(fn, globalCache, args, useAsset.lifespan, true);
useAsset.peek = (...args) => {
var _globalCache$find;
return (_globalCache$find = globalCache.find(entry => deepEqual(args, entry.args))) == null ? void 0 : _globalCache$find.response;
};
export { createAsset, useAsset };
{
"name": "use-asset",
"version": "1.0.0",
"version": "1.0.1",
"description": "A data fetching strategy for React Suspense",

@@ -35,5 +35,2 @@ "main": "dist/index.cjs",

},
"dependencies": {
"fast-deep-equal": "^3.1.3"
},
"devDependencies": {

@@ -58,5 +55,14 @@ "@babel/core": "7.12.10",

"@typescript-eslint/parser": "^4.13.0",
"eslint": "^7.18.0",
"eslint-config-prettier": "^6.13.0",
"eslint-import-resolver-alias": "^1.1.2",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jest": "^24.1.0",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-react": "^7.21.5",
"eslint-plugin-react-hooks": "^4.2.0",
"husky": "^4.3.8",
"lint-staged": "^10.5.3",
"prettier": "^2.2.1",
"react": "^17.0.1",
"rollup": "^2.36.2",

@@ -63,0 +69,0 @@ "rollup-plugin-size-snapshot": "^0.12.0",

@@ -33,3 +33,2 @@ import path from 'path'

resolve({ extensions }),
terser(),
],

@@ -41,9 +40,4 @@ },

external,
plugins: [
babel(getBabelOptions({ useESModules: false })),
sizeSnapshot(),
resolve({ extensions }),
terser(),
],
plugins: [babel(getBabelOptions({ useESModules: false })), sizeSnapshot(), resolve({ extensions })],
},
]

@@ -13,3 +13,9 @@ import { useMemo } from 'react'

const globalCache: PromiseCache<any, any[]>[] = []
const equal = (a: any[], b: any[]) => a.length === b.length && a.every((arg, index) => arg === b[index])
const deepEqual = (a: any[], b: any[]): boolean =>
a === b ||
(a.length === b.length &&
a.every((arg, index) => {
if (Array.isArray(arg) && Array.isArray(b[index])) return deepEqual(arg, b[index])
else return arg === b[index]
}))

@@ -25,3 +31,3 @@ function handleAsset<Response, Args extends any[]>(

// Find a match
if (equal(args, entry.args)) {
if (deepEqual(args, entry.args)) {
// If we're pre-loading and the element is present, just return

@@ -64,3 +70,3 @@ if (preload) return

else {
const entry = cache.find((entry) => equal(args, entry.args))
const entry = cache.find((entry) => deepEqual(args, entry.args))
if (entry) {

@@ -83,3 +89,3 @@ const index = cache.indexOf(entry)

clear: (...args: Args) => clear(cache, ...args),
peek: (...args: Args): void | Response => cache.find((entry) => equal(args, entry.args))?.response,
peek: (...args: Args): void | Response => cache.find((entry) => deepEqual(args, entry.args))?.response,
}

@@ -100,4 +106,4 @@ }

useAsset.peek = <Response, Args extends any[]>(...args: Args) =>
globalCache.find((entry) => equal(args, entry.args))?.response as Response
globalCache.find((entry) => deepEqual(args, entry.args))?.response as Response
export { createAsset, useAsset }
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc