Reference Source
import SubtreeRetainer from 'flarum/utils/SubtreeRetainer'
public class | source

SubtreeRetainer

The SubtreeRetainer class keeps track of a number of pieces of data, comparing the values of these pieces at every iteration.

This is useful for preventing redraws to relatively static (or huge) components whose VDOM only depends on very few values, when none of them have changed.

See:

Example:

// Check two callbacks for changes on each update
this.subtree = new SubtreeRetainer(
  () => this.attrs.post.freshness,
  () => this.showing
);

// Add more callbacks to be checked for updates
this.subtree.check(() => this.attrs.user.freshness);

// In a component's onbeforeupdate() method:
return this.subtree.needsRebuild()

Constructor Summary

Public Constructor
public

constructor(callbacks: ...callbacks)

Member Summary

Public Members
public
public

data: {}

Method Summary

Public Methods
public

check(callbacks: ...Function)

Add another callback to be checked.

public

Invalidate the subtree, forcing it to be rerendered.

public

Return whether any data has changed since the last check.

Public Constructors

public constructor(callbacks: ...callbacks) source

Params:

NameTypeAttributeDescription
callbacks ...callbacks

Functions returning data to keep track of.

Public Members

public callbacks: * source

public data: {} source

Public Methods

public check(callbacks: ...Function) source

Add another callback to be checked.

Params:

NameTypeAttributeDescription
callbacks ...Function

public invalidate() source

Invalidate the subtree, forcing it to be rerendered.

public needsRebuild(): boolean source

Return whether any data has changed since the last check. If so, Mithril needs to re-diff the vnode and its children.

Return:

boolean