From bc265e5b8a507bacd089fef7be2b8edae3ad70ca Mon Sep 17 00:00:00 2001 From: Stuyk Date: Sun, 2 Jul 2023 11:17:55 -0600 Subject: [PATCH] Change onSync Handler --- CHANGELOG.md | 5 +++++ package.json | 2 +- src/events/index.ts | 2 +- src/events/onSync.ts | 52 +++++++++----------------------------------- 4 files changed, 17 insertions(+), 44 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6389c00..038c368 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/package.json b/package.json index 720e356..7514bdc 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/events/index.ts b/src/events/index.ts index 8028f98..bf791ed 100644 --- a/src/events/index.ts +++ b/src/events/index.ts @@ -1,2 +1,2 @@ export { onKeyChange } from './keyChange.js'; -export * as onSync from './onSync.js'; +export { onSync } from './onSync.js'; diff --git a/src/events/onSync.ts b/src/events/onSync.ts index 1d1e759..a8b03f3 100644 --- a/src/events/onSync.ts +++ b/src/events/onSync.ts @@ -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); }