
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
Implementing JavaScript state machines using React Hooks.
npm install loxia
I consider React Hooks to be particularly suitable for implementing state machines. Based on this idea, I developed a bookmark manager. It's UI makes intensive use of the Hooks and patterns developed here and serves as a real world test.
In general, all Hooks in this library are built to be used with any React Hooks compliant implementation. That would be, for example, Batis, Preact, and of course React. In the usage examples, Batis is always used, but as an alternative, the analogous usage with React is shown as a comment.
useTransitionA transition is a function with special runtime behavior that can be used to implement the correct behavior of the transition methods of a state machine. A transition executes the passed callback once and returns true if its dependencies have not changed, so it should depend on the state of the associated state machine.
import {Host} from 'batis'; // import * as React from 'react';
import {createTransitionHook} from 'loxia';
const useTransition = createTransitionHook(Host /* React */);
function useLock(): Lock {
const [locked, setLocked] = Host /* React */.useState(false);
const transition = useTransition(locked);
const lock = Host /* React */.useCallback(
() => transition(() => setLocked(true)),
[transition],
);
const unlock = Host /* React */.useCallback(
() => transition(() => setLocked(false)),
[transition],
);
return Host /* React */.useMemo(
() => (locked ? {locked, unlock} : {locked, lock}),
[locked],
);
}
function createTransitionHook(hooks: BatisHooks): UseTransition;
type UseTransition = (
...dependencies: readonly [unknown, ...unknown[]]
) => Transition;
type Transition = (callback?: () => void) => boolean;
useBinderA binding is a function that is tied to the life cycle of the Hook or component
it surrounds. Often React components are already unmounted and an associated
asynchronous operation should no longer have any effect. It is therefore useful
to bind the callback functions of Promise.then, Promise.catch, and also
setTimeout.
import {Host} from 'batis'; // import * as React from 'react';
import {createBinderHook} from 'loxia';
const useBinder = createBinderHook(Host /* React */);
function useExample() {
const bind = useBinder();
Host /* React */.useEffect(() => {
setTimeout(
bind(() => {
// ...
}),
);
});
}
function createBinderHook(hooks: BatisHooks): UseBinder;
type UseBinder = () => Bind;
type Bind = <TCallback extends (...args: any[]) => void>(
callback: TCallback,
) => Binding<TCallback>;
type Binding<TCallback extends (...args: any[]) => void> = (
...args: Parameters<TCallback>
) => boolean;
useReceiverA receiver is a state machine which allows the reception of a signal in the form
of a promise passed as an argument. A receiver is always in one of the following
states receiving, successful, or failed. As long as the reference to the
passed promise remains the same, a receiver represents the state of the promise.
When a reference to a new promise is passed, the old promise no longer affects
the receiver state.
It makes sense to use a receiver if an asynchronous operation is based on user input. If the user input changes in the meantime and a new asynchronous operation overwrites the old one, the old one should no longer have any effect.
import {Host} from 'batis'; // import * as React from 'react';
import {createReceiverHook} from 'loxia';
const useReceiver = createReceiverHook(Host /* React */);
function useAsyncJsonData(url) {
const signal = Host /* React */.useMemo(
() => fetch(url).then((response) => response.json()),
[url],
);
return useReceiver(signal);
}
function createReceiverHook(hooks: BatisHooks): UseReceiver;
type UseReceiver = <TValue>(signal: Promise<TValue>) => Receiver<TValue>;
type Receiver<TValue> =
| ReceivingReceiver
| SuccessfulReceiver<TValue>
| FailedReceiver;
interface ReceivingReceiver {
readonly state: 'receiving';
}
interface SuccessfulReceiver<TValue> {
readonly state: 'successful';
readonly value: TValue;
}
interface FailedReceiver {
readonly state: 'failed';
readonly error: unknown;
}
useSenderA sender is a state machine which allows to send exactly one signal at a time.
function createSenderHook(hooks: BatisHooks): UseSender;
type UseSender = () => Sender;
type Sender = IdleSender | SendingSender | FailedSender;
interface IdleSender {
readonly state: 'idle';
send(signal: Promise<unknown>): boolean;
}
interface SendingSender {
readonly state: 'sending';
}
interface FailedSender {
readonly state: 'failed';
readonly error: unknown;
send(signal: Promise<unknown>): boolean;
}
FAQs
Implementing JavaScript state machines using React Hooks.
The npm package loxia receives a total of 9 weekly downloads. As such, loxia popularity was classified as not popular.
We found that loxia 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
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.