JAVASCRIPT 15
Forward.js Guest on 6th April 2021 03:21:01 AM
  1. // Forwarding callback utility, for callbacks that are in evaled script.
  2. //
  3. // Usage:
  4. // var cookie = forward.register(function() { alert('eval done!'); });
  5. // document.write('forward.oneshot(' + cookie + ');');
  6. //
  7. // oneshot can be called once only with a given cookie.
  8. // notify can can be called repeatedly with the same cookie.
  9. // remove can be used to explicitly remove a cookie.
  10.  
  11. var forward = {
  12.   map: {},
  13.   counter: 0,
  14.  
  15.   register: function register(f) {
  16.     forward.map[forward.counter] = f;
  17.     return forward.counter++;
  18.   },
  19.  
  20.   oneshot: function oneshot(n) {
  21.     var f = forward.map[n];
  22.     if (f) {
  23.       delete forward.map[n];
  24.       if (arguments.length == 2) {
  25.         f(arguments[1]);
  26.       } else {
  27.         f();
  28.       }
  29.     }
  30.   },
  31.  
  32.   notify: function notify(n) {
  33.     var f = forward.map[n];
  34.     if (f) {
  35.       if (arguments.length == 2) {
  36.         f(arguments[1]);
  37.       } else {
  38.         f();
  39.       }
  40.     }
  41.   },
  42.  
  43.   remove: function remove(n) {
  44.     delete forward.map[n];
  45.   }
  46. }

Paste-bin is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.