RN: Implement `defineLazyObjectProperty`

Reviewed By: sahrens

Differential Revision: D3500318

fbshipit-source-id: 275c5b9a2b747174fb23b80896e2270eb2131e4b
This commit is contained in:
Tim Yung 2016-06-29 16:34:09 -07:00 committed by Facebook Github Bot 4
parent 8b57f078de
commit de6ab1c6f4
2 changed files with 65 additions and 24 deletions

View File

@ -91,37 +91,17 @@ function defineLazyProperty<T>(
name: string,
getNewValue: () => T
): void {
const defineLazyObjectProperty = require('defineLazyObjectProperty');
const descriptor = getPropertyDescriptor(object, name);
if (descriptor) {
const backupName = `original${name[0].toUpperCase()}${name.substr(1)}`;
Object.defineProperty(object, backupName, descriptor);
}
const config = {
configurable: true,
defineLazyObjectProperty(object, name, {
get: getNewValue,
enumerable: descriptor ? descriptor.enumerable !== false : true,
writable: descriptor ? descriptor.writable !== false : true,
};
let value;
let valueSet = false;
function getValue(): T {
// WORKAROUND: A weird infinite loop occurs where calling `getValue` calls
// `setValue` which calls `Object.defineProperty` which somehow triggers
// `getValue` again. Adding `valueSet` breaks this loop.
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,
});
}

View File

@ -0,0 +1,61 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule defineLazyObjectProperty
* @flow
*/
'use strict';
/**
* Defines a lazily evaluated property on the supplied `object`.
*/
function defineLazyObjectProperty<T>(
object: Object,
name: string,
descriptor: {
get: () => T,
enumerable?: boolean,
writable?: boolean,
},
): void {
const {get} = descriptor;
const enumerable = descriptor.enumerable !== false;
const writable = descriptor.writable !== false;
let value;
let valueSet = false;
function getValue(): T {
// WORKAROUND: A weird infinite loop occurs where calling `getValue` calls
// `setValue` which calls `Object.defineProperty` which somehow triggers
// `getValue` again. Adding `valueSet` breaks this loop.
if (!valueSet) {
setValue(get());
}
return value;
}
function setValue(newValue: T): void {
value = newValue;
valueSet = true;
Object.defineProperty(object, name, {
value: newValue,
configurable: true,
enumerable,
writable,
});
}
Object.defineProperty(object, name, {
get: getValue,
set: setValue,
configurable: true,
enumerable,
});
}
module.exports = defineLazyObjectProperty;