Skip to content

Instantly share code, notes, and snippets.

@Yiin
Last active February 19, 2023 13:14
Show Gist options
  • Save Yiin/1e29cadaca481e79d5ddcab833fe1dda to your computer and use it in GitHub Desktop.
Save Yiin/1e29cadaca481e79d5ddcab833fe1dda to your computer and use it in GitHub Desktop.
declare module "alt-server" {
export interface Player {
store: ReturnType<
typeof import("../../src/shared/store/player.store").usePlayerStore
>;
}
}
this.store.$subscribe(
(mutation, state) => {
if (!mutation.events) {
this.emitRaw(Events.Client.SET_STATE, toRaw(state));
return;
}
const events = Array.isArray(mutation.events)
? mutation.events
: [mutation.events];
for (const event of events) {
const path = findPath(toRaw(state), event.target)?.join(".");
const { type, target, key, newValue } = event;
let payload;
switch (type) {
case "add":
payload = { type, path, target: toRaw(target) };
break;
case "set":
payload = { type, path, key, newValue: toRaw(newValue) };
break;
case "delete":
payload = { type, path, key };
break;
case "clear":
payload = { type, path };
break;
}
if (payload) {
this.emitRaw(Events.Client.UPDATE_STATE, payload);
}
}
},
{ immediate: true, flush: "sync" }
);
import { get, set } from "lodash-es";
import { Store } from "pinia";
export function updateStoreState<S extends Store>(
store: S,
event: { type: string; target: any; key: string; newValue: any; path: string }
) {
const { type, target, key, newValue, path } = event;
switch (type) {
case "add":
if (path) {
set(store.$state, path, target);
}
break;
case "set":
set(store.$state, `${path ? path + "." : ""}${key}`, newValue);
break;
case "delete":
if (path) {
delete get(store.$state, path)[key];
} else {
delete (store.$state as any)[key];
}
break;
case "clear":
if (path) {
get(store.$state, path)?.clear();
}
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment