laya-snake / src /web /js /gameState.js
deepondas's picture
Add obstacles, multiple foods and a real-time 3D (Three.js) view
38cd66a
Raw History Blame Contribute Delete
536 Bytes
const listeners = new Set();
// The latest server snapshot (unchanged payload) and the one before it.
export const game = { state: null, previous: null };
// Stores a fresh server snapshot and notifies every subscriber.
export function applyState(s) {
game.previous = game.state;
game.state = s;
for (const fn of listeners) fn(s, game.previous);
}
// Registers a callback run after every state change; returns an unsubscribe function.
export function subscribe(fn) {
listeners.add(fn);
return () => listeners.delete(fn);
}