-
Notifications
You must be signed in to change notification settings - Fork 8
/
World.js
40 lines (32 loc) · 918 Bytes
/
World.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'use strict';
const Playground = require('./Playground');
const persistence = require('./persistence');
function World () {
this._playgrounds = new Map();
}
World.prototype.tour = function () {
return Array.from(this._playgrounds.keys());
};
World.prototype.contains = function (id) {
return this._playgrounds.has(id);
};
World.prototype.getOrCreatePlayground = function (id) {
let playground;
if (this._playgrounds.has(id)) {
playground = this._playgrounds.get(id);
playground.refCount++;
} else {
playground = new Playground(id);
this._playgrounds.set(id, playground);
playground.refCount = 1;
persistence.setup(playground);
}
return playground;
};
World.prototype.releasePlayground = function (playground) {
playground.refCount--;
if (playground.refCount <= 0 && playground.isEmpty()) {
this._playgrounds.delete(playground.id);
}
};
module.exports = World;