jwidget/collection/SetFilterer

Consumption

import SetFilterer, {startFilteringSet} from "jwidget/collection/SetFilterer";

Default export#

Hierarchy#

Description#

T
Value type.

SetFilterer binds one set to another, filling it with values of the source set a callback function returns a truthy value for.

const source = new BindableSet([1, 2, 3]);
const filterer = new SetFilterer(source, x => x % 2 === 1);
const target = filterer.target;
expect(Array.from(target.native).sort()).eql([1, 3]);

// Target collection is automatically synchronized with the original collection.
source.addAll([4, 5, 6]);
expect(Array.from(target.native).sort()).eql([1, 3, 5]);

// Destroy the filterer to stop synchronization.
filterer.destroy();

Features and examples

You can pass target set in a config option:

const source = new BindableSet([1, 2, 3]);
const target = new BindableSet<number>();
const filterer = new SetFilterer(source, x => x % 2 === 1, {target});

If you want to filter the values into a new set, use shorthand function startFilteringSet instead:

const source = new BindableSet([1, 2, 3]);
const target = startFilteringSet(source, x => x % 2 === 1);

// Destroy the target array to stop synchronization.
target.destroy();

Synchronizer rules

  • Filtered values of the source collection get added to target immediately on the synchronizer initialization.
  • Filtered values get deleted from target set on the synchronizer destruction.
  • Target set gets synchronized with source set continuously until the synchronizer is destroyed.
  • You can build the target set from multiple sources as long as all values are unique.

Constructor#

new SetFilterer<T>(source: ReadonlyBindableSet<T>, test: (value: T) => boolean, config?: SetFilterer.Config<T>)

source
Source set.
test
Filtering criteria.
config
Filterer configuration.

Fields#

source#

readonly source: ReadonlyBindableSet<T>

Source set.
target#

readonly target: ReadonlyBindableSet<T>

Target set.

Methods#

destroy (inherited from Class)#

(): void

Class destructor invocation method. Destroys all aggregated objects and calls destroyObject method. You must call this method explicitly from outside, because JavaScript doesn't support automatic class destructor calling.

const object = new MyClass();

// ...

// When the object is not needed anymore, destroy it.
object.destroy();

Alternatively (and optimally), you should use own method to aggregate this object inside another one.

destroyObject (inherited from Class)#

protected (): void

Class destructor implementation. Called in destroy method after destruction of all aggregated objects. The logic of class instance destruction should usually be implemented here. If you override this method, remember to call super.destroyObject() at the end of the method:

destroyObject() {
    // Release resources
    ...

    // Call superclass destructor
    super.destroyObject();
}
own (inherited from IClass)#

<T extends Destroyable>(obj: T): T

obj
Object to aggregate.
returns
obj

Aggregates the object. It means that the specified object is automatically destroyed on this object destruction. The aggregated objects are destroyed in reverse order. Returns obj object, which makes it easy to use in field definition:

private selected = this.own(new Property(false));
owning (inherited from IClass)#

(obj: Destroyable): this

obj
Object to aggregate.
returns
this

The same as own, but returns this, which makes it easy to use in object instantiation:

const items = new BindableArray().ownValues();
return new Panel(items).owning(items);

SetFilterer.Config#

T
Value type.

Configuration of SetFilterer.

target#

readonly target?: IBindableSet<T>

Target set.

startFilteringSet#

<T>(source: ReadonlyBindableSet<T>, test: (value: T) => boolean): DestroyableReadonlyBindableSet<T>

source
Source set.
test
Filtering criteria.
returns
Target set.
Creates a new set bound to another set with SetFilterer.