Skip to content

Commit

Permalink
docs: clarify load order
Browse files Browse the repository at this point in the history
  • Loading branch information
Stuyk committed May 2, 2024
1 parent 70b2607 commit d53c0c6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
36 changes: 34 additions & 2 deletions docs/api/client/api/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export function useMyCoolAPI() {
```

2. Create a global declaration for your API.
+++ Without arguments
+++ Without arguments

```ts
import { useClientApi } from '@Client/api/index.js';

Expand All @@ -47,7 +48,9 @@ declare global {
// Really important to execute the return of your function
useClientApi().register('my-cool-api', useMyCoolAPI());
```

+++ With arguments

```ts
import { useClientApi } from '@Client/api/index.js';

Expand All @@ -71,6 +74,7 @@ declare global {
// Don't execute a composable here, because it will be executed later.
useClientApi().register('my-cool-api', useMyCoolAPI);
```

+++

3. Done
Expand All @@ -79,7 +83,10 @@ useClientApi().register('my-cool-api', useMyCoolAPI);

This is all that's necessary to start working with other plugin APIs

Below is dependent on load order, so your mileage may vary.

+++ Without arguments

```ts
import { useClientApi } from '@Client/api/index.js';

Expand All @@ -89,7 +96,9 @@ function someFunction(somePlayer: alt.Player) {
myCoolAPI.logPlayerName(somePlayer);
}
```

+++ With arguments

```ts
import { useClientApi } from '@Client/api/index.js';

Expand All @@ -99,4 +108,27 @@ function someFunction(somePlayer: alt.Player) {
useMyCoolAPI(somePlayer).logPlayerName();
}
```
+++

If you do not want to worry about load order. Consider the following pattern:

```ts
import * as alt from 'alt-client';
import { useClientApi } from '@Client/api/index.js';

const api = useClientApi();

async function init() {
// Wait for the API to be ready
await alt.Utils.waitFor(() => api.isReady('auth-api'), 30000);

// Get the API
const authApi = api.get('auth-api');

// Hook in your events
authApi.onLogin((player) => {
alt.log('Player Ready!');
});
}

init();
```
29 changes: 28 additions & 1 deletion docs/api/server/api/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ useApi().register('my-cool-api', useMyCoolAPI());

## How to Get an API

This is all that's necessary to start working with other plugin APIs
This is all that's necessary to start working with other plugin APIs.

Below is dependent on load order, so your mileage may vary.

```ts
import { useRebar } from '@Server/index.js';
Expand All @@ -64,3 +66,28 @@ function someFunction(somePlayer: alt.Player) {
myCoolAPI.logPlayerName(somePlayer);
}
```

If you do not want to worry about load order. Consider the following pattern:

```ts
import * as alt from 'alt-server';
import { useRebar } from '@Server/index.js';

const Rebar = useRebar();
const api = Rebar.useApi();

async function init() {
// Wait for the API to be ready
await alt.Utils.waitFor(() => api.isReady('auth-api'), 30000);

// Get the API
const authApi = api.get('auth-api');

// Hook in your events
authApi.onLogin((player) => {
alt.log('Player Ready!');
});
}

init();
```

0 comments on commit d53c0c6

Please sign in to comment.