destroyObject.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import defaultValue from './defaultValue.js';
  2. import DeveloperError from './DeveloperError.js';
  3. function returnTrue() {
  4. return true;
  5. }
  6. /**
  7. * Destroys an object. Each of the object's functions, including functions in its prototype,
  8. * is replaced with a function that throws a {@link DeveloperError}, except for the object's
  9. * <code>isDestroyed</code> function, which is set to a function that returns <code>true</code>.
  10. * The object's properties are removed with <code>delete</code>.
  11. * <br /><br />
  12. * This function is used by objects that hold native resources, e.g., WebGL resources, which
  13. * need to be explicitly released. Client code calls an object's <code>destroy</code> function,
  14. * which then releases the native resource and calls <code>destroyObject</code> to put itself
  15. * in a destroyed state.
  16. *
  17. * @exports destroyObject
  18. *
  19. * @param {Object} object The object to destroy.
  20. * @param {String} [message] The message to include in the exception that is thrown if
  21. * a destroyed object's function is called.
  22. *
  23. *
  24. * @example
  25. * // How a texture would destroy itself.
  26. * this.destroy = function () {
  27. * _gl.deleteTexture(_texture);
  28. * return Cesium.destroyObject(this);
  29. * };
  30. *
  31. * @see DeveloperError
  32. */
  33. function destroyObject(object, message) {
  34. message = defaultValue(message, 'This object was destroyed, i.e., destroy() was called.');
  35. function throwOnDestroyed() {
  36. //>>includeStart('debug', pragmas.debug);
  37. throw new DeveloperError(message);
  38. //>>includeEnd('debug');
  39. }
  40. for ( var key in object) {
  41. if (typeof object[key] === 'function') {
  42. object[key] = throwOnDestroyed;
  43. }
  44. }
  45. object.isDestroyed = returnTrue;
  46. return undefined;
  47. }
  48. export default destroyObject;