From 34c5a45054866bfa4fdb4f62e20508627bdd9065 Mon Sep 17 00:00:00 2001 From: Andrey Date: Sun, 24 May 2026 11:45:03 +0500 Subject: [PATCH] chore: JSDoc for new hooks --- src/core/game.ts | 41 +++++++++++++++++++++++--- src/interfaces/store/global-store.ts | 6 ++++ src/store/global-store.ts | 7 +++-- src/types/hooks/state-callback.type.ts | 14 +++++++++ 4 files changed, 62 insertions(+), 6 deletions(-) diff --git a/src/core/game.ts b/src/core/game.ts index bdf8501..3d53a4c 100644 --- a/src/core/game.ts +++ b/src/core/game.ts @@ -1,5 +1,17 @@ import { CommandType, FactoryKeys, GameEvent } from "@enums"; -import type { IGame, IGameOptions, IEventInfo, ISnapshot, ICommand, IInitGameOptions, IPlugin, IGlobalStateChangedData, IObjectDeletedOrCreatedData, IEntityTagsChangedData, ICommandBlocked } from "@interfaces"; +import type { + IGame, + IGameOptions, + IEventInfo, + ISnapshot, + ICommand, + IInitGameOptions, + IPlugin, + IGlobalStateChangedData, + IObjectDeletedOrCreatedData, + IEntityTagsChangedData, + ICommandBlocked +} from "@interfaces"; import { EntityManager, UndoManager } from "@core"; import type { EventCallback, @@ -22,8 +34,29 @@ import type { import { BASE_FPS, BASE_MAX_COMMAND_EXECUTING_ON_TICK_LIMIT, isServer } from "@const"; import { BluePrintsFactory, EffectFactory, IteractionsFactory, QuestsFactory, SoundsFactory } from "@factories"; import { GlobalStore } from "@store"; -import { baseChecksMiddleware, DropItemGuard, EntityInteractGuard, EquipItemGuard, MovementGuard, OpenChestGuard, PickUpGuard, ShootGuard, UseItemGuard } from "@middlewares"; -import { createPluginProto, extractMethodFromPlugin, extractPropertyFromPlugin, useLink, useValidation, useVisibility, useAttack, useAsyncState, useServer, useState } from "@utils"; +import { + baseChecksMiddleware, + DropItemGuard, + EntityInteractGuard, + EquipItemGuard, + MovementGuard, + OpenChestGuard, + PickUpGuard, + ShootGuard, + UseItemGuard +} from "@middlewares"; +import { + createPluginProto, + extractMethodFromPlugin, + extractPropertyFromPlugin, + useLink, + useValidation, + useVisibility, + useAttack, + useAsyncState, + useServer, + useState +} from "@utils"; import type { Entity, GameObject } from "@world"; import { ConflictResolverPlugin } from "@plugins"; @@ -467,7 +500,7 @@ export class Game implements IGame { const snapshot = { entities: Array.from(this.options.manager.entities.values()).map((e) => e.toDTO()), objects: Array.from(this.options.map.objects.values()).map((o) => o.toDTO()), - state: Object.fromEntries(this.options.store.state) + state: this.options.store.getAll() } if (cb) cb(snapshot) diff --git a/src/interfaces/store/global-store.ts b/src/interfaces/store/global-store.ts index 71a8232..56ad556 100644 --- a/src/interfaces/store/global-store.ts +++ b/src/interfaces/store/global-store.ts @@ -16,6 +16,12 @@ export interface IGlobalStore { * @returns { T | undefined } - Data if founded, else undefined */ readonly get: (key: string) => T | undefined; + + /** + * Get all global state key-value + * @returns { CommandContext } - Key-value state + */ + readonly getAll: () => CommandContext; } export interface IGlobalStoreOptions { diff --git a/src/store/global-store.ts b/src/store/global-store.ts index cf719d7..9596fd8 100644 --- a/src/store/global-store.ts +++ b/src/store/global-store.ts @@ -1,8 +1,7 @@ import type { IGlobalStateChangedData, IGlobalStore, IGlobalStoreOptions } from "@interfaces"; export class GlobalStore implements IGlobalStore { - public state = new Map() - + private readonly state = new Map() private readonly options: IGlobalStoreOptions; public constructor(options: IGlobalStoreOptions) { @@ -36,4 +35,8 @@ export class GlobalStore implements IGlobalStore { public get(key: string) { return this.state.get(key) as T } + + public getAll() { + return Object.fromEntries(this.state.entries()) + } } \ No newline at end of file diff --git a/src/types/hooks/state-callback.type.ts b/src/types/hooks/state-callback.type.ts index 8905b25..adf14ae 100644 --- a/src/types/hooks/state-callback.type.ts +++ b/src/types/hooks/state-callback.type.ts @@ -1,3 +1,17 @@ +/** + * Type defines callback with preview state, returns new state + */ export type StateCallback = (state: T) => T + +/** + * State setter callback type, provide prev state into callback, + * accept new state + */ export type StateSetterCallback = (cb: StateCallback) => T + +/** + * State type, returns array of object with current value state, + * function to update state, + * function to unSub from state + */ export type State = [{ readonly value: T }, ((value: T) => T) & StateSetterCallback, VoidFunction] \ No newline at end of file