forked from mathiasbynens/virtual-scroller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
item-source.js
47 lines (42 loc) · 1.16 KB
/
item-source.js
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
export const _getLength = Symbol();
export const _item = Symbol();
export const _key = Symbol();
export class ItemSource {
constructor({getLength, item, key}) {
if (typeof getLength !== 'function') {
throw new TypeError('getLength option must be a function');
}
if (typeof item !== 'function') {
throw new TypeError('item option must be a function');
}
if (typeof key !== 'function') {
throw new TypeError('key option must be a function');
}
this[_getLength] = getLength;
this[_item] = item;
this[_key] = key;
}
static fromArray(array, key) {
if (!Array.isArray(array)) {
throw new TypeError('First argument to fromArray() must be an array');
}
if (typeof key !== 'function' && key !== undefined) {
throw new TypeError(
'Second argument to fromArray() must be a function or undefined');
}
return new this({
getLength() {
return array.length;
},
item(index) {
return array[index];
},
key(index) {
return key ? key(array[index], index) : array[index];
}
});
}
get length() {
return this[_getLength]();
}
}