-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: synced binder for webview, client, etc. (#90)
- Loading branch information
Showing
15 changed files
with
347 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# useStreamSyncedGetter | ||
|
||
When using the [Stream Synced Binder](../../server/systems/useStreamSyncedBinder.md), this allows you to get type safe responses on client-side. | ||
|
||
```ts | ||
import * as alt from 'alt-client'; | ||
import { useRebarClient } from '../../../main/client/index.js'; | ||
|
||
const RebarClient = useRebarClient(); | ||
|
||
const streamGetter = RebarClient.systems.useStreamSyncedGetter(); | ||
|
||
const money = streamGetter.player(alt.Player.local).get('money'); | ||
const name = streamGetter.player(alt.Player.local).get('name'); | ||
|
||
const fuel = streamGetter.vehicle(someVehicle).get('fuel'); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# useStreamSyncedBinder | ||
|
||
What this allows you to do is target a specific document field from a `Character` or `Vehicle` document and automatically synchronize the document data to client-side through `streamSyncedMeta`. | ||
|
||
Whenever the value changes, it automatically updates for a player or vehicle. | ||
|
||
For instance, if specify the propery `money` and `money` is updated for the player, it will be available on client-side, and in the webview. | ||
|
||
```ts | ||
import { useRebar } from '../../../main/server/index.js'; | ||
|
||
const Rebar = useRebar(); | ||
const SyncedBinder = Rebar.systems.useStreamSyncedBinder(); | ||
|
||
// Bind character document data | ||
SyncedBinder.syncCharacterKey('money'); | ||
SyncedBinder.syncCharacterKey('name'); | ||
|
||
// Bind vehicle document data | ||
SyncedBinder.syncVehicleKey('fuel'); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# useSyncedMeta | ||
|
||
When using the [Stream Synced Binder](../../api/server/systems/useStreamSyncedBinder.md), this webview component allows you to get that data as vue refs. | ||
|
||
```jsx | ||
<script lang="ts" setup> | ||
import { useSyncedMeta } from '../../../../webview/composables/useSyncedMeta'; | ||
|
||
const syncedMeta = useSyncedMeta(); | ||
|
||
const character = syncedMeta.getCharacter(); | ||
const vehicle = syncedMeta.getVehicle(); | ||
</script> | ||
|
||
<template> | ||
<div class="fixed left-0 top-0 bg-black text-white"> | ||
<div class="text-2xl">Character</div> | ||
{{ character }} | ||
<div class="text-2xl">Vehicle</div> | ||
{{ vehicle }} | ||
</div> | ||
</template> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as alt from 'alt-client'; | ||
import { Vehicle } from '../../shared/types/vehicle.js'; | ||
import { Character } from '@Shared/types/character.js'; | ||
|
||
export function useStreamSyncedGetter() { | ||
/** | ||
* Get synced data from `streamedSyncedBinder` if it is available for a given vehicle | ||
* | ||
* @param {alt.Vehicle} vehicle | ||
* @return | ||
*/ | ||
function vehicle(vehicle: alt.Vehicle) { | ||
function has<K extends keyof Vehicle>(key: K): boolean { | ||
return vehicle.hasStreamSyncedMeta(key); | ||
} | ||
|
||
function get<K extends keyof Vehicle>(key: K): Vehicle[K] { | ||
return vehicle.getStreamSyncedMeta(key) as Vehicle[K]; | ||
} | ||
|
||
return { | ||
has, | ||
get, | ||
}; | ||
} | ||
|
||
/** | ||
* Get synced data from `streamedSyncedBinder` if it is available for a given player | ||
* | ||
* @param {alt.Player} player | ||
* @return | ||
*/ | ||
function player(player: alt.Player) { | ||
function has<K extends keyof Character>(key: K): boolean { | ||
return player.hasStreamSyncedMeta(key); | ||
} | ||
|
||
function get<K extends keyof Character>(key: K): Character[K] { | ||
return player.getStreamSyncedMeta(key) as Character[K]; | ||
} | ||
|
||
return { | ||
has, | ||
get, | ||
}; | ||
} | ||
|
||
return { | ||
player, | ||
vehicle, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.