// 全局状态:登录、宠物列表、当前宠物;服务端驱动 + 发布订阅 const api = require('./api.js'); const state = { user: null, pets: [], currentId: 0, }; const listeners = new Set(); let bootstrapPromise = null; function getPets() { return state.pets; } function getPet() { return state.pets.find((p) => p.id === state.currentId) || state.pets[0] || {}; } function currentPetId() { const p = getPet(); return p && p.id; } function notify() { const pet = getPet(); listeners.forEach((fn) => { try { fn(pet); } catch (e) { /* noop */ } }); } function subscribe(fn) { listeners.add(fn); return () => listeners.delete(fn); } // 登录 + 拉宠物列表(只跑一次) function ready() { if (!bootstrapPromise) { bootstrapPromise = (async () => { const res = await api.login(); state.user = res.user; await loadPets(); })().catch((e) => { bootstrapPromise = null; // 允许重试 throw e; }); } return bootstrapPromise; } async function loadPets() { const pets = await api.getPets(); state.pets = pets || []; if (!state.currentId && state.pets[0]) { state.currentId = state.pets[0].id; } // 当前宠物被删或不存在时回退第一只 if (!state.pets.find((p) => p.id === state.currentId) && state.pets[0]) { state.currentId = state.pets[0].id; } notify(); return state.pets; } function switchPet(id) { if (state.currentId === id) return; state.currentId = id; notify(); } // 本地更新当前宠物字段(服务端已改动后同步 UI) function patchCurrent(patch) { const pet = getPet(); Object.assign(pet, patch); notify(); } function setUser(u) { state.user = u; } function getUser() { return state.user; } module.exports = { ready, loadPets, getPets, getPet, currentPetId, switchPet, patchCurrent, subscribe, setUser, getUser, };