-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdata-application.js
More file actions
85 lines (83 loc) · 2.85 KB
/
data-application.js
File metadata and controls
85 lines (83 loc) · 2.85 KB
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
77
78
79
80
81
82
83
84
// MOST Web Framework 2.0 Codename Blueshift BSD-3-Clause license Copyright (c) 2017-2022, THEMOST LP All rights reserved
var {Args, PathUtils, SequentialEventEmitter} = require('@themost/common');
var {DataConfiguration} = require('./data-configuration');
var {DefaultDataContext} = require('./data-context');
var isObjectLike = require('lodash/isObjectLike');
/**
* @class
* @param {string} cwdOrConfig
*/
class DataApplication extends SequentialEventEmitter {
constructor(cwdOrConfig) {
super();
Object.defineProperty(this, '_services', {
configurable: true,
enumerable: false,
writable: false,
value: {}
});
if (isObjectLike(cwdOrConfig)) {
Object.defineProperty(this, 'configuration', {
configurable: true,
enumerable: false,
writable: false,
value: new DataConfiguration(cwdOrConfig)
});
return;
}
Object.defineProperty(this, 'configuration', {
configurable: true,
enumerable: false,
writable: false,
value: new DataConfiguration(PathUtils.join(cwdOrConfig, 'config'))
});
}
hasService(serviceCtor) {
if (serviceCtor == null) {
return false;
}
Args.check(typeof serviceCtor === 'function', new Error('Strategy constructor is invalid.'));
return Object.prototype.hasOwnProperty.call(this._services, serviceCtor.name);
}
getService(serviceCtor) {
if (serviceCtor == null) {
return false;
}
Args.check(typeof serviceCtor === 'function', new Error('Strategy constructor is invalid.'));
if (Object.prototype.hasOwnProperty.call(this._services, serviceCtor.name) === false) {
return;
}
return this._services[serviceCtor.name];
}
useStrategy(serviceCtor, strategyCtor) {
if (strategyCtor == null) {
return false;
}
Args.check(typeof serviceCtor === 'function', new Error('Service constructor is invalid.'));
Args.check(typeof strategyCtor === 'function', new Error('Strategy constructor is invalid.'));
Object.defineProperty(this._services, serviceCtor.name, {
configurable: true,
enumerable: true,
writable: true,
value: new strategyCtor(this)
});
return this;
}
useService(serviceCtor) {
return this.useStrategy(serviceCtor, serviceCtor);
}
getConfiguration() {
return this.configuration;
}
createContext() {
const context = new DefaultDataContext();
// override configuration
context.getConfiguration = () => {
return this.configuration;
};
return context;
}
}
module.exports = {
DataApplication
};