Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.sxam.core.SXAMEventSource;
- import java.util.Map;
- import java.util.concurrent.ConcurrentHashMap;
- import org.sxam.core.enums.SXAMEvent;
- import org.sxam.osrs.bot.structure.enums.LOG_LEVEL;
- import org.sxam.osrs.utils.Logger;
- /**
- * @author sxam
- * @version 2.00
- * @since 02/05/2021
- * A Observer pattern (publish/subscribe) implemented for pushing out SXAMEvents
- * to willing subscribers, used for spawning PLATFORM and UI events thus far.
- *
- * For the use of anonymous in-line method support for lazy programmers, we
- * implemented the methods that will be
- * automatically removed from the event queue during SXAMPlatform 'pause' tier
- * events.
- *
- * Since I can appreciate that some programmers would perfer a simplier syntax when writing
- * their scripts, this option provides a means to clean up your anonymous Observers (ex:
- *
- * SXAMEventSourceSingleton.Instance.subscribe(SXAMEvent.UI_CHAT_MESSAGE_EVENT,
- * (update) -> {
- * SXAMEventChatMessage chatMessageUpdate = (SXAMEventChatMessage) update;
- * // some work based on the message
- * }, true
- * );
- * )
- * This saves time when writing Observers, rather than a fully extended class definition,
- * we can provide easier to reproduce and write method without the hassel of
- * pausing and cleaning.
- *
- *
- * @see org.sxam.osrs.ui.core.structure.SXAMUI.java For example fireEvents
- * @see org.ItemDisplayItem.sxam.osrs.ui.core.ItemDisplayHelper.java For example subscribe/unsubscribe
- * @see org.sxam.core.SXAMEventSource.SXAMRenderSourceSingleton.java For the UI render event.
- */
- public class SXAMEventSourceSingleton {
- // access this API via SXAMEventSourceSingleton.Instance
- public static SXAMEventSourceSingleton Instance = new SXAMEventSourceSingleton();
- private SXAMEventSourceSingleton() {};
- public interface Observer {
- void update(Object event);
- }
- private final Map<Observer, SXAMEvent> _OBSERVERS = new ConcurrentHashMap<Observer, SXAMEvent>(64);
- private final Map<Observer, SXAMEvent> _AUTO_PAUSE = new ConcurrentHashMap<Observer, SXAMEvent>(64);
- /**
- * Typical subscribe method, use this if you have a formal definition (full class) extension
- * of an Observer, you'll need to hook in to the platform specific 'pause' routine that your
- * events are closed by, then manually unsubscribe your class objects.
- *
- * @see unsubscribe
- * @param event The SXAMEvent that your observer will be updated upon.
- * @param observer The formally defined extended Observer object which contains
- * a single method: 'update' which consumes an update object relvant
- * to the supplied SXAMEvent.
- */
- public void subscribe(SXAMEvent event, Observer observer) {
- Logger.Log(LOG_LEVEL.DEVELOPMENT_INFO, String.format("SXAMEventSource: An Observer subscribed to the [%s] event", event.getEventTypeName()));
- this._OBSERVERS.put(observer, event);
- }
- /**
- * Supply the SXAMEvent and an anonymous Observer to consume it's updates,
- * withPausing should be set to true for allowing us to automatically control the cleanup
- * of this Observer.
- *
- * @param event The SXAMEvent that your observer will be updated upon.
- * @param observer The formally defined extended Observer object which contains
- * a single method: 'update' which consumes an update object relvant
- * to the supplied SXAMEvent.
- * @param withPausing Supply true for allowing SXAM to automatically pause and destroy
- * your anonymous Observer whenever the platform 'pause'
- * or 'stop' events respectively occurs.
- */
- public void subscribe(SXAMEvent event, Observer observer, boolean withPausing) {
- Logger.Log(LOG_LEVEL.DEVELOPMENT_INFO, String.format("SXAMEventSource: An Observer subscribed via automatic pause to the [%s] event", event.getEventTypeName()));
- this._OBSERVERS.put(observer, event);
- if (withPausing) { this._AUTO_PAUSE.put(observer, event); }
- }
- /**
- *
- * @param event
- * @param observer
- */
- public void unsubscribe(SXAMEvent event, Observer observer) {
- Logger.Log(LOG_LEVEL.DEVELOPMENT_INFO, String.format("SXAMEventSource: An Observer unsubscribed from the [%s] Event", event.getEventTypeName()));
- this._OBSERVERS.remove(observer, event);
- }
- /**
- *
- * @param newEventType
- * @param newEventObject
- */
- public void publish(SXAMEvent newEventType, Object newEventObject) {
- this._OBSERVERS.forEach((obs, evType) -> {
- if (evType.getEventTypeName().equals(newEventType.getEventTypeName())) {
- obs.update(newEventObject);
- }
- });
- Logger.Log(LOG_LEVEL.DEVELOPMENT_INFO, String.format("SXAMEventSource: Published event [%s] to Observers. EventObject class: [%s]", newEventType.getEventTypeName(), newEventObject.getClass().getName()));
- }
- /**
- * Via the use of subscribe(observer, eventType, boolean) method, observers will be
- * enumerated and removed from the Observer Event list on platform wide 'pause' events,
- * this assists in allowing programmers to be lazy.
- *
- * @param continueExecution True to add the Observers back into the Observer Event list.
- * False to remove the Observers from the Observer Event list.
- */
- public void pause(boolean continueExecution) {
- if (continueExecution) {
- this._AUTO_PAUSE.forEach((obs, evType) -> {
- this._OBSERVERS.put(obs, evType);
- });
- } else {
- this._AUTO_PAUSE.forEach((obs, evType) -> {
- this._OBSERVERS.remove(obs, evType);
- });
- }
- }
- /**
- * When the platform fires a 'stop' event, the EventSource will clear the observers in
- * the Observer Event list & the Observer Pausing list.
- */
- public void clean() {
- this.pause(false);
- this._AUTO_PAUSE.clear();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment