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

async-ref

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-ref - npm Package Compare versions

Comparing version 0.1.1 to 0.1.2

2

package.json
{
"name": "async-ref",
"description": "Async ref objects for React. A tiny bridge between React.useSyncExternalStore and React.Suspense.",
"version": "0.1.1",
"version": "0.1.2",
"main": "dist/index.js",

@@ -6,0 +6,0 @@ "module": "dist/index.mjs",

@@ -7,3 +7,3 @@ # A tiny bridge between React.useSyncExternalStore and React Suspense

Furthermore, because `ref.current` will either return a value or _throw_ a promise, it implements the `MutableRefObject<T>` interface. That is,
Because `ref.current` will either return a value `T` or _throw_ a promise, the only thing it can return is a `T` and therefore implements the `MutableRefObject<T>` interface. That is,

@@ -30,4 +30,8 @@ ```ts

`AsyncMutableRefObject<T>` makes it easy for a typed _asynchronous_ external data source to work with React Suspense in a typed _synchornous_ way because we implement `getSnapshot` to always return the result of `ref.current`. A sample implementation of what you might do is the following.
`AsyncMutableRefObject<T>` makes it easy for a typed _asynchronous_ external data source to work with React Suspense in a typed _synchornous_ way because we can implement `getSnapshot` to just always return the result of `ref.current`.
## Example
A sample implementation of what you might do is the following where we define a data source that implements `getSnapshot` and `subscribe` functions.
```ts

@@ -65,3 +69,3 @@ type Subscriber = () => void;

As long as `getSnapshot()` is called from within the React-render cycle, it can be caught by Suspense.
As long as `getSnapshot()` is called from within the React-render cycle, the `Promise` it (might) throw will be caught by Suspense.

@@ -89,3 +93,3 @@ ```ts

## Using
## Use

@@ -126,4 +130,3 @@ `async-ref` exports a single function `createAsyncRef<T>()` which accepts an optional notifier function and returns an `AsyncMutableRefObject<T>`.

Alternatively, `AsyncMutableRefObject<T>` exposes `resolve`/`reject` functions for interacting with the
`Suspense`/`ErrorBoundary` portions of your component hierarchy.
Alternatively, `AsyncMutableRefObject<T>` exposes `resolve`/`reject` functions for a more familiar Promise-type feel.

@@ -156,4 +159,6 @@ ```ts

Finally, `AsyncMutableRefObject<T>` also implements `PromiseLike<T>` which means that you can also dereference the current value by awaiting on it. (If a current value is already set, it will immediately resolve.) This is safer than calling `ref.current` because it will wait for a current value to be set before resolving the promise, but of course does not work inside of a React component because it is asynchronous.
## Safely getting the value without Suspense
`AsyncMutableRefObject<T>` also implements `PromiseLike<T>` which means that you can dereference the current value by awaiting on it. (If a current value is already set, it will immediately resolve.) This is safer than calling `ref.current` because it will wait for a current value to be set before resolving the promise, but of course does not work inside of a React component because it is asynchronous.
```ts

@@ -160,0 +165,0 @@ import { createAsyncRef } from "async-ref";

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