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
Events that are registered in `guard`, and scoped instance will not be exposed to other instances, thus containing the event like `guard`
214
+
By changing the `type` value, the result should be as follows:
182
215
183
-
The response should be listed as follows:
184
-
| Path | Content-Type |
185
-
| ----- | ----------------------- |
186
-
| / | text/plain; charset=utf8 |
187
-
| /inner | text/html; charset=utf8 |
188
-
| /outer | text/plain; charset=utf8 |
216
+
| type | child | current | parent | main |
217
+
| ---------- | ----- | ------- | ------ | ---- |
218
+
| 'local' | ✅ | ✅ | ❌ | ❌ |
219
+
| 'scoped' | ✅ | ✅ | ✅ | ❌ |
220
+
| 'global' | ✅ | ✅ | ✅ | ✅ |
189
221
190
-
### Encapsulation
222
+
## guard
223
+
Guard is a hard limit for hook type.
191
224
192
-
It is important to note that the scoped instance, just like `guard`, will inherit the previous events from the main instance but not expose those registered in the scope.
225
+
Any life-cycle defined in `guard`, and `group` **will always** be contained in scope, even if hook type is **global**
193
226
194
227
```typescript
195
228
import { Elysia } from'elysia'
196
229
197
-
constscoped=newElysia({ scoped: true })
198
-
.onAfterHandle(() => {
199
-
console.log('1')
230
+
constplugin=newElysia()
231
+
.beforeHandle({ as: 'global' }, () => {
232
+
console.log('hi')
200
233
})
201
-
.get('/inner', () =>'hi')
202
234
203
-
newElysia()
204
-
.onAfterHandle(()=>{
205
-
console.log('2')
206
-
})
207
-
.use(scoped)
208
-
.get('/outer', () =>'hi')
235
+
const app =newElysia()
236
+
.guard(app=>app
237
+
.use(plugin)
238
+
.get('/inner', () =>'inner')
239
+
)
240
+
.get('/outer', () =>'outer')
209
241
.listen(3000)
210
242
```
211
243
212
-
The response should be listed as follows:
213
-
| Path | Log |
214
-
| ----- | ----------------------- |
215
-
| /inner | 1, 2 |
216
-
| /outer | 2 |
217
-
218
-
Scope and guard only prevent the event from being inherited but the scope itself will inherit the events.
0 commit comments