react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / vendor / immutable / Immutable.js
81158 views/**1* Copyright 2013-2014, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under the BSD-style license found in the5* LICENSE file in the root directory of this source tree. An additional grant6* of patent rights can be found in the PATENTS file in the same directory.7*8* @providesModule Immutable9*/1011var assign = require('Object.assign');12var invariant = require('invariant');13var isNode = require('isNode');14var keyOf = require('keyOf');1516var SECRET_KEY = keyOf({_DONT_EVER_TYPE_THIS_SECRET_KEY: null});1718/**19* `Immutable` provides a guarantee of immutability at developer time when20* strict mode is used. The extra computations required to enforce immutability21* are stripped out in production for performance reasons. `Immutable`22* guarantees to enforce immutability for enumerable, own properties. This23* allows easy wrapping of `Immutable` with the ability to store non-enumerable24* properties on the instance that only your static methods reason about. In25* order to achieve IE8 compatibility (which doesn't have the ability to define26* non-enumerable properties), modules that want to build their own reasoning27* of `Immutable`s and store computations can define their non-enumerable28* properties under the name `toString`, and in IE8 only define a standard29* property called `toString` which will mistakenly be considered not30* enumerable due to its name (but only in IE8). The only limitation is that no31* one can store their own `toString` property.32* https://developer.mozilla.org/en-US/docs/ECMAScript_DontEnum_attribute#JScript_DontEnum_Bug33*/34class Immutable {35/**36* An instance of `Immutable` appears to be a plain JavaScript object, except37* `instanceof Immutable` evaluates to `true`, and it is deeply frozen in38* development mode.39*40* @param {number} secret Ensures this isn't accidentally constructed outside41* of convenience constructors. If created outside of a convenience42* constructor, may not be frozen. Forbidding that use case for now until we43* have a better API.44*/45constructor(secret) {46invariant(47secret === Immutable[SECRET_KEY],48'Only certain classes should create instances of `Immutable`.' +49'You probably want something like ImmutableObject.create.'50);51}5253/**54* Helper method for classes that make use of `Immutable`.55* @param {Immutable} immutable Object to merge properties into.56* @param {array<object>} propertyObjects List of objects to merge into57* `destination`.58*/59static mergeAllPropertiesInto(destination, propertyObjects) {60var argLength = propertyObjects.length;61for (var i = 0; i < argLength; i++) {62assign(destination, propertyObjects[i]);63}64}656667/**68* Freezes the supplied object deeply. Other classes may implement their own69* version based on this.70*71* @param {*} object The object to freeze.72*/73static deepFreezeRootNode(object) {74if (isNode(object)) {75return; // Don't try to freeze DOM nodes.76}77Object.freeze(object); // First freeze the object.78for (var prop in object) {79if (object.hasOwnProperty(prop)) {80Immutable.recurseDeepFreeze(object[prop]);81}82}83Object.seal(object);84}8586/**87* Differs from `deepFreezeRootNode`, in that we first check if this is a88* necessary recursion. If the object is already an `Immutable`, then the89* recursion is unnecessary as it is already frozen. That check obviously90* wouldn't work for the root node version `deepFreezeRootNode`!91*/92static recurseDeepFreeze(object) {93if (isNode(object) || !Immutable.shouldRecurseFreeze(object)) {94return; // Don't try to freeze DOM nodes.95}96Object.freeze(object); // First freeze the object.97for (var prop in object) {98if (object.hasOwnProperty(prop)) {99Immutable.recurseDeepFreeze(object[prop]);100}101}102Object.seal(object);103}104105/**106* Checks if an object should be deep frozen. Instances of `Immutable` are107* assumed to have already been deep frozen, so we can have large `__DEV__`108* time savings by skipping freezing of them.109*110* @param {*} object The object to check.111* @return {boolean} Whether or not deep freeze is needed.112*/113static shouldRecurseFreeze(object) {114return (115typeof object === 'object' &&116!(object instanceof Immutable) &&117object !== null118);119}120}121122Immutable._DONT_EVER_TYPE_THIS_SECRET_KEY = Math.random();123124module.exports = Immutable;125126127