Skip to content

Add a subscription to a store's value #21

Answered by Marcisbee
terrasoff asked this question in General
Discussion options

You must be logged in to vote

Hey for triggering update event you can create a utility function that uses subscribe and tracks changes in values across time. This should work fine with full typescript types support:

import { Exome, subscribe } from "exome";

function subscribeToValueChanges<Store extends Exome>(
  store: Store,
  properyNames: (keyof Store)[],
  callback: (instance: Store) => void,
) {
  const previousValueMap: Partial<Record<keyof Store, any>> = {};

  return subscribe(store, (instance) => {
    let hasChanges = false;

    for (const name of properyNames) {
      const previousValue = previousValueMap[name];
      const currentValue = instance[name];

      if (previousValue !== currentValue) {

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by Marcisbee
Comment options

You must be logged in to vote
1 reply
@Marcisbee
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants