shapeCollection_pool.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import {
  2. createSizedArray,
  3. } from '../helpers/arrays';
  4. import shapePool from './shape_pool';
  5. import pooling from './pooling';
  6. import ShapeCollection from '../shapes/ShapeCollection';
  7. const shapeCollectionPool = (function () {
  8. var ob = {
  9. newShapeCollection: newShapeCollection,
  10. release: release,
  11. };
  12. var _length = 0;
  13. var _maxLength = 4;
  14. var pool = createSizedArray(_maxLength);
  15. function newShapeCollection() {
  16. var shapeCollection;
  17. if (_length) {
  18. _length -= 1;
  19. shapeCollection = pool[_length];
  20. } else {
  21. shapeCollection = new ShapeCollection();
  22. }
  23. return shapeCollection;
  24. }
  25. function release(shapeCollection) {
  26. var i;
  27. var len = shapeCollection._length;
  28. for (i = 0; i < len; i += 1) {
  29. shapePool.release(shapeCollection.shapes[i]);
  30. }
  31. shapeCollection._length = 0;
  32. if (_length === _maxLength) {
  33. pool = pooling.double(pool);
  34. _maxLength *= 2;
  35. }
  36. pool[_length] = shapeCollection;
  37. _length += 1;
  38. }
  39. return ob;
  40. }());
  41. export default shapeCollectionPool;