Stores are observable objects that contain a value.
Each store has a subscribe()
method which allows the execution of a callable whenever the value of the store changes, this callable provides the new value of the store as a parameter.
Stores can be of 2 types
- Writable stores
- Readable stores
You can read and write values in a writable store.
You can set the value on creation or some time after by using the set()
method.
use function CatPaw\Store\Writable;
function main() {
$store = new Writable("A"); // set on creation
echo $store->get().PHP_EOL; // will print "A"
$store->set("B"); // changing the value
echo $store->get().PHP_EOL; // will print "B"
}
Use subscribe()
to watch over the value of the store.
Note
Remember to $unsubscribe()
from the store after you're done with it.
use function CatPaw\Store\Writable;
function main() {
$store = new Writable("A");
$unsubscribe = $store->subscribe(static function($value) {
echo $value.PHP_EOL;
});
$store->set("B");
$unsubscribe();
// Unsubscribing here means the
// following `$store->set("C")` call
// will not trigger any subscribers.
$store->set("C");
}
A
B
Notice how it doesn't print C
; that is because by the time C
is set, $unsubscribe()
has already been invoked.
You can only read values from a readable store.
use function CatPaw\Store\Readable;
function main() {
$store = new Readable("default value", static function($set) {
$set("hello world");
});
$unsubscribe = $store->subscribe(static function($value){
echo $value.PHP_EOL;
})
$unsubscribe();
}
default value
hello world