scheduler.jsView |
---|
12 | 12 | */ |
13 | 13 | constructor () { |
14 | 14 | this._waits = [] |
15 | 15 | this._running = new Set() |
16 | | - this.instances = new SortedMap(comparator) |
| 16 | + this.actors = new SortedMap(comparator) |
17 | 17 | } |
18 | 18 | |
19 | 19 | |
20 | 20 | * locks the scheduler from clearing waits untill the lock is resolved |
27 | 27 | const p = new Promise((resolve, reject) => { |
28 | 28 | r = resolve |
29 | 29 | }) |
30 | 30 | p.ticks = 0 |
31 | | - this.instances.set(id, p) |
| 31 | + this.actors.set(id, p) |
32 | 32 | this._running.add(id) |
33 | 33 | return r |
34 | 34 | } |
35 | 35 | |
36 | 36 | |
37 | | - * updates an instance with a new tick count |
38 | | - * @param {Object} instance - an actor instance |
| 37 | + * updates an actor with a new tick count |
| 38 | + * @param {Object} actor - an actor instance |
39 | 39 | */ |
40 | | - update (instance) { |
41 | | - this._update(instance) |
42 | | - this._running.add(instance.id.toString('hex')) |
| 40 | + update (actor) { |
| 41 | + this._update(actor) |
| 42 | + this._running.add(actor.id.toString('hex')) |
43 | 43 | this._checkWaits() |
44 | 44 | } |
45 | 45 | |
46 | | - _update (instance) { |
47 | | - this.instances.delete(instance.id.toString('hex')) |
48 | | - this.instances.set(instance.id.toString('hex'), instance) |
| 46 | + _update (actor) { |
| 47 | + this.actors.delete(actor.id.toString('hex')) |
| 48 | + this.actors.set(actor.id.toString('hex'), actor) |
49 | 49 | } |
50 | 50 | |
51 | 51 | |
52 | 52 | * returns an Actor instance |
53 | 53 | * @param {String} id |
54 | 54 | * @return {Object} |
55 | 55 | */ |
56 | | - getInstance (id) { |
| 56 | + getActor (id) { |
57 | 57 | id = id.toString('hex') |
58 | | - return this.instances.get(id) |
| 58 | + return this.actors.get(id) |
59 | 59 | } |
60 | 60 | |
61 | 61 | |
62 | | - * deletes an instance from the scheduler |
| 62 | + * deletes an actor from the scheduler |
63 | 63 | * @param {String} id - the containers id |
64 | 64 | */ |
65 | 65 | done (id) { |
66 | 66 | id = id.toString('hex') |
67 | 67 | this._running.delete(id) |
68 | | - this.instances.delete(id) |
| 68 | + this.actors.delete(id) |
69 | 69 | this._checkWaits() |
70 | 70 | } |
71 | 71 | |
72 | 72 | |
97 | 97 | * @return {integer} |
98 | 98 | */ |
99 | 99 | leastNumberOfTicks (exclude) { |
100 | 100 | let ticks = Infinity |
101 | | - for (const instance of this.instances) { |
102 | | - ticks = instance[1].ticks |
103 | | - if (instance[1].id !== exclude) { |
| 101 | + for (const actor of this.actors) { |
| 102 | + ticks = actor[1].ticks |
| 103 | + if (actor[1].id !== exclude) { |
104 | 104 | return ticks |
105 | 105 | } |
106 | 106 | } |
107 | 107 | |
110 | 110 | |
111 | 111 | |
112 | 112 | _checkWaits () { |
113 | 113 | |
114 | | - if (!this.instances.size) { |
| 114 | + if (!this.actors.size) { |
115 | 115 | |
116 | 116 | this._waits.forEach(wait => wait.resolve()) |
117 | 117 | this._waits = [] |
118 | 118 | return |
119 | 119 | } |
120 | 120 | |
121 | | - |
| 121 | + |
122 | 122 | while (this._waits[0]) { |
123 | 123 | const wait = this._waits[0] |
124 | 124 | const least = this.leastNumberOfTicks(wait.id) |
125 | 125 | if (wait.ticks <= least) { |
134 | 134 | |
135 | 135 | |
136 | 136 | if (!this._running.size && this._waits.length) { |
137 | 137 | const oldest = this._waits[0].ticks |
138 | | - for (let instance of this.instances) { |
139 | | - instance = instance[1] |
140 | | - if (instance.ticks > oldest) { |
| 138 | + for (let actor of this.actors) { |
| 139 | + actor = actor[1] |
| 140 | + if (actor.ticks > oldest) { |
141 | 141 | break |
142 | 142 | } |
143 | | - instance.ticks = oldest |
144 | | - this._update(instance) |
| 143 | + actor.ticks = oldest |
| 144 | + this._update(actor) |
145 | 145 | } |
146 | 146 | return this._checkWaits() |
147 | 147 | } |
148 | 148 | } |