You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.4 KiB
66 lines
1.4 KiB
4 years ago
|
'use strict';
|
||
|
|
||
|
const Aspect = {
|
||
|
READ_OPERATION: Symbol('READ_OPERATION'),
|
||
|
WRITE_OPERATION: Symbol('WRITE_OPERATION'),
|
||
|
RETRYABLE: Symbol('RETRYABLE'),
|
||
|
EXECUTE_WITH_SELECTION: Symbol('EXECUTE_WITH_SELECTION')
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* This class acts as a parent class for any operation and is responsible for setting this.options,
|
||
|
* as well as setting and getting a session.
|
||
|
* Additionally, this class implements `hasAspect`, which determines whether an operation has
|
||
|
* a specific aspect.
|
||
|
*/
|
||
|
class OperationBase {
|
||
|
constructor(options) {
|
||
|
this.options = Object.assign({}, options);
|
||
|
}
|
||
|
|
||
|
hasAspect(aspect) {
|
||
|
if (this.constructor.aspects == null) {
|
||
|
return false;
|
||
|
}
|
||
|
return this.constructor.aspects.has(aspect);
|
||
|
}
|
||
|
|
||
|
set session(session) {
|
||
|
Object.assign(this.options, { session });
|
||
|
}
|
||
|
|
||
|
get session() {
|
||
|
return this.options.session;
|
||
|
}
|
||
|
|
||
|
clearSession() {
|
||
|
delete this.options.session;
|
||
|
}
|
||
|
|
||
|
get canRetryRead() {
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
execute() {
|
||
|
throw new TypeError('`execute` must be implemented for OperationBase subclasses');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function defineAspects(operation, aspects) {
|
||
|
if (!Array.isArray(aspects) && !(aspects instanceof Set)) {
|
||
|
aspects = [aspects];
|
||
|
}
|
||
|
aspects = new Set(aspects);
|
||
|
Object.defineProperty(operation, 'aspects', {
|
||
|
value: aspects,
|
||
|
writable: false
|
||
|
});
|
||
|
return aspects;
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
Aspect,
|
||
|
defineAspects,
|
||
|
OperationBase
|
||
|
};
|