Framework for state management of values
A state contains a value, it can be subscribed to and will update its subscribers when the value is changed.
A state can also express invalid value using the Result library.
All states are async by default.
Create a state like so, passing a value wrapped in a result.
let state = new State(Ok(1));
The state concept is devided into three contexts, Reader, Writer and Owner
The reader context is four methods, subscribe, unsubscribe, async then and related.
Use the reader context by using the StateRead type on variables.
let functionWithStateParam = (state: StateRead<number>) => {};
Subscribe and unsubscribe are used to add and remove a function to be updated with changes to the states value.
let update = true;
let func = state.subscribe((value) => {
if (value.ok) {
//Do stuff valid value
} else {
//Do stuff invalid value
}
}, update);
state.unsubscribe(func);
Then can be used either by using the await keyword or passing a function to the then method
let value = await state;
if (value.ok) {
//Do stuff valid value
} else {
//Do stuff invalid value
}
state.then((value) => {
if (value.ok) {
//Do stuff valid value
} else {
//Do stuff invalid value
}
});
Related is used to get related states to the state, a state can return other states that are relevant to the state
//A number state as an example
let related = state.related;
if (value.ok) {
let maxValue = await related.max;
if (value.ok) {
//Do stuff valid maximum value
} else {
//Do stuff invalid maximum value
}
} else {
//Do stuff invalid related
}
The writer context is just a write method, it will not nessesarily change the value of the state, it just request a change of the owner.
state.write(5);
The owner context is just all the rest of the methods on the state
To get full type functionality when extending a state you must do it like this, unless you know what you are doing
class StateSpecial<R, W = R, L extends StateRelated = {}, A = W> extends State<
R,
W,
L,
A
> {}
You can also extend a state and used fixed types, but you must supply all the types then
class StateSpecial extends State<number, number, {}, number> {}
You can descripe an enum state with the enum state helper
export const enum TestEnum {
Key1 = "key1",
Key2 = "key2",
}
const TestEnumList = {
[TestEnum.Key1]: {
name: "Key 1",
icon: optional svg icon,
},
[TestEnum.Key2]: {
name: "Key 2",
description: "Optional long description",
},
} satisfies StateEnumHelperList;
const TestEnumState = new State(
TestEnum.Key1
true,
new StateEnumHelper(TestEnumList)
)
- Fixed type of StateWrite missing get
- Changed related default type to {} so it wouldn't be any and not check
-
Made list on StateEnumHelper mandatory
Added more documentation - Fixed enum helpers
- Fixed helpers
- Changed helper system around
- Moved to a async/sync seperate setup, as there are too many situtation where true sync is needed
- Changed type of setter to make i more user friendly
- Fixed StateEnumLimits to return value from check
- Fixed StateNumberRelated unit being a number Changed some types on state to make it more obvious that Option is used Changed StateDerived to support different types for each state
- Added enum to statelimiter
- Removed code from state error as it makes no sense
- Fixed issue when derived state has a single state connected, it would not call the subscriber when the update flag was set high
- Fixed issue when writing to a state, that is awaiting or have a lazy function for initial value
- Fixed issue with using async state with lazy function
- Updated to rely on result
- Updated dependency
- Added stateResource to support remote states that cannot be instantly accessed