phase is a zero dependency micro-store/change emitter.
- global stores
- local stores
- store change subscriptions
- <1kb size
- zero dependencies
- advanced path access/subscriptions
- plugins (ex. react)
npm i @klintm/phase
import Phase from "@klintm/phase";
// To create a new store an initial state object & an optional function returning actions are passed
const phase = Phase(
{
people: [
{
name: "Anon",
age: 123
}
],
count: 0
},
state => ({
increment: () => state.set("count", state("count") + 1),
decrement: () => state.set("count", state("count") - 1)
})
);
phase.set("count", 5); // Sets count to 5
phase.set("people[0].age", 25); // Path accessors for both objects and arrays
phase.set("people[0]['name']"); // Strings can be used as well
const ageChanged = (oldAge, newAge) => console.log(`You are: ${newAge}`);
phase.on("people[0].age", ageChanged); // Subscribe to changes
phase.off(ageChanged); // Remove subscriptions
phase.on(["people", "count"], console.log); // Subscribe to multiple changes
phase.actions.increment(); // Action dispatches