@powfix/core-js 0.13.0 → 0.13.2

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.
@@ -17,6 +17,11 @@ declare class TransactionManager {
17
17
  removeListener<T extends EventEmitter.EventNames<TransactionManagerEventTypes>>(event: T, fn: EventEmitter.EventListener<TransactionManagerEventTypes, T>): void;
18
18
  add(transaction: Transaction, option?: TransactionManagerAddOption): void;
19
19
  remove(transaction: Transaction): void;
20
+ flush(action?: TransactionManagerTimeoutAction): Promise<PromiseSettledResult<{
21
+ handled: boolean;
22
+ transaction: Transaction;
23
+ }>[]>;
24
+ private execute;
20
25
  private timeout;
21
26
  private afterCommit;
22
27
  }
@@ -22,7 +22,7 @@ class TransactionManager {
22
22
  this.transactionTimeoutMap = new Map();
23
23
  // Emitter
24
24
  this.emitter = new eventemitter3_1.default();
25
- if (this.logLevel >= constants_1.TransactionManagerLogLevel.VERBOSE) {
25
+ if (this.logLevel <= constants_1.TransactionManagerLogLevel.VERBOSE) {
26
26
  console.log(LOG_TAG, 'TransactionManager instance initialized');
27
27
  }
28
28
  }
@@ -35,7 +35,7 @@ class TransactionManager {
35
35
  }
36
36
  setLogLevel(logLevel) {
37
37
  this.logLevel = logLevel;
38
- if (this.logLevel >= constants_1.TransactionManagerLogLevel.VERBOSE) {
38
+ if (this.logLevel <= constants_1.TransactionManagerLogLevel.VERBOSE) {
39
39
  console.log(LOG_TAG, 'log level changed to', logLevel);
40
40
  }
41
41
  }
@@ -48,7 +48,7 @@ class TransactionManager {
48
48
  add(transaction, option) {
49
49
  var _a, _b;
50
50
  if (this.transactionTimeoutMap.has(transaction)) {
51
- if (this.logLevel >= constants_1.TransactionManagerLogLevel.ERROR) {
51
+ if (this.logLevel <= constants_1.TransactionManagerLogLevel.ERROR) {
52
52
  console.error(LOG_TAG, 'transaction already exists');
53
53
  }
54
54
  return;
@@ -56,29 +56,43 @@ class TransactionManager {
56
56
  const action = (_a = option === null || option === void 0 ? void 0 : option.timeoutAction) !== null && _a !== void 0 ? _a : TransactionManager.DEFAULT_ACTION;
57
57
  const timeout = (_b = option === null || option === void 0 ? void 0 : option.timeout) !== null && _b !== void 0 ? _b : TransactionManager.DEFAULT_TIMEOUT;
58
58
  const handler = setTimeout(this.timeout.bind(this), timeout, transaction, action);
59
- this.transactionTimeoutMap.set(transaction, handler);
59
+ this.transactionTimeoutMap.set(transaction, {
60
+ handler,
61
+ action,
62
+ });
60
63
  // Callback
61
64
  transaction.afterCommit(this.afterCommit.bind(this));
62
65
  }
63
66
  remove(transaction) {
64
67
  const transactionTimeout = this.transactionTimeoutMap.get(transaction);
65
68
  if (transactionTimeout != null) {
66
- clearTimeout(transactionTimeout);
69
+ clearTimeout(transactionTimeout.handler);
67
70
  this.transactionTimeoutMap.delete(transaction);
68
- if (this.logLevel >= constants_1.TransactionManagerLogLevel.VERBOSE) {
71
+ if (this.logLevel <= constants_1.TransactionManagerLogLevel.VERBOSE) {
69
72
  console.log(LOG_TAG, this.getTransactionLogArg(transaction), 'removed');
70
73
  }
71
74
  }
72
75
  }
73
- timeout(transaction, action) {
76
+ flush(action) {
77
+ return Promise.allSettled(Array.from(this.transactionTimeoutMap.entries()).map(([transaction, transactionTimeout]) => __awaiter(this, void 0, void 0, function* () {
78
+ const handled = yield this.execute(transaction, action !== null && action !== void 0 ? action : transactionTimeout.action, 'flush');
79
+ this.remove(transaction);
80
+ return {
81
+ handled,
82
+ transaction,
83
+ };
84
+ })));
85
+ }
86
+ execute(transaction, action, reason) {
74
87
  return __awaiter(this, void 0, void 0, function* () {
88
+ let handled = false;
75
89
  const finished = transaction === null || transaction === void 0 ? void 0 : transaction.finished;
76
90
  if (finished != null) {
77
- if (this.logLevel >= constants_1.TransactionManagerLogLevel.VERBOSE) {
78
- console.log(LOG_TAG, this.getTransactionLogArg(transaction), `is already handled(${finished}) after timeout`);
91
+ if (this.logLevel <= constants_1.TransactionManagerLogLevel.VERBOSE) {
92
+ console.log(LOG_TAG, this.getTransactionLogArg(transaction), `is already handled(${finished}) after`, reason);
79
93
  }
80
94
  this.remove(transaction);
81
- return;
95
+ return handled;
82
96
  }
83
97
  try {
84
98
  let finished;
@@ -94,29 +108,37 @@ class TransactionManager {
94
108
  break;
95
109
  }
96
110
  default: {
97
- if (this.logLevel >= constants_1.TransactionManagerLogLevel.ERROR) {
111
+ if (this.logLevel <= constants_1.TransactionManagerLogLevel.ERROR) {
98
112
  console.error(LOG_TAG, `unknown action`, action);
99
113
  }
100
114
  break;
101
115
  }
102
116
  }
103
- const handled = finished != null;
117
+ handled = finished != null;
104
118
  if (finished != null) {
105
- if (this.logLevel >= constants_1.TransactionManagerLogLevel.ERROR) {
106
- console.error(LOG_TAG, this.getTransactionLogArg(transaction), `handled(${finished}) after timeout`);
119
+ if (this.logLevel <= constants_1.TransactionManagerLogLevel.ERROR) {
120
+ console.error(LOG_TAG, this.getTransactionLogArg(transaction), `handled(${finished}) after`, reason);
107
121
  }
108
122
  }
109
123
  else {
110
- if (this.logLevel >= constants_1.TransactionManagerLogLevel.ERROR) {
124
+ if (this.logLevel <= constants_1.TransactionManagerLogLevel.ERROR) {
111
125
  console.error(LOG_TAG, this.getTransactionLogArg(transaction), `not handled 🚫`);
112
126
  }
113
127
  }
114
- // Emit
115
- this.emitter.emit('onUnhandledTransaction', transaction, handled);
116
128
  }
117
129
  catch (e) {
118
130
  console.error(e);
119
131
  }
132
+ finally {
133
+ this.remove(transaction);
134
+ }
135
+ return handled;
136
+ });
137
+ }
138
+ timeout(transaction, action) {
139
+ return __awaiter(this, void 0, void 0, function* () {
140
+ const handled = yield this.execute(transaction, action, 'timeout');
141
+ this.emitter.emit('onUnhandledTransaction', transaction, handled);
120
142
  });
121
143
  }
122
144
  afterCommit(transaction) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@powfix/core-js",
3
- "version": "0.13.0",
3
+ "version": "0.13.2",
4
4
  "description": "core package",
5
5
  "author": "Kwon Kyung-Min <powfix@gmail.com>",
6
6
  "private": false,