Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
@platform/state
Advanced tools
Readme
A small, simple rx/observable based state-machine.
For applying to UI see the react bindings at @platform/state.react
yarn add @platform/state
To work abstractly with a state library use the isolated type library:
yarn add @platform/state.types
Define your model
and mutation events
:
type IMyModel = {
count: number;
};
type MyEvent = IIncrementEvent | IDecrementEvent;
type IIncrementEvent = { type: 'TEST/increment'; payload: { by: number } };
type IDecrementEvent = { type: 'TEST/decrement'; payload: { by: number } };
type IStatustEvent = { type: 'TEST/status'; payload: { status: string } };
Create a new state-machine:
import { Store } from '@platform/state';
const initial: IMyModel = { count: 0, status: string };
const store = Store.create<IMyModel, MyEvent>({ initial });
Define a listener that mutates the state based on a specific event type (equivalent to a "reducer"):
store.on<ITestIncrementEvent>('TEST/increment').subscribe((e) => {
const count = e.state.count + e.payload.by;
const next = { ...e.state, count };
e.change(next); // UPDATE: New copy of state applied.
});
alternatively you can use "mutation like" syntax by passing a change function:
store
.on<ITestIncrementEvent>('TEST/increment')
.subscribe(e => {
e.change(draft => {
draft.count += e.payload.by; // UPDATE: New "structurally shared" immutable changes applied.
}));
});
This safely modifies an immutable clone of the state using "structural sharing" for efficiency (via immer).
Dispatch events to change state:
store.state; // => count === 0
store.dispatch({ type: 'TEST/increment', payload: { by: 1 } });
store.state; // => count === 1
Listen for changes to the state and react accordingly, for instance updating UI that may be rendering the state.:
store.changed$.subscribe((e) => {
// ...
});
Add logic that reacts to events asynchronously and dispatches new update events (equivalent to an "epic"):
store
.on<IIncrementEvent>('TEST/increment')
.pipe(debounceTime(300))
.subscribe(async (e) => {
const status = await getNetworkStatus();
e.dispatch({ type: 'TEST/status', payload: { status } });
});
FAQs
A small, simple, strongly typed, [rx/observable] state-machine.
The npm package @platform/state receives a total of 69 weekly downloads. As such, @platform/state popularity was classified as not popular.
We found that @platform/state 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
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.