/
AdapterFilters.ts
250 lines (199 loc) · 7.68 KB
/
AdapterFilters.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import type {
IDynAdapterFilters,
DynDataFilter,
DynFilterFn,
DynIndexerUpdateFn } from '../../types';
export class AdapterFilters<T> implements IDynAdapterFilters<T>
{
#filtersData: { filters: DynDataFilter<T>[] };
readonly #indexUpdate: DynIndexerUpdateFn;
#mapUnsubscribe: Map<Function, Function> = new Map();
constructor(indexUpdate: DynIndexerUpdateFn, filtersAdapter: { filters: DynDataFilter<T>[] })
{
this.#indexUpdate = indexUpdate;
this.#filtersData = filtersAdapter;
Object.freeze(this);
}
get length(): number { return this.#filtersData.filters.length; }
*[Symbol.iterator](): IterableIterator<DynDataFilter<T>> | void
{
if (this.#filtersData.filters.length === 0) { return; }
for (const entry of this.#filtersData.filters)
{
yield { ...entry };
}
}
add(...filters: (DynFilterFn<T>|DynDataFilter<T>)[])
{
/**
* Tracks the number of filters added that have subscriber functionality.
*/
let subscribeCount: number = 0;
for (const filter of filters)
{
const filterType = typeof filter;
if (filterType !== 'function' && (filterType !== 'object' || filter === null))
{
throw new TypeError(`AdapterFilters error: 'filter' is not a function or object.`);
}
let data: DynDataFilter<T> = void 0;
let subscribeFn: (indexUpdate: DynIndexerUpdateFn) => () => void = void 0;
if (filterType === 'function')
{
data = {
id: void 0,
filter: filter as DynFilterFn<T>,
weight: 1
};
subscribeFn = filter.subscribe;
}
else if (filterType === 'object')
{
if ('filter' in filter)
{
if (typeof filter.filter !== 'function')
{
throw new TypeError(`AdapterFilters error: 'filter' attribute is not a function.`);
}
if (filter.weight !== void 0 && typeof filter.weight !== 'number' ||
(filter.weight < 0 || filter.weight > 1))
{
throw new TypeError(
`AdapterFilters error: 'weight' attribute is not a number between '0 - 1' inclusive.`);
}
data = {
id: filter.id !== void 0 ? filter.id : void 0,
filter: filter.filter,
weight: filter.weight || 1
};
subscribeFn = filter.filter.subscribe ?? filter.subscribe;
}
else
{
throw new TypeError(`AdapterFilters error: 'filter' attribute is not a function.`);
}
}
// Find the index to insert where data.weight is less than existing values weight.
const index = this.#filtersData.filters.findIndex((value) =>
{
return data.weight < value.weight;
});
// If an index was found insert at that location.
if (index >= 0)
{
this.#filtersData.filters.splice(index, 0, data);
}
else // push to end of filters.
{
this.#filtersData.filters.push(data);
}
if (typeof subscribeFn === 'function')
{
const unsubscribe = subscribeFn(this.#indexUpdate);
// Ensure that unsubscribe is a function.
if (typeof unsubscribe !== 'function')
{
throw new TypeError(
'AdapterFilters error: Filter has subscribe function, but no unsubscribe function is returned.');
}
// Ensure that the same filter is not subscribed to multiple times.
if (this.#mapUnsubscribe.has(data.filter))
{
throw new Error(
'AdapterFilters error: Filter added already has an unsubscribe function registered.');
}
this.#mapUnsubscribe.set(data.filter, unsubscribe);
subscribeCount++;
}
}
// Filters with subscriber functionality are assumed to immediately invoke the `subscribe` callback. If the
// subscriber count is less than the amount of filters added then automatically trigger an index update manually.
if (subscribeCount < filters.length) { this.#indexUpdate(true); }
}
clear()
{
this.#filtersData.filters.length = 0;
// Unsubscribe from all filters with subscription support.
for (const unsubscribe of this.#mapUnsubscribe.values())
{
unsubscribe();
}
this.#mapUnsubscribe.clear();
this.#indexUpdate();
}
remove(...filters: (DynFilterFn<T>|DynDataFilter<T>)[])
{
const length = this.#filtersData.filters.length;
if (length === 0) { return; }
for (const data of filters)
{
// Handle the case that the filter may either be a function or a filter entry / object.
const actualFilter = typeof data === 'function' ? data : data !== null && typeof data === 'object' ?
data.filter : void 0;
if (!actualFilter) { continue; }
for (let cntr = this.#filtersData.filters.length; --cntr >= 0;)
{
if (this.#filtersData.filters[cntr].filter === actualFilter)
{
this.#filtersData.filters.splice(cntr, 1);
// Invoke any unsubscribe function for given filter then remove from tracking.
let unsubscribe: Function = void 0;
if (typeof (unsubscribe = this.#mapUnsubscribe.get(actualFilter)) === 'function')
{
unsubscribe();
this.#mapUnsubscribe.delete(actualFilter);
}
}
}
}
// Update the index a filter was removed.
if (length !== this.#filtersData.filters.length) { this.#indexUpdate(true); }
}
removeBy(callback: (id: any, filter: DynFilterFn<T>, weight: number) => boolean)
{
const length = this.#filtersData.filters.length;
if (length === 0) { return; }
if (typeof callback !== 'function')
{
throw new TypeError(`AdapterFilters error: 'callback' is not a function.`);
}
this.#filtersData.filters = this.#filtersData.filters.filter((data) =>
{
const remove = callback.call(callback, { ...data });
if (remove)
{
let unsubscribe: Function;
if (typeof (unsubscribe = this.#mapUnsubscribe.get(data.filter)) === 'function')
{
unsubscribe();
this.#mapUnsubscribe.delete(data.filter);
}
}
// Reverse remove boolean to properly filter / remove this filter.
return !remove;
});
if (length !== this.#filtersData.filters.length) { this.#indexUpdate(true); }
}
removeById(...ids: any[])
{
const length = this.#filtersData.filters.length;
if (length === 0) { return; }
this.#filtersData.filters = this.#filtersData.filters.filter((data) =>
{
let remove = 0;
for (const id of ids) { remove |= (data.id === id ? 1 : 0); }
// If not keeping invoke any unsubscribe function for given filter then remove from tracking.
if (!!remove)
{
let unsubscribe: Function;
if (typeof (unsubscribe = this.#mapUnsubscribe.get(data.filter)) === 'function')
{
unsubscribe();
this.#mapUnsubscribe.delete(data.filter);
}
}
return !remove; // Swap here to actually remove the item via array filter method.
});
if (length !== this.#filtersData.filters.length) { this.#indexUpdate(true); }
}
}