-
Notifications
You must be signed in to change notification settings - Fork 8
/
Playground.js
54 lines (43 loc) · 1.41 KB
/
Playground.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use strict';
const util = require('util');
const EventEmitter = require('events');
const CodeObject = require('./CodeObject');
function Playground (id) {
EventEmitter.call(this);
this.id = id;
this._codeObjects = new Map();
}
util.inherits(Playground, EventEmitter);
Playground.prototype.getOrCreateCodeObject = function (codeObjectId) {
if (this._codeObjects.has(codeObjectId)) {
return this._codeObjects.get(codeObjectId);
}
var codeObject = new CodeObject(codeObjectId,
(co) => this.emit('codeObjectUpdated', co));
this._codeObjects.set(codeObjectId, codeObject);
return codeObject;
};
Playground.prototype.deleteCodeObject = function (codeObjectId) {
if (!this.contains(codeObjectId)) return;
const codeObject = this._codeObjects.get(codeObjectId);
this._codeObjects.delete(codeObjectId);
this.emit('codeObjectDeleted', codeObject);
return codeObject;
};
Playground.prototype.isEmpty = function () {
return this._codeObjects.size === 0;
};
Playground.prototype.contains = function (id) {
return this._codeObjects.has(id);
};
Playground.prototype.getData = function () {
return Array.from(this._codeObjects.values())
.map((codeObject) => codeObject.getData());
};
Playground.prototype.getDataFor = function (codeObjectId) {
var codeObject =
this._codeObjects.get(codeObjectId) ||
new CodeObject(codeObjectId);
return codeObject.getData();
};
module.exports = Playground;