#include "model_internal.hpp" namespace ps { void append_record(Impl &state, int kind, int device, int actor, std::uint64_t token, std::uint64_t generation) { Record record; record.sequence = state.next_record++; record.kind = kind; record.device = device; record.actor = actor; record.token = token; record.generation = generation; state.records.push_back(record); if (state.records.size() > 128) { state.records.pop_front(); } } void publish(Impl &state, int device, bool removed) { auto &entry = state.devices[device]; ++entry.generation; append_record(state, 1, device, -1, 0, entry.generation); auto listeners = entry.listeners; for (int actor : listeners) { auto &watch = state.actors[actor].view; ++watch.wakes; watch.pending = entry.generation; append_record(state, 2, device, actor, watch.token, entry.generation); if (removed) { watch.phase = GONE; watch.ready = 0; unsubscribe(state, actor); } } } int Model::journal(std::uint64_t cursor, int limit, std::vector &out, std::uint64_t &next) const { if (limit < 0 || limit > 32) { return -22; } std::uint64_t oldest = state->records.empty() ? state->next_record : state->records.front().sequence; std::uint64_t start = cursor == 0 ? oldest : cursor; if (start < oldest) { return -75; } if (start > state->next_record) { return -22; } std::vector result; for (const auto &record : state->records) { if (record.sequence < start) { continue; } if (static_cast(result.size()) == limit) { break; } result.push_back(record); } next = start + result.size(); out = result; return 0; } }