Writable example:
log("--------- writable example ---------");
let count = writable(0, () => {
log("got a subscriber");
return () => log('no more subscribers');
});
count.set(1); // does nothing
let unsubscribe = count.subscribe(value => {
log(value);
}); // logs 'got a subscriber', then '1'
unsubscribe(); // logs 'no more subscribers'
log("------- end writable example -------");
Readable example:
log("--------- readable example ---------");
let time = readable(null, (set) => {
set(new Date());
const interval = setInterval(() => { set(new Date()); }, 1000);
return () => clearInterval(interval);
});
// Exercise sarting/stopping of subscriptions
let unsub1 = time.subscribe((v) => { log("1: value:", v); });
setTimeout(unsub1, 3000)
let unsub2 = time.subscribe((v) => { log("2: value:", v); });
setTimeout(unsub2, 6000)
setTimeout(() => {
log(get(time) + "");
log("------- end readable example -------");
}, 6100)
Output