You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// when you don't know the specific parameters, you can use `getMiddlewareContext` fn get `next` and `task` object.
87
+
const handle2 = (...args:any[]) => {
88
+
const { task, next } =getMiddlewareContext(...args)
89
+
console.log('handle task 2', args, task)
90
+
next()
91
+
}
85
92
86
93
// define event signal with crosscheck, and customized cacheable tasks
87
94
// note: use redis as the cache layer
@@ -162,6 +169,7 @@ Each `.task(...)` starts a `Task Flow`
162
169
-~~`return true` to identify handle success, and entering `onSuccess`~~
163
170
-~~`return false` to identify handle failed, and entering `onFailed`~~
164
171
- It will automatically detect success and failed and call the `onSuccess` and `onFailed` hooks
172
+
- this fn is essentially a middleware, so it has all the features of middleware
165
173
166
174
**.cache(sm: StoreManager)**
167
175
- set the store to cache the tasks
@@ -181,6 +189,7 @@ Each `.task(...)` starts a `Task Flow`
181
189
182
190
**.key(toKey: ToKeyFn)**
183
191
- defines the primary key of a task based on the event values (i.e. log topics)
192
+
-`ToKeyFn` will callback with the event values, and should return a string as the key
184
193
- default: random hex string
185
194
186
195
**.success(onSuccess: HandleResultFn)**
@@ -198,16 +207,194 @@ Each `.task(...)` starts a `Task Flow`
198
207
- back to the parent `Event Flow`, so that it can add another `.task`
199
208
- e.g. `orap.event(...).task().another().task()`
200
209
201
-
### Middlewares
210
+
## Middlewares
211
+
212
+
The middlewares are used in the `Task Flow` to handle the task processing, it's a chain of functions that can be called in order.
202
213
203
-
The middlewares are used in the `Task Flow` to handle the task processing, it's a chain of functions that can be called in order.
214
+
You can define `use` to add a middleware to the task flow.
204
215
205
-
#### Features
216
+
NOTE: handle is a middleware that can be flexibly placed at any position within the middleware chain, and it will be called in order.
217
+
218
+
> Middleware is only applicable to task flow, not event flow.
219
+
220
+
### Features
206
221
207
222
- the last parameter of the handler is the `next` fn, so you have to call it to continue the next handler.
208
223
- the penultimate parameter is the `TaskRaplized` object, which contains the task info.
209
224
- you can pass parameters to the next handler by calling `next(param1, param2, ...)`, it will be passed to the next handler as arguments, note: you cannot pass `TaskRaplized` object and `next` fn to the next handler, it will pass the next handler with the `TaskRaplized` object and `next` fn automatically.
210
225
226
+
### Usage
227
+
228
+
#### create a middleware for check transaction status
// it will be caught by the error handler, and terminate the execution of the entire middlewares chain
267
+
thrownewError('Transaction failed')
268
+
}
269
+
awaitnext()
270
+
}
271
+
else {
272
+
// throw error if the contract event payload is invalid, it will be caught by the error handler, and terminate the execution of the entire middlewares chain
0 commit comments