Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only invalidate referenced values #2865

Merged
merged 3 commits into from
May 27, 2019
Merged

only invalidate referenced values #2865

merged 3 commits into from
May 27, 2019

Conversation

Rich-Harris
Copy link
Member

This PR means that changes to local state only trigger an invalidation if the state is referenced in the markup, or in a reactive declaration, meaning less work and less code:

function instance($$self, $$props, $$invalidate) {
  let seconds = 0;
	
  onMount(() => {
    const interval = setInterval(() => {
-      $$invalidate('seconds', seconds += 1);
+      seconds += 1;
      console.log(`the component has been mounted for ${seconds} seconds`);
    }, 1000);
		
    return () => {
      clearInterval(interval);
    };
  });

  return {};
}

A more practical example:

<script>
  import { setContext } from 'svelte';

  setContext('WEBGL_SCENE', {
    invalidate: () => {
      if (!update_scheduled) {
        update_scheduled = true;
        Promise.resolve().then(draw);
      }
    },

    // ...
  });

  function draw() {
    // this currently 'dirties' the component; i don't think it should
    update_scheduled = false;

    // ...draw implementation goes here...
  }
</script>

<slot></slot>

There is a small change to observable behaviour (other than avoiding unnecessary work) — beforeUpdate and afterUpdate will no longer fire when unreferenced local state changes. I think that's probably the correct behaviour anyway, since no 'update' is happening, and it's useful to be able to 'clear' those kinds of values in afterUpdate without triggering another update (which necessitates the infinite loop guard).

Thoughts?

@Rich-Harris Rich-Harris merged commit ed72aea into master May 27, 2019
@Rich-Harris Rich-Harris deleted the less-invalidation branch May 27, 2019 10:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant