realm-js/lib/util.js

48 lines
1.2 KiB
JavaScript
Raw Normal View History

2015-10-08 00:08:19 +00:00
'use strict';
let idKey = exports.idKey = Symbol();
let realmKey = exports.realmKey = Symbol();
let resizeListKey = exports.resizeListKey = Symbol();
2015-10-08 00:08:19 +00:00
exports.createList = createList;
2015-10-08 00:08:19 +00:00
function createList(prototype, getterForLength, getterForIndex, setterForIndex) {
2015-10-14 23:05:49 +00:00
let list = prototype ? Object.create(prototype) : {};
let size = 0;
2015-10-08 00:08:19 +00:00
Object.defineProperty(list, 'length', {get: getterForLength});
2015-10-08 00:08:19 +00:00
list[resizeListKey] = function(length) {
if (length == null) {
length = this.length;
}
if (length == size) {
2015-10-08 00:08:19 +00:00
return;
}
if (length > size) {
let props = {};
for (let i = size; i < length; i++) {
props[i] = {
get: getterForIndex(i),
set: setterForIndex && setterForIndex(i),
enumerable: true,
configurable: true,
};
}
2015-10-08 00:08:19 +00:00
Object.defineProperties(this, props);
}
else if (length < size) {
for (let i = size - 1; i >= length; i--) {
delete this[i];
}
}
2015-10-08 00:08:19 +00:00
size = length;
};
2015-10-08 00:08:19 +00:00
return list;
2015-10-08 00:08:19 +00:00
}