2015-01-30 01:10:49 +00:00
|
|
|
/**
|
2015-03-23 22:07:33 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-01-30 01:10:49 +00:00
|
|
|
*
|
2018-05-11 02:06:46 +00:00
|
|
|
* @format
|
2015-03-24 16:26:16 +00:00
|
|
|
* @flow
|
2015-01-30 01:10:49 +00:00
|
|
|
*/
|
|
|
|
|
2015-03-24 16:26:16 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
/**
|
|
|
|
* If your application is accepting different values for the same field over
|
|
|
|
* time and is doing a diff on them, you can either (1) create a copy or
|
|
|
|
* (2) ensure that those values are not mutated behind two passes.
|
|
|
|
* This function helps you with (2) by freezing the object and throwing if
|
|
|
|
* the user subsequently modifies the value.
|
|
|
|
*
|
|
|
|
* There are two caveats with this function:
|
|
|
|
* - If the call site is not in strict mode, it will only throw when
|
|
|
|
* mutating existing fields, adding a new one
|
|
|
|
* will unfortunately fail silently :(
|
|
|
|
* - If the object is already frozen or sealed, it will not continue the
|
|
|
|
* deep traversal and will leave leaf nodes unfrozen.
|
|
|
|
*
|
|
|
|
* Freezing the object and adding the throw mechanism is expensive and will
|
|
|
|
* only be used in DEV.
|
|
|
|
*/
|
2018-02-13 22:04:49 +00:00
|
|
|
function deepFreezeAndThrowOnMutationInDev<T: Object>(object: T): T {
|
2015-01-30 01:10:49 +00:00
|
|
|
if (__DEV__) {
|
2018-05-11 02:06:46 +00:00
|
|
|
if (
|
|
|
|
typeof object !== 'object' ||
|
|
|
|
object === null ||
|
|
|
|
Object.isFrozen(object) ||
|
|
|
|
Object.isSealed(object)
|
|
|
|
) {
|
2018-02-13 22:04:49 +00:00
|
|
|
return object;
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const keys = Object.keys(object);
|
2018-06-07 09:26:17 +00:00
|
|
|
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
2017-01-11 16:44:58 +00:00
|
|
|
|
|
|
|
for (var i = 0; i < keys.length; i++) {
|
|
|
|
var key = keys[i];
|
2018-06-07 09:26:17 +00:00
|
|
|
if (hasOwnProperty.call(object, key)) {
|
|
|
|
Object.defineProperty(object, key, {
|
|
|
|
get: identity.bind(null, object[key]),
|
|
|
|
});
|
|
|
|
Object.defineProperty(object, key, {
|
|
|
|
set: throwOnImmutableMutation.bind(null, key),
|
|
|
|
});
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-12 22:53:36 +00:00
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
Object.freeze(object);
|
|
|
|
Object.seal(object);
|
2016-02-12 22:53:36 +00:00
|
|
|
|
2017-01-11 16:44:58 +00:00
|
|
|
for (var i = 0; i < keys.length; i++) {
|
|
|
|
var key = keys[i];
|
2018-06-07 09:26:17 +00:00
|
|
|
if (hasOwnProperty.call(object, key)) {
|
2016-02-12 22:53:36 +00:00
|
|
|
deepFreezeAndThrowOnMutationInDev(object[key]);
|
|
|
|
}
|
|
|
|
}
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
2018-02-13 22:04:49 +00:00
|
|
|
return object;
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function throwOnImmutableMutation(key, value) {
|
|
|
|
throw Error(
|
2018-05-11 02:06:46 +00:00
|
|
|
'You attempted to set the key `' +
|
|
|
|
key +
|
|
|
|
'` with the value `' +
|
|
|
|
JSON.stringify(value) +
|
|
|
|
'` on an object that is meant to be immutable ' +
|
|
|
|
'and has been frozen.',
|
2015-01-30 01:10:49 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function identity(value) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = deepFreezeAndThrowOnMutationInDev;
|