Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/utils/hooks/use-alternative.hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import { AsyncPlugin } from "@plugins";
/**
* This hook allows you to client-side prediction command
* Command will be executed in alternative game
* @param command - Command to execute in alternative world
* @param command - Command(s) to execute in alternative world
* @param core - Game reference if hydration disabled
* @returns { Promise<Game> } - Alternative isolated game (not affect real)
*/
export async function useAlternative(command: ICommand, core?: Game): Promise<Game> {
export async function useAlternative<T = any>(command: ICommand<T> | ICommand<T>[], core?: Game): Promise<Game> {
const game = useAlternative.prototype.game as Game || core

const asyncPlugin = new AsyncPlugin()
const alternative = Game.fromSnapshot(game.save(), alternative => alternative.registerPlugin(asyncPlugin))

await asyncPlugin.asyncExecute(command)
if (!Array.isArray(command)) await asyncPlugin.asyncExecute(command)
else for (const cmd of command) await asyncPlugin.asyncExecute(cmd)

return alternative
}
Loading