destroyObject.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. * @function
  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(
  35. message,
  36. "This object was destroyed, i.e., destroy() was called."
  37. );
  38. function throwOnDestroyed() {
  39. //>>includeStart('debug', pragmas.debug);
  40. throw new DeveloperError(message);
  41. //>>includeEnd('debug');
  42. }
  43. for (const key in object) {
  44. if (typeof object[key] === "function") {
  45. object[key] = throwOnDestroyed;
  46. }
  47. }
  48. object.isDestroyed = returnTrue;
  49. return undefined;
  50. }
  51. export default destroyObject;