mirror of
https://github.com/moodle/moodle.git
synced 2025-01-19 14:27:22 +01:00
1 line
2.8 KiB
Plaintext
1 line
2.8 KiB
Plaintext
{"version":3,"file":"pubsub.min.js","sources":["../src/pubsub.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * A simple Javascript PubSub implementation.\n *\n * @module core/pubsub\n * @copyright 2018 Ryan Wyllie <ryan@moodle.com>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\nimport Pending from 'core/pending';\n\nconst events = {};\n\n/**\n * Subscribe to an event.\n *\n * @param {string} eventName The name of the event to subscribe to.\n * @param {function} callback The callback function to run when eventName occurs.\n */\nexport const subscribe = function(eventName, callback) {\n events[eventName] = events[eventName] || [];\n events[eventName].push(callback);\n};\n\n/**\n * Unsubscribe from an event.\n *\n * @param {string} eventName The name of the event to unsubscribe from.\n * @param {function} callback The callback to unsubscribe.\n */\nexport const unsubscribe = function(eventName, callback) {\n if (events[eventName]) {\n for (var i = 0; i < events[eventName].length; i++) {\n if (events[eventName][i] === callback) {\n events[eventName].splice(i, 1);\n break;\n }\n }\n }\n};\n\n/**\n * Publish an event to all subscribers.\n *\n * @param {string} eventName The name of the event to publish.\n * @param {any} data The data to provide to the subscribed callbacks.\n */\nexport const publish = function(eventName, data) {\n const pendingPromise = new Pending(\"Publishing \" + eventName);\n if (events[eventName]) {\n events[eventName].forEach(function(callback) {\n callback(data);\n });\n }\n pendingPromise.resolve();\n};\n"],"names":["events","eventName","callback","push","i","length","splice","data","pendingPromise","Pending","forEach","resolve"],"mappings":";;;;;;;6LAwBMA,OAAS,sBAQU,SAASC,UAAWC,UACzCF,OAAOC,WAAaD,OAAOC,YAAc,GACzCD,OAAOC,WAAWE,KAAKD,gCASA,SAASD,UAAWC,aACvCF,OAAOC,eACF,IAAIG,EAAI,EAAGA,EAAIJ,OAAOC,WAAWI,OAAQD,OACtCJ,OAAOC,WAAWG,KAAOF,SAAU,CACnCF,OAAOC,WAAWK,OAAOF,EAAG,4BAarB,SAASH,UAAWM,YACjCC,eAAiB,IAAIC,iBAAQ,cAAgBR,WAC/CD,OAAOC,YACPD,OAAOC,WAAWS,SAAQ,SAASR,UAC/BA,SAASK,SAGjBC,eAAeG"} |