Skip to content

Commit ec5c61e

Browse files
committed
hooks documentation
1 parent bc77f55 commit ec5c61e

1 file changed

Lines changed: 220 additions & 0 deletions

File tree

content/docs/ox_lib/Hooks.mdx

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
---
2+
title: Hooks
3+
---
4+
5+
See our [internally registered hooks](https://github.com/overextended/ox_lib/blob/main/resource/state/server.lua) for examples.
6+
7+
# HookPipeline
8+
9+
Creates a hook pipeline for a specific event, which manages a collection of registered hooks and controls execution flow through filtering, rejection, and dispatching.
10+
11+
<Tabs groupId="language" items={['Lua', 'TypeScript']}>
12+
<Tab>
13+
```lua
14+
lib.hook:new(event, filter)
15+
```
16+
</Tab>
17+
<Tab>
18+
```ts
19+
import { HookPipeline } from "@overextended/ox_lib/hooks";
20+
21+
new HookPipeline(event, filter)
22+
```
23+
</Tab>
24+
</Tabs>
25+
26+
- event: `string`
27+
- filter?: `function(hook, payload): boolean`
28+
- Used to filter out registered hooks based on if they pass a predicate function.
29+
30+
**Returns:** `HookPipeline`
31+
32+
## Methods
33+
34+
### registerHook
35+
36+
Registers a new event handler into the hook pipeline.
37+
38+
<Tabs groupId="language" items={['Lua', 'TypeScript']}>
39+
<Tab>
40+
```lua
41+
pipeline:registerHook(handler, options)
42+
```
43+
</Tab>
44+
<Tab>
45+
```ts
46+
pipeline.registerHook(handler, options)
47+
```
48+
</Tab>
49+
</Tabs>
50+
51+
- handler: `function(payload: unknown): boolean`
52+
- options?: `table`
53+
- Optional metadata to attach to the hook.
54+
55+
### remove
56+
57+
Removes hooks from the pipeline.
58+
59+
<Tabs groupId="language" items={['Lua', 'TypeScript']}>
60+
<Tab>
61+
```lua
62+
pipeline:remove(hookId)
63+
```
64+
</Tab>
65+
<Tab>
66+
```ts
67+
pipeline.remove(hookId)
68+
```
69+
</Tab>
70+
</Tabs>
71+
72+
- hookId?: `string`
73+
- If defined, only the specified hook is removed - otherwise all hooks for the invoking resource are removed.
74+
75+
### dispatch
76+
77+
Executes each registered hook in order of registration, checking the payload against a provided filter using the hook options and executing the hook callback.
78+
79+
A hook may block execution by returning `false` from a registered handler.
80+
81+
If any hook rejects the execution, dispatch is cancelled and `result.ok` is set to `false`.
82+
83+
The returned object acts as a finalisation handle and emits results to registered handlers once closed.
84+
85+
<Tabs groupId="language" items={['Lua', 'TypeScript']}>
86+
<Tab>
87+
```lua
88+
local hook <close> = pipeline:dispatch(payload)
89+
```
90+
</Tab>
91+
<Tab>
92+
```ts
93+
using hook = pipeline.dispatch(payload)
94+
```
95+
</Tab>
96+
</Tabs>
97+
98+
- payload: `unknown`
99+
100+
# EventHook
101+
102+
Allows resources to call [`pipeline.registerHook`](#registerHook) using exports.
103+
104+
<Tabs groupId="language" items={['Lua', 'TypeScript']}>
105+
<Tab>
106+
```lua
107+
lib.registerHook(eventName, handler, options)
108+
```
109+
</Tab>
110+
<Tab>
111+
```ts
112+
import { registerHook } from "@overextended/ox_lib/hooks";
113+
114+
registerHook(eventName, handler, options)
115+
```
116+
</Tab>
117+
</Tabs>
118+
119+
- eventName: `string`
120+
- handler?: `function(payload: unknown): boolean`
121+
- options?: Optional metadata to attach to the hook.
122+
123+
## Methods
124+
125+
### on
126+
127+
Attaches a post-execution event handler for this hook.
128+
129+
<Tabs groupId="language" items={['Lua', 'TypeScript']}>
130+
<Tab>
131+
```lua
132+
hook:on(handler)
133+
```
134+
</Tab>
135+
<Tab>
136+
```ts
137+
hook.on(handler)
138+
```
139+
</Tab>
140+
</Tabs>
141+
142+
- handler: `function(ok: boolean, payload: unknown)`
143+
144+
### off
145+
146+
Detatches the currently registered post-hook event handler, if one exists.
147+
148+
<Tabs groupId="language" items={['Lua', 'TypeScript']}>
149+
<Tab>
150+
```lua
151+
hook:off()
152+
```
153+
</Tab>
154+
<Tab>
155+
```ts
156+
hook.off()
157+
```
158+
</Tab>
159+
</Tabs>
160+
161+
### remove
162+
163+
Fully removes this hook from both the local event system and the external hook pipeline provided from the originating resource.
164+
165+
<Tabs groupId="language" items={['Lua', 'TypeScript']}>
166+
<Tab>
167+
```lua
168+
hook:remove()
169+
```
170+
</Tab>
171+
<Tab>
172+
```ts
173+
hook.remove()
174+
```
175+
</Tab>
176+
</Tabs>
177+
178+
# Registered Hooks
179+
180+
These hook pipelines are registered and managed by ox_lib for other resources to interact with.
181+
182+
See [EventHook](Hooks#eventhook) for more information.
183+
184+
## ox_lib:setPlayerState
185+
186+
When using `sv_stateBagStrictMode` players cannot arbitrarily modify their own states, and changes will be rejected by default.
187+
188+
<Tabs groupId="language" items={['Lua', 'TypeScript']}>
189+
<Tab>
190+
```lua
191+
lib.registerHook('ox_lib:setPlayerState', predicate, { key: state })
192+
```
193+
</Tab>
194+
<Tab>
195+
```ts
196+
import { registerHook } from "@overextended/ox_lib/hooks";
197+
198+
registerHook('ox_lib:setPlayerState', predicate, { key: state })
199+
```
200+
</Tab>
201+
</Tabs>
202+
203+
## ox_lib:setEntityState
204+
205+
When using `sv_stateBagStrictMode` players cannot arbitrarily modify entity states, and changes will be rejected by default.
206+
207+
<Tabs groupId="language" items={['Lua', 'TypeScript']}>
208+
<Tab>
209+
```lua
210+
lib.registerHook('ox_lib:setEntityState', predicate, { key: state })
211+
```
212+
</Tab>
213+
<Tab>
214+
```ts
215+
import { registerHook } from "@overextended/ox_lib/hooks";
216+
217+
registerHook('ox_lib:setEntityState', predicate, { key: state })
218+
```
219+
</Tab>
220+
</Tabs>

0 commit comments

Comments
 (0)