Skip to content

Commit

Permalink
docs: Add "Events" info to Callbacks docs page
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Nov 14, 2024
1 parent 834a2ff commit ce4c415
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
Binary file modified bun.lockb
Binary file not shown.
47 changes: 44 additions & 3 deletions docs/docs/types/callbacks.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,50 @@ Nitro has a clever reference counting system to allow users to use callbacks/fun
Each callback holds a strong reference on the native side and can be called as often as needed.
Once the callback is no longer used, it will be safely deleted from memory.

<Tabs>
<TabItem value="ts" label="TypeScript" default>
In TypeScript, a callback is represented as an anonymous function:

```ts
interface Server extends HybridObject {
start(onNewUserJoined: (user: User) => void): void
}
```
</TabItem>
<TabItem value="cpp" label="C++">
In C++, a callback is represented as a function:

```cpp
void start(std::function<void(User)> onNewUserJoined) {
onNewUserJoined(user);
}
```
</TabItem>
<TabItem value="swift" label="Swift">
In Swift, a callback is represented as a closure:

```swift
func start(onNewUserJoined: (User) -> Void) {
onNewUserJoined(user)
}
```
</TabItem>
<TabItem value="kotlin" label="Kotlin">
In Kotlin, a callback is represented as a lambda:

```kotlin
fun start(onNewUserJoined: (User) -> Unit) {
onNewUserJoined(user)
}
```
</TabItem>
</Tabs>

## Events

Since callbacks can be safely kept in memory for longer and called multiple times, Nitro does not have a special type for an "event".
It is simply a function you store in memory and call later, just like in a normal JS class. ✨

<Tabs>
<TabItem value="ts" label="TypeScript" default>
In TypeScript, a callback is represented as an anonymous function:
Expand Down Expand Up @@ -75,9 +119,6 @@ Once the callback is no longer used, it will be safely deleted from memory.
</TabItem>
</Tabs>

Since callbacks can be safely kept in memory for longer and called multiple times, Nitro does not have a special type for an "event".
It is simply a function you store in memory and call later. ✨

## Callbacks that return a value (`(...) => T`)

Since JS callbacks could theoretically be called from any native Thread,
Expand Down

0 comments on commit ce4c415

Please sign in to comment.