@overextended/ox_lib
Version:
JS/TS wrapper for ox_lib exports
31 lines (30 loc) • 1.19 kB
JavaScript
import { cache } from '../../cache';
const allowStateBagReplication = cache.game === 'fxserver' || !GetConvarBool('sv_stateBagStrictMode', false);
export class StateBag {
statebag;
constructor(statebag = '') {
this.statebag = statebag;
}
/** Writes a value to the statebag. Replicated values set from the client are send to the server for validation. */
async set(key, value, replicated = false) {
if (replicated && !allowStateBagReplication) {
return import('../../../client/callback').then((m) => m.triggerServerCallback('ox_lib:requestSetStateBag', null, this.statebag, key, value));
}
// @ts-ignore
const packed = msgpack_pack(value);
SetStateBagValue(this.statebag, key, packed, packed.length, replicated);
return true;
}
/** Returns a value from the statebag. */
get(key) {
return GetStateBagValue(this.statebag, key);
}
/** Returns if a key exists on the statebag. */
has(key) {
return !!StateBagHasKey(this.statebag, key);
}
/** Returns an array of all keys on the statebag. */
keys() {
return GetStateBagKeys(this.statebag);
}
}