JSFiddle - React, Tailwind, and code Playground

by seong gyun jeon

JavaScript

var Observer = {
    
    EVENTID : 0,
    listeners: {},
    addEvent : function(type, hnd){

        if(!this.listeners[type]) this.listeners[type] = {};

        var eventId = Observer.EVENTID++;
        this.listeners[type][eventId] = hnd;

        return eventId;
    },

    fireEvent : function(type){

        var handlers = this.listeners[type], eventId,
            args =  Array.prototype.slice.call(arguments);
        if(handlers.stop) return false;

        args.shift();
        for(eventId in handlers) if(handlers.hasOwnProperty(eventId)){
            if(eventId !== "stop"){
                if(!handlers[eventId].stop){
                    handlers[eventId].apply(this, args);
                }
            }
        };
    },

    removeEvent : function(type, hnd){

        if(!this.listeners[type]) return -1;

        var handlers = this.listeners[type], eventId = -1;
        if(typeof hnd === "function"){
            for(eventId in handlers) if(handlers.hasOwnProperty(f)){
                if(handlers[eventId] === hnd){
                    delete handlers[eventId];
                    break;
                }
            };
            return !handlers[eventId];
        }else{
            if(handlers[hnd]) delete handlers[hnd]
            return !handlers[hnd];
        };
    },

    stopEvent : function(type, hnd){

        if(!this.listeners[type]) return -1;
        var handlers = this.listeners[type], eventId = -1;
        if(hnd){
            if(typeof hnd === "function"){
                for(eventId in handlers) if(handlers.hasOwnProperty(f)){
                    if(handlers[eventId] === hnd){
                        handlers[eventId].stop = true;
                    }
                };
            }else{
                handlers[hnd].stop = true;
            }
        }else{
            handlers.stop = true;
        }
    },

    restoreEvent : function(type, hnd){

        if(!this.listeners[type]) return -1;

        var handlers =...