PooledClass.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. * @flow
  9. */
  10. 'use strict';
  11. const invariant = require('invariant');
  12. /**
  13. * Static poolers. Several custom versions for each potential number of
  14. * arguments. A completely generic pooler is easy to implement, but would
  15. * require accessing the `arguments` object. In each of these, `this` refers to
  16. * the Class itself, not an instance. If any others are needed, simply add them
  17. * here, or in their own files.
  18. */
  19. const oneArgumentPooler = function(copyFieldsFrom) {
  20. const Klass = this;
  21. if (Klass.instancePool.length) {
  22. const instance = Klass.instancePool.pop();
  23. Klass.call(instance, copyFieldsFrom);
  24. return instance;
  25. } else {
  26. return new Klass(copyFieldsFrom);
  27. }
  28. };
  29. const twoArgumentPooler = function(a1, a2) {
  30. const Klass = this;
  31. if (Klass.instancePool.length) {
  32. const instance = Klass.instancePool.pop();
  33. Klass.call(instance, a1, a2);
  34. return instance;
  35. } else {
  36. return new Klass(a1, a2);
  37. }
  38. };
  39. const threeArgumentPooler = function(a1, a2, a3) {
  40. const Klass = this;
  41. if (Klass.instancePool.length) {
  42. const instance = Klass.instancePool.pop();
  43. Klass.call(instance, a1, a2, a3);
  44. return instance;
  45. } else {
  46. return new Klass(a1, a2, a3);
  47. }
  48. };
  49. const fourArgumentPooler = function(a1, a2, a3, a4) {
  50. const Klass = this;
  51. if (Klass.instancePool.length) {
  52. const instance = Klass.instancePool.pop();
  53. Klass.call(instance, a1, a2, a3, a4);
  54. return instance;
  55. } else {
  56. return new Klass(a1, a2, a3, a4);
  57. }
  58. };
  59. const standardReleaser = function(instance) {
  60. const Klass = this;
  61. invariant(
  62. instance instanceof Klass,
  63. 'Trying to release an instance into a pool of a different type.',
  64. );
  65. instance.destructor();
  66. if (Klass.instancePool.length < Klass.poolSize) {
  67. Klass.instancePool.push(instance);
  68. }
  69. };
  70. const DEFAULT_POOL_SIZE = 10;
  71. const DEFAULT_POOLER = oneArgumentPooler;
  72. type Pooler = any;
  73. /**
  74. * Augments `CopyConstructor` to be a poolable class, augmenting only the class
  75. * itself (statically) not adding any prototypical fields. Any CopyConstructor
  76. * you give this may have a `poolSize` property, and will look for a
  77. * prototypical `destructor` on instances.
  78. *
  79. * @param {Function} CopyConstructor Constructor that can be used to reset.
  80. * @param {Function} pooler Customizable pooler.
  81. */
  82. const addPoolingTo = function<T>(
  83. CopyConstructor: Class<T>,
  84. pooler: Pooler,
  85. ): Class<T> & {
  86. getPooled(
  87. ...args: $ReadOnlyArray<mixed>
  88. ): /* arguments of the constructor */ T,
  89. release(instance: mixed): void,
  90. ...
  91. } {
  92. // Casting as any so that flow ignores the actual implementation and trusts
  93. // it to match the type we declared
  94. const NewKlass = (CopyConstructor: any);
  95. NewKlass.instancePool = [];
  96. NewKlass.getPooled = pooler || DEFAULT_POOLER;
  97. if (!NewKlass.poolSize) {
  98. NewKlass.poolSize = DEFAULT_POOL_SIZE;
  99. }
  100. NewKlass.release = standardReleaser;
  101. return NewKlass;
  102. };
  103. const PooledClass = {
  104. addPoolingTo: addPoolingTo,
  105. oneArgumentPooler: (oneArgumentPooler: Pooler),
  106. twoArgumentPooler: (twoArgumentPooler: Pooler),
  107. threeArgumentPooler: (threeArgumentPooler: Pooler),
  108. fourArgumentPooler: (fourArgumentPooler: Pooler),
  109. };
  110. module.exports = PooledClass;