RN: Avoid Infinite Loop w/ Polyfills

Reviewed By: voideanvalue

Differential Revision: D3472319

fbshipit-source-id: 87c8bd6719eb1771ec16c7e363cec9ee247d87fe
This commit is contained in:
Tim Yung 2016-06-23 19:09:00 -07:00 committed by Facebook Github Bot 1
parent fa6022dc1a
commit 9cb28b9a7e
1 changed files with 26 additions and 17 deletions

View File

@ -77,7 +77,6 @@ function defineProperty(object: Object, name: string, newValue: mixed): void {
value: object[name], value: object[name],
}); });
} }
const {enumerable, writable} = descriptor || {}; const {enumerable, writable} = descriptor || {};
Object.defineProperty(object, name, { Object.defineProperty(object, name, {
configurable: true, configurable: true,
@ -87,32 +86,42 @@ function defineProperty(object: Object, name: string, newValue: mixed): void {
}); });
} }
function defineLazyProperty( function defineLazyProperty<T>(
object: Object, object: Object,
name: string, name: string,
getValue: () => mixed getNewValue: () => T
): void { ): void {
const descriptor = getPropertyDescriptor(object, name); const descriptor = getPropertyDescriptor(object, name);
if (descriptor) { if (descriptor) {
const backupName = `original${name[0].toUpperCase()}${name.substr(1)}`; const backupName = `original${name[0].toUpperCase()}${name.substr(1)}`;
Object.defineProperty(object, backupName, descriptor); Object.defineProperty(object, backupName, descriptor);
} }
const config = {
const {enumerable, writable} = descriptor || {};
Object.defineProperty(object, name, {
configurable: true, configurable: true,
enumerable: enumerable !== false, enumerable: descriptor ? descriptor.enumerable !== false : true,
get() { writable: descriptor ? descriptor.writable !== false : true,
return (object[name] = getValue()); };
}, let value;
set(value) { let valueSet = false;
Object.defineProperty(object, name, { function getValue(): T {
configurable: true, // WORKAROUND: A weird infinite loop occurs where calling `getValue` calls
enumerable: enumerable !== false, // `setValue` which calls `Object.defineProperty` which somehow triggers
writable: writable !== false, // `getValue` again. Adding `valueSet` breaks this loop.
value, if (!valueSet) {
}); setValue(getNewValue());
} }
return value;
}
function setValue(newValue: T): void {
value = newValue;
valueSet = true;
Object.defineProperty(object, name, {...config, value: newValue});
}
Object.defineProperty(object, name, {
configurable: config.configurable,
enumerable: config.enumerable,
get: getValue,
set: setValue,
}); });
} }