github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/integration/messagebus/glue/client/src/channel.js (about)

     1  /*
     2   *  Glue - Robust Go and Javascript Socket Library
     3   *  Copyright (C) 2015  Roland Singer <roland.singer[at]desertbit.com>
     4   *
     5   *  This program is free software: you can redistribute it and/or modify
     6   *  it under the terms of the GNU General Public License as published by
     7   *  the Free Software Foundation, either version 3 of the License, or
     8   *  (at your option) any later version.
     9   *
    10   *  This program is distributed in the hope that it will be useful,
    11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   *  GNU General Public License for more details.
    14   *
    15   *  You should have received a copy of the GNU General Public License
    16   *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   */
    18  
    19  /*
    20   *  This code lives inside the glue function.
    21   */
    22  
    23  var channel = (function() {
    24      /*
    25       * Variables
    26       */
    27  
    28       var instance = {}, // Our public instance object returned by this function.
    29           channels = {}; // Object as key value map.
    30  
    31  
    32  
    33       /*
    34        * Private Methods
    35        */
    36  
    37       var newChannel = function(name) {
    38           // Create the channel object.
    39           var channel = {
    40               // Set to a dummy function.
    41               onMessageFunc: function() {}
    42           };
    43  
    44           // Set the channel public instance object.
    45           // This is the value which is returned by the public glue.channel(...) function.
    46           channel.instance = {
    47               // onMessage sets the function which is triggered as soon as a message is received.
    48               onMessage: function(f) {
    49                   channel.onMessageFunc = f;
    50               },
    51  
    52               // send a data string to the channel.
    53               // One optional discard callback can be passed.
    54               // It is called if the data could not be send to the server.
    55               // The data is passed as first argument to the discard callback.
    56               // returns:
    57               //  1 if immediately send,
    58               //  0 if added to the send queue and
    59               //  -1 if discarded.
    60               send: function(data, discardCallback) {
    61                   // Discard empty data.
    62                   if (!data) {
    63                       return -1;
    64                   }
    65  
    66                   // Call the helper method and send the data to the channel.
    67                   return sendBuffered(Commands.ChannelData, utils.marshalValues(name, data), discardCallback);
    68               }
    69           };
    70  
    71           // Return the channel object.
    72           return channel;
    73       };
    74  
    75  
    76  
    77       /*
    78        * Public Methods
    79        */
    80  
    81       // Get or create a channel if it does not exists.
    82       instance.get = function(name) {
    83           if (!name) {
    84               return false;
    85           }
    86  
    87           // Get the channel.
    88           var c = channels[name];
    89           if (c) {
    90               return c.instance;
    91           }
    92  
    93           // Create a new one, if it does not exists and add it to the map.
    94           c = newChannel(name);
    95           channels[name] = c;
    96  
    97           return c.instance;
    98       };
    99  
   100       instance.emitOnMessage = function(name, data) {
   101           if (!name || !data) {
   102               return;
   103           }
   104  
   105           // Get the channel.
   106           var c = channels[name];
   107           if (!c) {
   108               console.log("glue: channel '" + name + "': emit onMessage event: channel does not exists");
   109               return;
   110           }
   111  
   112           // Call the channel's on message event.
   113           try {
   114               c.onMessageFunc(data);
   115           }
   116           catch(err) {
   117               console.log("glue: channel '" + name + "': onMessage event call failed: " + err.message);
   118               return;
   119           }
   120       };
   121  
   122       return instance;
   123  })();