schangxiang@126.com
2025-09-19 0821aa23eabe557c0d9ef5dbe6989c68be35d1fe
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
'use strict';
 
const Aspect = {
  READ_OPERATION: Symbol('READ_OPERATION'),
  SKIP_SESSION: Symbol('SKIP_SESSION'),
  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, including `SKIP_SESSION` and other aspects to encode retryability
 * and other functionality.
 */
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
};