-
Notifications
You must be signed in to change notification settings - Fork 18
/
InvertedNodeCollection.ts
63 lines (54 loc) · 2.05 KB
/
InvertedNodeCollection.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*!
* Copyright 2021 Cognite AS
*/
import { NumericRange, IndexSet } from '@reveal/utilities';
import { AreaCollection } from './prioritized/AreaCollection';
import { CdfModelNodeCollectionDataProvider } from './CdfModelNodeCollectionDataProvider';
import { NodeCollection } from './NodeCollection';
import { SerializedNodeCollection } from './SerializedNodeCollection';
/**
* Node collection that inverts the result from another node collection.
*/
export class InvertedNodeCollection extends NodeCollection {
public static readonly classToken = 'InvertedNodeCollection';
private readonly _allTreeIndicesRange: NumericRange;
private readonly _innerCollection: NodeCollection;
private _cachedIndexSet?: IndexSet;
constructor(model: CdfModelNodeCollectionDataProvider, innerSet: NodeCollection) {
super(InvertedNodeCollection.classToken);
this._innerCollection = innerSet;
this._innerCollection.on('changed', () => {
this._cachedIndexSet = undefined;
this.notifyChanged();
});
this._allTreeIndicesRange = new NumericRange(0, model.nodeCount);
}
get isLoading(): boolean {
return this._innerCollection.isLoading;
}
getIndexSet(): IndexSet {
if (this._cachedIndexSet === undefined) {
const inner = this._innerCollection.getIndexSet();
const invertedIndices = new IndexSet();
invertedIndices.addRange(this._allTreeIndicesRange);
invertedIndices.differenceWith(inner);
this._cachedIndexSet = invertedIndices;
}
return this._cachedIndexSet;
}
getAreas(): AreaCollection {
throw new Error(`${this.getAreas.name} is not supported for ${InvertedNodeCollection.name}`);
}
serialize(): SerializedNodeCollection {
return { token: this.classToken, state: { innerCollection: this._innerCollection.serialize() } };
}
/**
* Not supported.
* @throws Always throws an error.
*/
clear(): never {
// clearing the underlying set would result in all nodes being added to this set
// which would feel counter-intuitive.
throw new Error('clear() is not supported');
}
}