Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
<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 wh
Try some demos:
Fetching from HackerNews: ji8ky
Component A waits for the result of component B: 70908
function createAsset(fn: PromiseFn, lifespan = 0): {
read: (...args: any[]) => any;
preload: (...args: any[]) => void;
clear: (...args: any[]) => void;
peek: (...args: any[]) => any;
}
Each 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.
import React, { Suspense } from "react"
import { createAsset } from "use-asset"
// First create an asset, the arguments are user-provided.
const asset = createAsset(async (id, version) => {
const res = await fetch(`https://hacker-news.firebaseio.com/${version}/item/${id}.json`)
return await res.json()
})
// You can preload assets, these will be executed and cached immediately
asset.preload(10000, "v0")
function Post({ id }) {
// Request asset, this component will now suspend
const { by, title } = asset.read(id, "v0")
return <div>{title} by {by}</div>
}
function App() {
<Suspense fallback={null}>
<Post id={10000} />
</Suspense>
}
// This asset will be removed from the cache in 15 seconds
const asset = createAsset(fn, 15000)
// Clear all cached entries
asset.clear()
// Clear a specific entry
asset.clear("/image.png")
// This will either return the value (without suspense!) or undefined
asset.peek("/image.png")
function useAsset(fn: PromiseFn, args: any[]): any
useAsset.lifespan = 0
useAsset.read = (...args: any[]) => any
useAsset.preload = (fn: PromiseFn, ...args: any[]) => void
useAsset.clear = (...args: any[]) => void
useAsset.peek = (...args: any[]) => any
You can also use the useAsset
hook, 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.
import { useAsset } from "use-asset"
function Post({ id }) {
const { by, title } = useAsset(fn, [id])
return <div>{title} by {by}</div>
}
function App() {
<Suspense fallback={null}>
<Post id={1000} />
The hook has the same API as any asset:
// Bust cache in 15 seconds
useAsset.lifespan = 15000
useAsset(fn, ["/image.png"])
// Clear all cached entries
useAsset.clear()
// Clear a specific entry
useAsset.clear("/image.png")
// Preload entries
useAsset.preload(fn, "/image.png")
// This will either return the value (without suspense!) or undefined
useAsset.peek("/image.png")
FAQs
A data fetching strategy for React Suspense
The npm package use-asset receives a total of 31,380 weekly downloads. As such, use-asset popularity was classified as popular.
We found that use-asset demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.