Skip to content

Commit

Permalink
Change onSync Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Stuyk committed Jul 2, 2023
1 parent 4b3d488 commit bc265e5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 44 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 1.0.3

- Simplify `onSync` logic to just pass any entity.
- Allows for more control over what to do with entity data when onSync is called.

# 1.0.2

- Fixed bug where specifying `_id` would not convert it to ObjectID for usage.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stuyk/cross-resource-cache",
"version": "1.0.2",
"version": "1.0.3",
"description": "add mongodb database saving to cross resource environments for altv",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/events/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { onKeyChange } from './keyChange.js';
export * as onSync from './onSync.js';
export { onSync } from './onSync.js';
52 changes: 10 additions & 42 deletions src/events/onSync.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,19 @@
import * as alt from 'alt-server';

const callbacks: { [key: string]: Array<(entity: alt.Entity) => void> } = {};

function bindCallback(type: string, callback: (entity: alt.Entity) => void) {
if (!callbacks[type]) {
callbacks[type] = [];
}

callbacks[type].push(callback);
}
const callbacks: Array<(entity: alt.Entity) => void> = [];

export function invoke(entity: alt.Entity) {
const type = entity.constructor.name.toLowerCase();
const availableCallbacks = callbacks[type];
if (!availableCallbacks) {
return;
}

for (let callback of availableCallbacks) {
for (let callback of callbacks) {
callback(entity);
}
}

export function onPlayer(callback: (entity: alt.Entity) => void) {
bindCallback('player', callback);
}

export function onVehicle(callback: (entity: alt.Entity) => void) {
bindCallback('vehicle', callback);
}

export function onColshape(callback: (entity: alt.Entity) => void) {
bindCallback('colshape', callback);
}

export function onCheckpoint(callback: (entity: alt.Entity) => void) {
bindCallback('checkpoint', callback);
}

export function onPed(callback: (entity: alt.Entity) => void) {
bindCallback('ped', callback);
}

export function onObject(callback: (entity: alt.Entity) => void) {
bindCallback('object', callback);
}

export function onMarker(callback: (entity: alt.Entity) => void) {
bindCallback('marker', callback);
/**
* Calls when an entity has `crc.data.sync` invoked on it.
*
* @export
* @param {(entity: alt.Entity) => void} callback
*/
export function onSync(callback: (entity: alt.Entity) => void) {
callbacks.push(callback);
}

0 comments on commit bc265e5

Please sign in to comment.