framework-do-dede 5.4.4 → 5.4.6

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/README.md CHANGED
@@ -279,6 +279,8 @@ class CreatePhotoUseCase extends UseCase<{ name: string }, { id: string }> {
279
279
  }
280
280
  ```
281
281
 
282
+ Por padrão, `HookBefore` e `HookAfter` recebem `this.data` se nenhum payload for definido manualmente. Apenas `HookAfter` pode ser sobrescrito via `this.afterHook.use(...)`. O `HookBefore` não recebe o retorno do `execute()` por ser um evento before.
283
+
282
284
  `HookAfter` não executa quando o método lança erro. Para executar mesmo em erro:
283
285
 
284
286
  ```ts
@@ -35,9 +35,11 @@ declare class HookManager {
35
35
  private readonly position;
36
36
  constructor(owner: UseCase<any, any>, position: HookPosition);
37
37
  use(payload: unknown): void;
38
+ useIfUnset(payload: unknown): void;
38
39
  notifyBefore(): Promise<void>;
39
40
  notifyAfter(onError: boolean): Promise<void>;
40
41
  private buildEntry;
42
+ private notifyEntry;
41
43
  }
42
44
  export declare function DecorateUseCase(options: DecorateUseCaseOptions): <T extends UseCaseConstructor>(target: T) => T;
43
45
  export declare function HookBefore(hookClass: HookConstructor): <T extends UseCaseConstructor>(target: T) => T;
@@ -33,6 +33,7 @@ function ensureHookedExecution(target) {
33
33
  const wrapped = async function executeWithHooks(...args) {
34
34
  const beforeHook = this.beforeHook;
35
35
  const afterHook = this.afterHook;
36
+ beforeHook.useIfUnset(this.data);
36
37
  await beforeHook.notifyBefore();
37
38
  let result;
38
39
  let originalError;
@@ -42,6 +43,7 @@ function ensureHookedExecution(target) {
42
43
  catch (error) {
43
44
  originalError = error;
44
45
  }
46
+ afterHook.useIfUnset(this.data);
45
47
  await afterHook.notifyAfter(!!originalError);
46
48
  if (originalError) {
47
49
  throw originalError;
@@ -74,11 +76,22 @@ class HookManager {
74
76
  this.entry = this.buildEntry();
75
77
  }
76
78
  use(payload) {
77
- this.entry?.setPayload(payload);
79
+ if (!this.entry) {
80
+ return;
81
+ }
82
+ this.entry.setPayload(payload);
83
+ this.entry.payloadSet = true;
84
+ }
85
+ useIfUnset(payload) {
86
+ if (!this.entry || this.entry.payloadSet) {
87
+ return;
88
+ }
89
+ this.entry.setPayload(payload);
90
+ this.entry.payloadSet = true;
78
91
  }
79
92
  async notifyBefore() {
80
93
  if (this.position === 'before' && this.entry?.metadata.position === 'before') {
81
- await this.entry.instance.notify();
94
+ await this.notifyEntry(this.entry);
82
95
  }
83
96
  }
84
97
  async notifyAfter(onError) {
@@ -88,7 +101,7 @@ class HookManager {
88
101
  if (onError && !this.entry.metadata.runOnError) {
89
102
  return;
90
103
  }
91
- await this.entry.instance.notify();
104
+ await this.notifyEntry(this.entry);
92
105
  }
93
106
  buildEntry() {
94
107
  const metadata = getHookMetadata(this.owner.constructor);
@@ -97,13 +110,25 @@ class HookManager {
97
110
  return undefined;
98
111
  }
99
112
  const instance = new entry.hookClass();
100
- return {
113
+ const hookEntry = {
101
114
  metadata: entry,
102
115
  instance,
116
+ payload: undefined,
117
+ payloadSet: false,
103
118
  setPayload: (payload) => {
119
+ hookEntry.payload = payload;
104
120
  instance.setPayload(payload, this.owner.context);
105
121
  },
106
122
  };
123
+ return hookEntry;
124
+ }
125
+ async notifyEntry(entry) {
126
+ const proto = Object.getPrototypeOf(entry.instance);
127
+ const usesBaseNotify = proto.notify === Hook.prototype.notify;
128
+ if (!usesBaseNotify) {
129
+ await entry.instance.use(entry.payload, entry.instance.context);
130
+ }
131
+ await entry.instance.notify();
107
132
  }
108
133
  }
109
134
  function buildUseCaseInstances(owner, options) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "framework-do-dede",
3
- "version": "5.4.4",
3
+ "version": "5.4.6",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",