• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

excaliburjs / Excalibur / 14804036802

02 May 2025 09:58PM UTC coverage: 5.927% (-83.4%) from 89.28%
14804036802

Pull #3404

github

web-flow
Merge 5c103d7f8 into 0f2ccaeb2
Pull Request #3404: feat: added Graph module to Math

234 of 8383 branches covered (2.79%)

229 of 246 new or added lines in 1 file covered. (93.09%)

13145 existing lines in 208 files now uncovered.

934 of 15759 relevant lines covered (5.93%)

4.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

22.86
/src/engine/Util/Pool.ts
1
import { Logger } from '../Util/Log';
2
export class Pool<Type> {
3
  public totalAllocations = 0;
1✔
4
  public index = 0;
1✔
5
  public objects: Type[] = [];
1✔
6
  public disableWarnings = false;
1✔
7
  private _logger = Logger.getInstance();
1✔
8

9
  constructor(
10
    public builder: () => Type,
1✔
11
    public recycler?: (instance: Type) => Type,
1✔
12
    public maxObjects: number = 100
1!
13
  ) {}
14

15
  dispose() {
UNCOV
16
    this.objects.length = 0;
×
17
  }
18

19
  preallocate() {
UNCOV
20
    for (let i = 0; i < this.maxObjects; i++) {
×
UNCOV
21
      this.objects[i] = this.builder();
×
22
    }
23
  }
24

25
  /**
26
   * Use many instances out of the in the context and return all to the pool.
27
   *
28
   * By returning values out of the context they will be un-hooked from the pool and are free to be passed to consumers
29
   * @param context
30
   */
31
  using(context: (pool: Pool<Type>) => Type[] | void) {
UNCOV
32
    const result = context(this);
×
UNCOV
33
    if (result) {
×
UNCOV
34
      return this.done(...result);
×
35
    }
UNCOV
36
    return this.done();
×
37
  }
38

39
  /**
40
   * Use a single instance out of th pool and immediately return it to the pool
41
   * @param context
42
   */
43
  borrow(context: (object: Type) => void) {
UNCOV
44
    const object = this.get();
×
UNCOV
45
    context(object);
×
UNCOV
46
    this.index--;
×
47
  }
48

49
  /**
50
   * Retrieve a value from the pool, will allocate a new instance if necessary or recycle from the pool
51
   */
52
  get(): Type {
UNCOV
53
    if (this.index === this.maxObjects) {
×
UNCOV
54
      if (!this.disableWarnings) {
×
UNCOV
55
        this._logger.warn('Max pooled objects reached, possible memory leak? Doubling');
×
56
      }
UNCOV
57
      this.maxObjects = this.maxObjects * 2;
×
58
    }
59

UNCOV
60
    if (this.objects[this.index]) {
×
61
      // Pool has an available object already constructed
UNCOV
62
      if (this.recycler) {
×
UNCOV
63
        return this.recycler(this.objects[this.index++]);
×
64
      }
UNCOV
65
      return this.objects[this.index++];
×
66
    } else {
67
      // New allocation
UNCOV
68
      this.totalAllocations++;
×
UNCOV
69
      const object = (this.objects[this.index++] = this.builder());
×
UNCOV
70
      return object;
×
71
    }
72
  }
73

74
  /**
75
   * Signals we are done with the pool objects for now, Reclaims all objects in the pool.
76
   *
77
   * If a list of pooled objects is passed to done they are un-hooked from the pool and are free
78
   * to be passed to consumers
79
   * @param objects A list of object to separate from the pool
80
   */
81
  done(...objects: Type[]): Type[];
82
  done(): void;
83
  done(...objects: Type[]): Type[] | void {
84
    // All objects in pool now considered "free"
UNCOV
85
    this.index = 0;
×
UNCOV
86
    for (const object of objects) {
×
UNCOV
87
      const poolIndex = this.objects.indexOf(object);
×
88
      // Build a new object to take the pool place
UNCOV
89
      this.objects[poolIndex] = (this as any).builder();
×
UNCOV
90
      this.totalAllocations++;
×
91
    }
UNCOV
92
    return objects;
×
93
  }
94
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc