Get last state of a derived store in svelte

580 Views Asked by At

Is it possible to get the last state of a store that is derived from a custom store in svelte?

export const derivedState(customStore, (derived) => {
   // what i would like to do here is something like
   if (derived.status === 'OK'){
      // do something and with derived and return
   } else {
      // return whatever the last value of derivedState was
   }  
}
1

There are 1 best solutions below

0
On

Store the last state outside and update when appropriate

let lastState;
export const derivedState(customStore, (derived) => {
   if (derived.status === 'OK'){
      lastState = computeNewStateSomehow(derived);
   }
   return lastState; 
}