![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
async-signals
Advanced tools
Synthatic sugar and utilities to support async tasks on top of signal-polyfill
Synthatic sugar and utilities to support async tasks on top of signal-polyfill
.
npm install async-signals
An AsyncSignal
is just a signal that has an AsyncResult
as its value. AsyncResult
is defined as follows:
export type AsyncResult<T, E = unknown> =
| {
status: "pending";
}
| {
status: "completed";
value: T;
}
| {
status: "error";
error: E;
};
It has 3 possible states: pending
, completed
or error
. You can see that the lifecycle is really similar to that of a Promise
. The big difference is that an AsyncSignal
can update any watcher multiple times (eg. a UI component) throughout the lifecycle of an application when new completed values get discovered.
A common use case is to implement a signal that is responsible for some async task (eg. fetching data from the backend) only when it's watched.
import { AsyncState } from 'async-signals';
import { getMyFriendsUsersIds, onNewFriendAdded } from './requests.js';
let unsubscribe = undefined;
// Will contain all the user ids for all my friends, updated in real-time when a new friend is added
const myFriendsUsersIdsSignal = new AsyncState({ status: 'pending' }, {
[Signal.subtle.watched]: async () => {
const myFriendsUsersIds = await getMyFriendsUsersIds();
myFriendsUsersIdsSignal.set({
status: "completed",
value: myFriendsUsersIds,
});
unsubscribe = onNewFriendAdded(friendUserId => {
const currentFriends = myFriendsUsersIdsSignal.get().value;
const friends = [...currentFriends, friendUserId];
myFriendsUsersIdsSignal.set({
status: 'completed',
value: friends
});
});
},
[Signal.subtle.unwatched]: () => {
// We revert back to pending state so that the next time this signal is queried,
// the backend request is sent again
asyncState.set({
status: "pending",
});
unsubscribe?.();
},
});
Once we have source signals for some interesting state, we can create any downstread computed signals with AsyncComputed
.
import { AsyncComputed } from 'async-signals';
// Will contain the number of my friends, updated in real-time when a new friend is added
const myFriendsCountSignal = new AsyncComputed(() => {
// This will be re-run every time there is a new value for `myFriendsUsersIds`
const myFriendsUsersIds = myFriendsUsersIdsSignal.get();
// Return early any other status than completed
if (myFriendsUsersIds.status !== 'completed') return myFriendsUsersIds;
return myFriendsUsersIds.length;
});
Whenever this signal is watched, it will watch the myFriendsUsersIdsSignal
, which in its turn will make a request to the backend and listen to any new friends added. Whenever the value of myFriendsUsersIdsSignal
changes, either by the request to the backend being completed
or because there is a new friend added, the callback for myFriendsCountSignal
will be run again. With this, myFriendsCountSignal
can guarantee to any downstream watcher that it will contain the count of my friends, up to date with the state of the backend.
Sometimes we just want to read once the resulting state from a signal. For that we have toPromise
:
const myFriendsCount = await toPromise(myFriendsCountSignal);
[!WARN] Don't do this when reading a signal from a UI component. The UI component will not be update every time the value of the signal changes. Only do this in business logic that only needs to read the value of the signal once.
We can easily construct an AsyncSignal
from a Promise
with fromPromise
:
import { fromPromise } from 'async-signals';
import { getMyUserId } from './requests.js';
const myUserId = await fromPromise(() => getMyUserId());
This is useful for tasks whose result doesn't need to be updated after they resolve.
Image we need a myFriendsProfilesSignal
that will contain an up to date HashMap
: UserId
-> Profile
for all our friends.
We can implement myFriendsProfilesSignal
as follows:
import { AsyncComputed, joinMap } from 'async-signals';
import { getUserProfile } from './requests.js';
// Client-side "cache" so that we don't request the profile for a user if we already have it
const usersProfiles = new Map();
// Will contain all the profiles for all my friends, updated in real-time when a new friend is added
const myFriendsProfilesSignal = new AsyncComputed(() => {
// This will be re-run every time there is a new value for `myFriendsUsersIds`
const myFriendsUsersIds = myFriendsUsersIds.get();
// Return early any other status than completed
if (myFriendsUsersIds.status !== 'completed') return myFriendsUsersIds;
for (const friendUserId of myFriendsUsersIds.value) {
if (!usersProfiles.has(friendUserId)) {
usersProfiles.set(friendUserId, fromPromise(() => getUserProfile(friendUserId)));
}
}
return joinAsyncMap(friendsProfiles); // Will iterate over all the signals in the map and will join their results
// If any of the results is still pending, it will return "pending"
// If any of the results is an error, it will return that error
// If all results are completed, it will return the map of their values
});
FAQs
Synthatic sugar and utilities to support async tasks on top of signal-polyfill
The npm package async-signals receives a total of 114 weekly downloads. As such, async-signals popularity was classified as not popular.
We found that async-signals demonstrated a healthy version release cadence and project activity because the last version was released less than 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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.