dyo-tools 0.1.0-rc2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.c8rc.json +4 -0
- package/.eslintignore +2 -0
- package/.eslintrc.json +47 -0
- package/LICENSE +21 -0
- package/Makefile +34 -0
- package/README.md +0 -7
- package/babel.config.js +1 -0
- package/cucumber-report.html +48 -0
- package/cucumber.js +9 -0
- package/dist/constants.d.ts +6 -0
- package/dist/constants.js +63 -0
- package/dist/constants.js.map +1 -0
- package/dist/core/DTBunch.d.ts +11 -15
- package/dist/core/DTBunch.js +27 -106
- package/dist/core/DTBunch.js.map +1 -1
- package/dist/core/DTComponent.d.ts +13 -5
- package/dist/core/DTComponent.js +39 -1
- package/dist/core/DTComponent.js.map +1 -1
- package/dist/core/DTComponentPhysical.d.ts +10 -0
- package/dist/core/DTComponentPhysical.js +16 -0
- package/dist/core/DTComponentPhysical.js.map +1 -0
- package/dist/core/DTComponentWithMeta.d.ts +2 -2
- package/dist/core/DTComponentWithMeta.js.map +1 -1
- package/dist/core/DTElement.d.ts +2 -7
- package/dist/core/DTElement.js +3 -12
- package/dist/core/DTElement.js.map +1 -1
- package/dist/core/DTManager.d.ts +31 -0
- package/dist/core/DTManager.js +180 -0
- package/dist/core/DTManager.js.map +1 -0
- package/dist/core/DTPlayer.js +1 -1
- package/dist/core/DTPlayer.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/libs/DYOFinder.d.ts +10 -0
- package/dist/libs/DYOFinder.js +96 -0
- package/dist/libs/DYOFinder.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1321
- package/dist/types/index.d.ts +64 -24
- package/dist/types/index.js.map +1 -1
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +29 -0
- package/docs/assets/main.js +58 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1367 -0
- package/docs/index.html +46 -0
- package/e2e/0.2.0/epic1.feature +29 -0
- package/e2e/0.2.0/epic2.feature +22 -0
- package/e2e/0.2.0/epic3.feature +25 -0
- package/e2e/0.2.0/resources/dominion.js +195 -0
- package/e2e/0.2.0/resources/utils.js +27 -0
- package/e2e/0.2.0/support/steps.js +108 -0
- package/e2e/future/epic4.feature +39 -0
- package/e2e/future/resources/dominion.js +238 -0
- package/e2e/future/resources/utils.js +27 -0
- package/jest.config.js +6 -0
- package/package.json +33 -23
- package/src/constants.ts +85 -0
- package/src/core/DTBunch.ts +461 -0
- package/src/core/DTComponent.ts +225 -0
- package/src/core/DTComponentPhysical.ts +39 -0
- package/src/core/DTComponentWithMeta.ts +65 -0
- package/src/core/DTElement.ts +69 -0
- package/src/core/DTError.ts +78 -0
- package/src/core/DTManager.ts +446 -0
- package/src/core/DTPlayer.ts +57 -0
- package/src/index.ts +9 -0
- package/src/libs/DYOFinder.ts +175 -0
- package/src/types/index.ts +162 -0
- package/test/core/DTBunch.double.ts +253 -0
- package/test/core/DTBunch.spec.ts +895 -0
- package/test/core/DTComponent.double.ts +164 -0
- package/test/core/DTComponent.spec.ts +295 -0
- package/test/core/DTComponentPhysical.double.ts +76 -0
- package/test/core/DTComponentPhysical.spec.ts +64 -0
- package/test/core/DTComponentWithMeta.double.ts +115 -0
- package/test/core/DTComponentWithMeta.spec.ts +124 -0
- package/test/core/DTElement.double.ts +147 -0
- package/test/core/DTElement.spec.ts +102 -0
- package/test/core/DTError.double.ts +92 -0
- package/test/core/DTError.spec.ts +89 -0
- package/test/core/DTManager.double.ts +192 -0
- package/test/core/DTManager.spec.ts +902 -0
- package/test/core/DTPlayer.double.ts +64 -0
- package/test/core/DTPlayer.spec.ts +80 -0
- package/test/core/copy.spec.ts +227 -0
- package/test/libs/DYOFinder.double.ts +152 -0
- package/test/libs/DYOFinder.spec.ts +194 -0
- package/tsconfig.dev.json +22 -0
- package/tsconfig.json +21 -0
- package/dist/utils/filters.d.ts +0 -6
- package/dist/utils/filters.js +0 -39
- package/dist/utils/filters.js.map +0 -1
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { DTAcceptedMetaData, DTComponentOptions } from '../types';
|
|
2
|
+
import DYOToolsComponentWithMeta from './DTComponentWithMeta';
|
|
3
|
+
import DYOToolsPlayer from './DTPlayer';
|
|
4
|
+
|
|
5
|
+
export default abstract class DYOToolsComponentPhysical<
|
|
6
|
+
IComponentMeta extends DTAcceptedMetaData,
|
|
7
|
+
IComponentOptions extends DTComponentOptions = DTComponentOptions,
|
|
8
|
+
> extends DYOToolsComponentWithMeta<IComponentMeta, IComponentOptions> {
|
|
9
|
+
/**
|
|
10
|
+
* Owner DTPlayer instance if defined
|
|
11
|
+
*/
|
|
12
|
+
protected _owner?: DYOToolsPlayer<DTAcceptedMetaData>;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Getter for _owner property.
|
|
16
|
+
*/
|
|
17
|
+
getOwner(): DYOToolsPlayer<DTAcceptedMetaData> {
|
|
18
|
+
return this._owner;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Setter for _owner property.
|
|
23
|
+
*/
|
|
24
|
+
setOwner(value: DYOToolsPlayer<DTAcceptedMetaData>): void {
|
|
25
|
+
this._owner = value;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Remove the current owner of element.
|
|
30
|
+
*/
|
|
31
|
+
removeOwner(): void {
|
|
32
|
+
this._owner = undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Abstract method for copying the Component and returning it.
|
|
37
|
+
*/
|
|
38
|
+
abstract copy(): DYOToolsComponentPhysical<IComponentMeta, IComponentOptions>;
|
|
39
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import DYOToolsComponent from './DTComponent';
|
|
2
|
+
import { DTAcceptedMetaData, DTComponentOptions } from '../types';
|
|
3
|
+
|
|
4
|
+
export default abstract class DYOToolsComponentWithMeta<
|
|
5
|
+
IComponentMeta extends DTAcceptedMetaData,
|
|
6
|
+
IComponentOptions extends DTComponentOptions = DTComponentOptions,
|
|
7
|
+
> extends DYOToolsComponent<IComponentOptions> {
|
|
8
|
+
/**
|
|
9
|
+
* Component meta data.
|
|
10
|
+
* Defined by generic type IComponentMeta.
|
|
11
|
+
* @default {}
|
|
12
|
+
*/
|
|
13
|
+
protected _meta : Partial<IComponentMeta> = {} as Partial<IComponentMeta>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Getter for one meta by key.
|
|
17
|
+
*
|
|
18
|
+
* @param metaKey Key name of one meta data.
|
|
19
|
+
*
|
|
20
|
+
* @returns Associated meta **metaKey** value or undefined if not found.
|
|
21
|
+
*/
|
|
22
|
+
getMeta<K extends keyof IComponentMeta>(metaKey : K) : IComponentMeta[K] | undefined {
|
|
23
|
+
return this._meta && this._meta[metaKey];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Setter for one meta by key.
|
|
28
|
+
*
|
|
29
|
+
* @param metaKey Key name of the meta data to update.
|
|
30
|
+
* @param metaValue New value to set into the meta data.
|
|
31
|
+
*/
|
|
32
|
+
setMeta<K extends keyof IComponentMeta>(metaKey : K, metaValue : IComponentMeta[K]) : void {
|
|
33
|
+
this._meta[metaKey] = metaValue;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Returns multiple defined keys values of meta data.
|
|
38
|
+
*
|
|
39
|
+
* @param metaKeys Array of keys to filter for meta data. If not provided or empty, returns all keys.
|
|
40
|
+
*
|
|
41
|
+
* @returns Meta data object with **metaKeys** provided keys only.
|
|
42
|
+
*/
|
|
43
|
+
getManyMeta(metaKeys : Array<keyof IComponentMeta> = []) : Partial<IComponentMeta> {
|
|
44
|
+
const arrayMeta: Partial<IComponentMeta> = {} as Partial<IComponentMeta>;
|
|
45
|
+
if (!metaKeys.length) {
|
|
46
|
+
return this._meta;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
metaKeys.forEach((key) => {
|
|
50
|
+
if (this._meta && this._meta[key]) {
|
|
51
|
+
arrayMeta[key] = this._meta[key];
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
return arrayMeta;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Set multiple meta data.
|
|
59
|
+
*
|
|
60
|
+
* @param metaValues Object of meta data to set, according to the meta data property type.
|
|
61
|
+
*/
|
|
62
|
+
setManyMeta(metaValues : Partial<IComponentMeta>) : void {
|
|
63
|
+
this._meta = { ...this._meta, ...metaValues };
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { DTAcceptedMetaData, DTElementToObject } from '../types';
|
|
2
|
+
import DYOToolsComponentPhysical from './DTComponentPhysical';
|
|
3
|
+
|
|
4
|
+
export default class DYOToolsElement<
|
|
5
|
+
IComponentMeta extends DTAcceptedMetaData,
|
|
6
|
+
> extends DYOToolsComponentPhysical<IComponentMeta> {
|
|
7
|
+
/**
|
|
8
|
+
* Defining component type to "element".
|
|
9
|
+
*/
|
|
10
|
+
protected _componentType = 'element';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Create and return a new DTElement instance by applying from current instance :
|
|
14
|
+
* - Copy _key property
|
|
15
|
+
* - Copy _meta property
|
|
16
|
+
*
|
|
17
|
+
* @returns New DTElement instance copied.
|
|
18
|
+
*/
|
|
19
|
+
copy(): DYOToolsElement<IComponentMeta> {
|
|
20
|
+
const copyElement = new DYOToolsElement<IComponentMeta>(this._key, this._options);
|
|
21
|
+
copyElement.setManyMeta({ ...this.getManyMeta() });
|
|
22
|
+
|
|
23
|
+
return copyElement;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Return JSON Object representation of the Element instance.
|
|
28
|
+
*
|
|
29
|
+
* JSON Object returned has the following structure :
|
|
30
|
+
* * **id** : _id property of the Element.
|
|
31
|
+
* * **key** : _key property of the Element.
|
|
32
|
+
* * **type** : _componentType property of the Element.
|
|
33
|
+
* * **owner** : String representation of the current _owner property of the Element (only if defined).
|
|
34
|
+
* * **meta** : JSON Object of all current metadata in _meta property of the Element (only if not empty).
|
|
35
|
+
*
|
|
36
|
+
* @returns JSON Object representation of the Element.
|
|
37
|
+
*/
|
|
38
|
+
toObject(): DTElementToObject<IComponentMeta> {
|
|
39
|
+
const objectElement: DTElementToObject<IComponentMeta> = {
|
|
40
|
+
id: this._id,
|
|
41
|
+
key: this._key,
|
|
42
|
+
type: this._componentType,
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
if (this._owner) {
|
|
46
|
+
objectElement.owner = this._owner.toString();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (this._meta && Object.keys(this._meta).length > 0) {
|
|
50
|
+
objectElement.meta = { ...this.getManyMeta() };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return objectElement;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Return String representation of the Element instance.
|
|
58
|
+
*
|
|
59
|
+
* @returns String representation of the Element.
|
|
60
|
+
*/
|
|
61
|
+
toString(): string {
|
|
62
|
+
let ownerKey = '';
|
|
63
|
+
if (this._owner) {
|
|
64
|
+
ownerKey = ` - Owner: ${this._owner.getKey()}`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return `Component ${this._key} - Type: Element${ownerKey}`;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import DYOToolsComponent from './DTComponent';
|
|
2
|
+
|
|
3
|
+
export default class DYOToolsError extends Error {
|
|
4
|
+
/**
|
|
5
|
+
* Error code.
|
|
6
|
+
*
|
|
7
|
+
* Available error codes are :
|
|
8
|
+
* * **id_conflict** : _id property conflict between two DTComponent in the same context.
|
|
9
|
+
* * **key_conflict** : _key property conflict between two DTComponent in the same context.
|
|
10
|
+
*/
|
|
11
|
+
protected code: string;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Error trigger date.
|
|
15
|
+
*/
|
|
16
|
+
protected timestamp: Date;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* DTComponent which trigger the error during its current execution process.
|
|
20
|
+
*/
|
|
21
|
+
protected initiator?: DYOToolsComponent;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* DTComponent which is directly involved in the error trigger.
|
|
25
|
+
*/
|
|
26
|
+
protected convicted?: DYOToolsComponent;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Set all property for a new DTError.
|
|
30
|
+
*
|
|
31
|
+
* @param code
|
|
32
|
+
* @param message
|
|
33
|
+
* @param initiator
|
|
34
|
+
* @param convicted
|
|
35
|
+
*/
|
|
36
|
+
constructor(code: string, message: string, initiator?: DYOToolsComponent, convicted?: DYOToolsComponent) {
|
|
37
|
+
super(message);
|
|
38
|
+
this.code = code;
|
|
39
|
+
this.timestamp = new Date();
|
|
40
|
+
this.initiator = initiator;
|
|
41
|
+
this.convicted = convicted;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Getter for code property.
|
|
46
|
+
*/
|
|
47
|
+
getCode(): string {
|
|
48
|
+
return this.code;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Getter for message property (inherited from Error).
|
|
53
|
+
*/
|
|
54
|
+
getMessage(): string {
|
|
55
|
+
return this.message;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Getter for timestamp property.
|
|
60
|
+
*/
|
|
61
|
+
getTimestamp(): Date {
|
|
62
|
+
return this.timestamp;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Getter for initiator property.
|
|
67
|
+
*/
|
|
68
|
+
getInitiator(): DYOToolsComponent {
|
|
69
|
+
return this.initiator;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Getter for convicted property.
|
|
74
|
+
*/
|
|
75
|
+
getConvicted(): DYOToolsComponent {
|
|
76
|
+
return this.convicted;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
import DYOToolsComponent from './DTComponent';
|
|
2
|
+
import DYOToolsBunch from './DTBunch';
|
|
3
|
+
import DYOToolsError from './DTError';
|
|
4
|
+
import {
|
|
5
|
+
DTAcceptedMetaData,
|
|
6
|
+
DTComponentOptions,
|
|
7
|
+
DTManagerFilters,
|
|
8
|
+
DTManagerItemsType,
|
|
9
|
+
DTManagerItemType,
|
|
10
|
+
DTManagerOptions,
|
|
11
|
+
DTManagerToObject,
|
|
12
|
+
DYOFinderConfiguration,
|
|
13
|
+
} from '../types';
|
|
14
|
+
import DYOFinder from '../libs/DYOFinder';
|
|
15
|
+
import { componentManagerDefaultFinderConfiguration, managerDefaultOptions as defaultOptions } from '../constants';
|
|
16
|
+
import DYOToolsElement from './DTElement';
|
|
17
|
+
|
|
18
|
+
export default class DYOToolsManager<
|
|
19
|
+
IBunchItem extends DYOToolsElement<DTAcceptedMetaData>,
|
|
20
|
+
> extends DYOToolsComponent<DTManagerOptions> {
|
|
21
|
+
/**
|
|
22
|
+
* Defining component type to "manager".
|
|
23
|
+
*/
|
|
24
|
+
protected _componentType = 'manager';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* DTBunch instances managed by the Manager.
|
|
28
|
+
*
|
|
29
|
+
* This property is an object with an entry for each bunch :
|
|
30
|
+
* * The key is the bunch _id.
|
|
31
|
+
* * The value is an object with :
|
|
32
|
+
* * * *scope* : the current affected **scope** of the bunch.
|
|
33
|
+
* * * *item* : the DTBunch instance.
|
|
34
|
+
*/
|
|
35
|
+
protected _items: DTManagerItemsType<IBunchItem>;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Valid scopes list for bunches.
|
|
39
|
+
*
|
|
40
|
+
* Each bunch is affected to one scope, in order to facilitate filtering and grouping of bunches.
|
|
41
|
+
* A DTManager instance comes with two initial scopes : 'default' and 'virtual'.
|
|
42
|
+
*/
|
|
43
|
+
protected _scopes: string[];
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Current Library instance for the Manager.
|
|
47
|
+
*
|
|
48
|
+
* The Library is a special virtual bunch which contains all elements added in bunches.
|
|
49
|
+
* The purpose is the guarantee of id uniqueness when transferring elements between bunches.
|
|
50
|
+
* It also facilitates searching elements in all Manager bunches.
|
|
51
|
+
*/
|
|
52
|
+
protected _library: DYOToolsBunch<IBunchItem>;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Current DYOFinder instance.
|
|
56
|
+
*
|
|
57
|
+
* This instance offers advanced methods to manipulate items, like searching.
|
|
58
|
+
*/
|
|
59
|
+
protected _finder: DYOFinder;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Applying the parent constructor, and execute following process steps :
|
|
63
|
+
* * If **key** isn't provided and the property _domain is defined, the _key property has the _domain value.
|
|
64
|
+
* * Add *default* and *virtual* as basic _scopes value, and add **scopes**.
|
|
65
|
+
* * Create the Library instance, and add **elements** into it.
|
|
66
|
+
* * Initialize *DYOFinder* with **getFinderConfiguration** method.
|
|
67
|
+
*
|
|
68
|
+
* @param key
|
|
69
|
+
* @param elements Array of DTElement instance to add into the Manager Library. Default empty array.
|
|
70
|
+
* @param scopes Array of custom scopes for the Manager. Default empty array.
|
|
71
|
+
* @param options Specific options configuration for the instance. Default empty object.
|
|
72
|
+
*/
|
|
73
|
+
constructor(key?: string, elements: IBunchItem[] = [], scopes: string[] = [], options: Partial<DTManagerOptions> = {}) {
|
|
74
|
+
super(key, { ...defaultOptions, ...options });
|
|
75
|
+
// Use default _domain as _key
|
|
76
|
+
this._key = !key ? (this.getDomain() || this._id) : key;
|
|
77
|
+
|
|
78
|
+
this._items = {};
|
|
79
|
+
this._scopes = [
|
|
80
|
+
'default',
|
|
81
|
+
'virtual',
|
|
82
|
+
...scopes,
|
|
83
|
+
];
|
|
84
|
+
this._library = new DYOToolsBunch('library', elements, { virtualContext: true });
|
|
85
|
+
this._finder = new DYOFinder(this, this.getFinderConfiguration());
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Returns DYOFinder configuration for standard DTManager instance.
|
|
90
|
+
*
|
|
91
|
+
* This method can be overridden to extend the configuration.
|
|
92
|
+
*
|
|
93
|
+
* @returns DYOFinderConfiguration standard configuration.
|
|
94
|
+
*/
|
|
95
|
+
getFinderConfiguration(): DYOFinderConfiguration {
|
|
96
|
+
return componentManagerDefaultFinderConfiguration;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Getter for _library property.
|
|
101
|
+
*/
|
|
102
|
+
getLibrary(): DYOToolsBunch<IBunchItem> {
|
|
103
|
+
return this._library;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Getter for _scopes property.
|
|
108
|
+
*/
|
|
109
|
+
getScopes(): string[] {
|
|
110
|
+
return this._scopes;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Define if a scope name is a valid scope into the Manager.
|
|
115
|
+
*
|
|
116
|
+
* @param scope scope name.
|
|
117
|
+
* @returns boolean if the scope is valid or not.
|
|
118
|
+
*/
|
|
119
|
+
isValidScope(scope: string): boolean {
|
|
120
|
+
return this._scopes.includes(scope);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Add a DTBunch **item** into _items Manager property.
|
|
125
|
+
*
|
|
126
|
+
* The adding process has the following specifications :
|
|
127
|
+
* * If the added item has the same _id than existing item, an error occurred (depending on **errors** option).
|
|
128
|
+
* * The bunch item is automatically added to a scope depending on its virtual context :
|
|
129
|
+
* * * *default* if the bunch option **virtualContext** is false.
|
|
130
|
+
* * * *virtual* if the bunch option **virtualContext** is true.
|
|
131
|
+
* * An optional parameter **targetScope** can be passed to force the scope affectation.
|
|
132
|
+
* An error occurred if the affection doesn't conform to the following restrictions :
|
|
133
|
+
* * * Virtual context bunch must be affected to the 'virtual' scope.
|
|
134
|
+
* * * Not virtual context bunch must be affected to the 'default' scope, or any scope other than the 'virtual' one.
|
|
135
|
+
* * All elements of the added bunch item are added to the Manager Library, only if the element doesn't already exist
|
|
136
|
+
* into the Library.
|
|
137
|
+
* * The Manager instance becomes the new context of the added item.
|
|
138
|
+
* * The added item format follows the type DTManagerItemsType.
|
|
139
|
+
* @see DTManagerItemsType
|
|
140
|
+
*
|
|
141
|
+
* @param item A DTBunch instance to add into the Manager.
|
|
142
|
+
* @param targetScope Optional scope for affectation.
|
|
143
|
+
*/
|
|
144
|
+
add(item: DYOToolsBunch<IBunchItem>, targetScope?: string): void {
|
|
145
|
+
// Id conflict
|
|
146
|
+
if (Object.keys(this._items).includes(item.getId())) {
|
|
147
|
+
this.triggerError(new DYOToolsError(
|
|
148
|
+
'id_conflict',
|
|
149
|
+
'Bunch with same id already exists in the manager',
|
|
150
|
+
this,
|
|
151
|
+
item,
|
|
152
|
+
));
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Define scope validity
|
|
157
|
+
const { virtualContext } = item.getOptions();
|
|
158
|
+
let scope;
|
|
159
|
+
if (!targetScope) {
|
|
160
|
+
scope = virtualContext ? 'virtual' : 'default';
|
|
161
|
+
} else {
|
|
162
|
+
const { code: errorCode, message: errorMessage } = this.getErrorDataForScope(targetScope, virtualContext);
|
|
163
|
+
|
|
164
|
+
if (errorCode && errorMessage) {
|
|
165
|
+
this.triggerError(new DYOToolsError(errorCode, errorMessage, this, item));
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
scope = targetScope;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Update Library with non-existing new Elements
|
|
172
|
+
if (item.getAll().length) {
|
|
173
|
+
item.getAll().forEach((element: IBunchItem) => {
|
|
174
|
+
if (!this._library.get(element.getId())) {
|
|
175
|
+
this._library.add(element);
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Update context
|
|
181
|
+
item.setContext<DYOToolsManager<IBunchItem>>(this);
|
|
182
|
+
const oldContext = item.getContext();
|
|
183
|
+
if (oldContext && oldContext.getComponentType() === 'manager') {
|
|
184
|
+
(oldContext as DYOToolsManager<IBunchItem>).remove(item.getId());
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Add the new item
|
|
188
|
+
this._items[item.getId()] = {
|
|
189
|
+
scope,
|
|
190
|
+
item,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Add each bunch of an array **items** into _items Manager property.
|
|
196
|
+
*
|
|
197
|
+
* @see [add](#add) method for adding specifications.
|
|
198
|
+
* @param items An array of DTBunch instances to add into the Manager.
|
|
199
|
+
* @param targetScope Optional scope for affectation (all added items are affected).
|
|
200
|
+
*/
|
|
201
|
+
addMany(items: DYOToolsBunch<IBunchItem>[], targetScope?: string): void {
|
|
202
|
+
const previousItems = { ...this._items };
|
|
203
|
+
const { errors }: DTComponentOptions = this._options;
|
|
204
|
+
|
|
205
|
+
try {
|
|
206
|
+
items.forEach((item: DYOToolsBunch<IBunchItem>) => {
|
|
207
|
+
this.add(item, targetScope);
|
|
208
|
+
});
|
|
209
|
+
} catch (err: unknown) {
|
|
210
|
+
this._items = previousItems;
|
|
211
|
+
if (!errors) {
|
|
212
|
+
throw err;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Move a bunch item to a new scope.
|
|
219
|
+
*
|
|
220
|
+
* Note : an error occurred (depending on **errors** option) if the bunch id doesn't exist into the Manager.
|
|
221
|
+
*
|
|
222
|
+
* @see [add](#add) method for restrictions into the scope affectation.
|
|
223
|
+
* @param bunchId _id property of the bunch to move.
|
|
224
|
+
* @param targetScope new scope of the bunch.
|
|
225
|
+
*/
|
|
226
|
+
moveToScope(bunchId: string, targetScope: string): void {
|
|
227
|
+
if (!Object.keys(this._items).includes(bunchId)) {
|
|
228
|
+
this.triggerError(new DYOToolsError(
|
|
229
|
+
'invalid_id',
|
|
230
|
+
'Bunch id provided doesn\'t exist in the manager',
|
|
231
|
+
this,
|
|
232
|
+
));
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const bunch = this._items[bunchId].item;
|
|
237
|
+
|
|
238
|
+
const { virtualContext } = bunch.getOptions();
|
|
239
|
+
const { code: errorCode, message: errorMessage } = this.getErrorDataForScope(targetScope, virtualContext);
|
|
240
|
+
if (errorCode && errorMessage) {
|
|
241
|
+
this.triggerError(new DYOToolsError(errorCode, errorMessage, this, bunch));
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
this._items[bunchId].scope = targetScope;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Return one DTBunch instance included in the _items property by id.
|
|
250
|
+
*
|
|
251
|
+
* @param id bunch id to return.
|
|
252
|
+
* @returns DYOToolsBunch instance that corresponds to the id provided, or undefined if not found.
|
|
253
|
+
*/
|
|
254
|
+
get(id: string): DYOToolsBunch<IBunchItem> | undefined {
|
|
255
|
+
return this._items[id]?.item ?? undefined;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Return all DTBunch instance managed into the Manager.
|
|
260
|
+
*
|
|
261
|
+
* @param scope Optional parameter **scope** to return only bunches which are affected to a specific scope.
|
|
262
|
+
* @returns DYOToolsBunch array.
|
|
263
|
+
*/
|
|
264
|
+
getAll(scope?: string): DYOToolsBunch<IBunchItem>[] {
|
|
265
|
+
const finalItems = [];
|
|
266
|
+
Object.values(this._items).forEach((item: DTManagerItemType<IBunchItem>) => {
|
|
267
|
+
if (!scope || item.scope === scope) {
|
|
268
|
+
finalItems.push(item.item);
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
return finalItems;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Return the current affected scope of a bunch depending on its id.
|
|
277
|
+
*
|
|
278
|
+
* @param id bunch id to return.
|
|
279
|
+
* @returns Current scope affected to the bunch, or undefined if the bunch doesn't exist.
|
|
280
|
+
*/
|
|
281
|
+
getScope(id: string): string | undefined {
|
|
282
|
+
return this._items[id] && this._items[id].scope;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Remove one bunch into the _items property of the Manager, depending on its id.
|
|
287
|
+
*
|
|
288
|
+
* If option **libraryDeletion** is *true*, the method performs also a deletion of removed bunch elements
|
|
289
|
+
* in the Manager Library.
|
|
290
|
+
*
|
|
291
|
+
* @param id bunch id to remove.
|
|
292
|
+
*/
|
|
293
|
+
remove(id: string): void {
|
|
294
|
+
this.removeMany([id]);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Remove many bunches into the _items property of the Manager, depending on their ids.
|
|
299
|
+
*
|
|
300
|
+
* If option **libraryDeletion** is *true*, the method performs also a deletion of removed bunch elements
|
|
301
|
+
* in the Manager Library.
|
|
302
|
+
*
|
|
303
|
+
* @param ids Array of bunch ids to remove.
|
|
304
|
+
* @param options Optional Manager option configuration object to apply only for this method execution. Options are not
|
|
305
|
+
* saved in current _options property. Available Options are : **libraryDeletion** and **errors**.
|
|
306
|
+
*/
|
|
307
|
+
removeMany(ids: string[], options: Partial<DTManagerOptions> = {}): void {
|
|
308
|
+
const { libraryDeletion } = { ...this._options, ...options };
|
|
309
|
+
|
|
310
|
+
ids.forEach((id: string) => {
|
|
311
|
+
if (this._items[id]) {
|
|
312
|
+
if (libraryDeletion) {
|
|
313
|
+
this._items[id].item.getAll().forEach((item: IBunchItem) => {
|
|
314
|
+
this._library.remove(item.getId());
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
delete this._items[id];
|
|
319
|
+
}
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Remove all bunches into the _items property of the Manager.
|
|
325
|
+
*
|
|
326
|
+
* If option **libraryDeletion** is *true*, the method performs also a deletion of removed bunch elements
|
|
327
|
+
* in the Manager Library.
|
|
328
|
+
*/
|
|
329
|
+
removeAll(): void {
|
|
330
|
+
this.removeMany(Object.keys(this._items));
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Return an array of DTBunch from _items property filtered with a **filters** argument.
|
|
335
|
+
*
|
|
336
|
+
* This method use the DYOFinder instance **execute** method.
|
|
337
|
+
*
|
|
338
|
+
* Search filters can be applied on following bunch properties :
|
|
339
|
+
* * **id** : property _id. Basic operators only.
|
|
340
|
+
* * **key** : property _key. Basic operators only.
|
|
341
|
+
* * **scope** : affected scope name. Basic operators only.
|
|
342
|
+
* * **owner** : property _id of current _owner instance. Basic operators only.
|
|
343
|
+
* * **meta** : each meta Key of _meta property. Extended operators can be used.
|
|
344
|
+
*
|
|
345
|
+
* Examples of **filters** argument :
|
|
346
|
+
* * { key: { $eq: "key_1" } } : Return all DTBunch instances into _items with *key_1* as _key property.
|
|
347
|
+
* * { scope: { $in: ["default", "scope_1"] } } : Return all DTBunch instances into _items affected to scopes
|
|
348
|
+
* "default" or "scope_1".
|
|
349
|
+
* * { key: { $ne: "key_1" }, meta: { score: { $gte: 50, $lte: 100 } } } : Return all DTBunch instance into _items
|
|
350
|
+
* with _key property different from *key_1*, and meta key *score* value from _meta property between 50 and 100.
|
|
351
|
+
*
|
|
352
|
+
* @param filters Filters Object. The format is :
|
|
353
|
+
* { [property_1] : { [operator_1] : filter_value, [operator_2] : filter_value_2, ... }, [property_2] : { ... }, ... }
|
|
354
|
+
*
|
|
355
|
+
* For **meta**, you have to pass the meta key before the operator :
|
|
356
|
+
* { meta: { [meta_key1] : { [operator_1] : filter_value_1, ... }, [meta_key2] : { ... }, ... }, ... }
|
|
357
|
+
* @returns Array of DTBunch instance corresponding to the filters. Empty if no filter or invalid ones are passed.
|
|
358
|
+
* @see DYOFinder
|
|
359
|
+
*/
|
|
360
|
+
find(filters: Partial<DTManagerFilters>): DYOToolsBunch<IBunchItem>[] {
|
|
361
|
+
return this._finder.execute<DYOToolsBunch<IBunchItem>>(filters);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Remove all items into the Manager Library, and add items from all bunches instance managed by the Manager.
|
|
366
|
+
*
|
|
367
|
+
* This method is useful to guarantee that the Library contains all bunches items, especially after many complex
|
|
368
|
+
* operations.
|
|
369
|
+
*/
|
|
370
|
+
reloadLibrary(): void {
|
|
371
|
+
this._library.removeAll();
|
|
372
|
+
|
|
373
|
+
Object.values(this._items).forEach((item: DTManagerItemType<IBunchItem>) => {
|
|
374
|
+
item.item.getAll().forEach((element: IBunchItem) => {
|
|
375
|
+
if (!this._library.get(element.getId())) {
|
|
376
|
+
this._library.add(element);
|
|
377
|
+
}
|
|
378
|
+
});
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Return JSON Object representation of the Manager instance.
|
|
384
|
+
*
|
|
385
|
+
* JSON Object returned has the following structure :
|
|
386
|
+
* * **id** : _id property of the Manager.
|
|
387
|
+
* * **key** : _key property of the Manager.
|
|
388
|
+
* * **type** : _componentType property of the Manager.
|
|
389
|
+
* * **items** : Array of JSON Object representation for each DTBunch instance in _items property of the Manager.
|
|
390
|
+
*
|
|
391
|
+
* @returns JSON Object representation of the Manager.
|
|
392
|
+
*/
|
|
393
|
+
toObject(): DTManagerToObject {
|
|
394
|
+
const objectManager: DTManagerToObject = {
|
|
395
|
+
id: this._id,
|
|
396
|
+
key: this._key,
|
|
397
|
+
type: this._componentType,
|
|
398
|
+
items: [],
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
Object.keys(this._items).forEach((bunchId: string) => {
|
|
402
|
+
objectManager.items.push({
|
|
403
|
+
scope: this._items[bunchId].scope,
|
|
404
|
+
...this._items[bunchId].item.toObject(),
|
|
405
|
+
});
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
return objectManager;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Return String representation of the Manager instance.
|
|
413
|
+
*
|
|
414
|
+
* @returns String representation of the Manager.
|
|
415
|
+
*/
|
|
416
|
+
toString(): string {
|
|
417
|
+
const libraryLabel = `Library: ${this._library.getAll().length}`;
|
|
418
|
+
|
|
419
|
+
return `Component ${this._key} - Type: Manager - ${libraryLabel} - Items: ${Object.keys(this._items).length}`;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Get error data for scope affectation. Used in method which performs scope affectation.
|
|
424
|
+
*
|
|
425
|
+
* @param targetScope scope name to check for affectation.
|
|
426
|
+
* @param virtualContext boolean if the bunch to affect has **virtualContext** option enabled or not. Default *false*.
|
|
427
|
+
* @returns An Object with error code and message.
|
|
428
|
+
*/
|
|
429
|
+
private getErrorDataForScope(targetScope: string, virtualContext = false): { code: string, message: string } {
|
|
430
|
+
const response = { code: '', message: '' };
|
|
431
|
+
if (!this.isValidScope(targetScope)) {
|
|
432
|
+
response.code = 'invalid_scope';
|
|
433
|
+
response.message = "Scope provided doesn't exist in the manager";
|
|
434
|
+
}
|
|
435
|
+
if (virtualContext && targetScope !== 'virtual') {
|
|
436
|
+
response.code = 'forbidden_scope';
|
|
437
|
+
response.message = 'Scope provided cannot be associated to a virtual bunch';
|
|
438
|
+
}
|
|
439
|
+
if (!virtualContext && targetScope === 'virtual') {
|
|
440
|
+
response.code = 'forbidden_virtual_scope';
|
|
441
|
+
response.message = 'Virtual Scope provided cannot be associated to a physical bunch';
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
return response;
|
|
445
|
+
}
|
|
446
|
+
}
|