package com.movesdk /** * Holds the newest payload of each event emitted before JS subscribed, so it can * be replayed once a subscriber appears. * * `MoveSdk.init()` runs from `Application.onCreate`, so the SDK's first * callbacks fire before the React instance exists; without this they are dropped * with no redelivery. * * Only the newest payload per event is kept — these carry current state, not * history, so a late subscriber wants the latest value, not a backlog. */ internal class PendingEventBuffer { // Guarded by `lock`: the SDK invokes listeners from its own coroutine // dispatchers, while replay happens on the JS thread. private val lock = Any() private val pending = mutableMapOf() private val subscribed = mutableSetOf() fun record(eventName: String, payload: Any) { synchronized(lock) { pending[eventName] = payload } } /** Returns the buffered payload for [eventName], removing it. */ fun takePending(eventName: String): Any? { return synchronized(lock) { pending.remove(eventName) } } /** Whether JS has ever subscribed to [eventName]; after that, emits go straight through. */ fun isSubscribed(eventName: String): Boolean { return synchronized(lock) { subscribed.contains(eventName) } } /** * Marks [eventName] subscribed and returns the payload it missed, if any. * * Sticky, because `removeListeners` reports only a count and never which * event, so there is no sound way to resume buffering. */ fun markSubscribed(eventName: String): Any? { return synchronized(lock) { subscribed.add(eventName) pending.remove(eventName) } } }