-
Notifications
You must be signed in to change notification settings - Fork 1
State
runtoolkit edited this page Apr 13, 2026
·
1 revision
Per-entity key-value store. All operations are keyed by (entity, key).
import { State } from './src/state.js';
const state = new State();state.set('alice', 'score', 10);
state.get('alice', 'score'); // → 10
state.get('alice', 'missing', 0); // → 0 (default value)
state.has('alice', 'score'); // → true
state.delete('alice', 'score'); // → true if existed
state.clearAll('alice'); // remove all keys for entitystate.increment('alice', 'score', 5); // default by: 1
state.decrement('alice', 'score', 2);
state.addDefault('alice', 'score', 0); // set only if key doesn't exist
state.clamp('alice', 'score', 0, 100); // clamp current value in [0, 100]state.setFlag('alice', 'isReady'); // sets to true
state.setFlag('alice', 'isReady', false); // explicit false
state.unsetFlag('alice', 'isReady'); // sets to false
state.toggleFlag('alice', 'isReady'); // flips
state.getFlag('alice', 'isReady'); // → boolean// Swap two keys
state.swap('alice', 'a', 'b');
// Copy value from one entity/key to another
state.copy('alice', 'score', 'bob', 'score');
// Compare
state.is('alice', 'rank', 'admin'); // → true if value === 'admin'
// Cycle through an array of values
state.toggle('alice', 'mode', ['walk', 'run', 'sprint']);
// each call advances to the next value, wraps aroundstate.list('alice'); // → ['score', 'rank', ...]
state.listAll(); // → ['alice', 'bob', ...]