-
Notifications
You must be signed in to change notification settings - Fork 0
/
Feature.js
67 lines (65 loc) · 2.27 KB
/
Feature.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
const { NotImplementedError } = require('./Error');
const { FEATURE_PREFIX_SEPARATOR, FEATURE_TIME_OUT } = require("./constants");
class Feature {
/**
* Abstract class
*
* Every feature that will be used on Raccoon must be extends to the Feature class
* @constructor
* @param {any} owner - A unique identity that distinguishes each feature from a different owner
*/
constructor(owner) {
if (this.constructor === Feature) {
throw new NotImplementedError("Feature class");
}
this.owner = owner;
this.name = this.constructor.name; // feature name
this.prefix = `${this.name}${FEATURE_PREFIX_SEPARATOR}${this.owner}`; // prefix for callback data on keyboard
this.createdAt = new Date().getTime(); // to calculate how long a method has been executed.
}
/**
* Abstract Method
* Methods must be implemented in classes that extend to the Feature class.
*
* The start method is the method used to initialize the first response of a feature instance.
* Returns the response template.
*/
start() {
throw new NotImplementedError(`start method`);
}
/**
* Executes the methods available on the feature Object by using the method name
*
* @param {string} method
* @param {string} params
* @param {any} context
*
*/
async run(method, params, context) {
const func = this[method];
if (func === undefined) {
throw new Error(`Feature ${this.name} doesn't have method '${method}'`);
}
this.createdAt = new Date().getTime(); // updating the time the method is called
return func.call(this, params, context);
}
/** calculate how long a method is running */
get durration() {
const current = new Date().getTime();
return (current - this.createdAt) / 1000;
}
/** Returns true if the method has been running longer than session time */
isSessionExpired() {
return this.durration >= FEATURE_TIME_OUT;
}
/** Tells 'Raccoons' to immediately remove expired methods from the activity state */
cleanupActivity() {
return {
type: "$cleanup",
owner: this.owner
};
}
}
module.exports = {
Feature
}