-
Notifications
You must be signed in to change notification settings - Fork 4
/
rtmese.manager.js
76 lines (62 loc) · 1.38 KB
/
rtmese.manager.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use strict';
var config = require('./config');
var games = {};
module.exports.schedule = function (
name, game, delay, execAction,
waitCallback, execCallback, stopCallback,
callback, fail
) {
var stop = function () {
stopCallback(game);
if (games[name] !== game) {
throw Error('broken runtime');
}
delete games[name];
delete game.stop;
delete game.delay;
};
var exec = function () {
if (game.stop) {
stop();
} else if (game.delay > 0) {
waitCallback(game);
game.delay -= 1;
setTimeout(exec, config.rtmeseInterval);
} else if (execAction(game)) {
execCallback(game);
setTimeout(exec, config.rtmeseInterval);
} else {
stop();
}
};
if (games[name] === undefined) {
games[name] = game;
game.stop = false;
game.delay = delay;
callback();
exec();
} else {
fail();
}
};
module.exports.stop = function (
name,
callback, fail
) {
if (games[name] === undefined) {
fail();
} else {
games[name].stop = true;
callback();
}
};
module.exports.get = function (
name,
callback, fail
) {
if (games[name] === undefined) {
fail();
} else {
callback(games[name]);
}
};