21 lines
1.0 KiB
TypeScript
21 lines
1.0 KiB
TypeScript
/** Local presentation state for messages which need a player's acknowledgement. */
|
|
export type ActionableNotice = { id: number; text: string };
|
|
export type ActionableNoticeQueue = { nextId: number; items: ActionableNotice[] };
|
|
export type ActionableNoticeUpdate =
|
|
| { type: 'enqueue'; text: string }
|
|
| { type: 'dismiss'; id: number };
|
|
|
|
export const initialActionableNotices: ActionableNoticeQueue = { nextId: 1, items: [] };
|
|
|
|
/** Repeated identical messages coalesce while pending; a stale button cannot
|
|
* dismiss a later message, including another occurrence of the same text. */
|
|
export function updateActionableNotices(
|
|
state: ActionableNoticeQueue,
|
|
update: ActionableNoticeUpdate,
|
|
): ActionableNoticeQueue {
|
|
if (update.type === 'dismiss') return state.items[0]?.id === update.id
|
|
? { ...state, items: state.items.slice(1) } : state;
|
|
if (!update.text || state.items.some(item => item.text === update.text)) return state;
|
|
return { nextId: state.nextId + 1, items: [...state.items, { id: state.nextId, text: update.text }] };
|
|
}
|