PageSourceSearch

https://app.voxingpro.com/packages/callback-hook.js?hash=8bdf3b5506fd2197bffa0db605fa422b9022fd10

js voxingpro.com collected 2026-10-02 14:38:39 UTC 7,437 bytes, 212 lines download raw bytes

1//////////////////////////////////////////////////////////////////////////
2//                                                                      //
3// This is a generated file. You can view the original                  //
4// source in your browser if your browser supports source maps.         //
5// Source maps are supported by all recent versions of Chrome, Safari,  //
6// and Firefox, and by Internet Explorer 11.                            //
7//                                                                      //
8//////////////////////////////////////////////////////////////////////////
9
10
11(function () {
12
13/* Imports */
14var Meteor = Package.meteor.Meteor;
15var global = Package.meteor.global;
16var meteorEnv = Package.meteor.meteorEnv;
17var meteorInstall = Package.modules.meteorInstall;
18var meteorBabelHelpers = Package['babel-runtime'].meteorBabelHelpers;
19var Promise = Package.promise.Promise;
20var Symbol = Package['ecmascript-runtime-client'].Symbol;
21var Map = Package['ecmascript-runtime-client'].Map;
22var Set = Package['ecmascript-runtime-client'].Set;
23
24/* Package-scope variables */
25var Hook;
26
27var require = meteorInstall({"node_modules":{"meteor":{"callback-hook":{
27"hook.js":function(require,exports,module){
28
29/////////////////////////////////////////////////////////////////////////////////////////////////////
30//                                                                                                 //
31// packages/callback-hook/hook.js                                                                  //
32//                                                                                                 //
33/////////////////////////////////////////////////////////////////////////////////////////////////////
34                                                                                                   //
35module.export({
36  Hook: function () {
37    return Hook;
38  }
39});
40// XXX This pattern is under development. Do not add more callsites
41// using this package for now. See:
42// https://meteor.hackpad.com/Design-proposal-Hooks-YxvgEW06q6f
43//
44// Encapsulates the pattern of registering callbacks on a hook.
45//
46// The `each` method of the hook calls its iterator function argument
47// with each registered callback.  This allows the hook to
48// conditionally decide not to call the callback (if, for example, the
49// observed object has been closed or terminated).
50//
51// By default, callbacks are bound with `Meteor.bindEnvironment`, so they will be
52// called with the Meteor environment of the calling code that
53// registered the callback. Override by passing { bindEnvironment: false }
54// to the constructor.
55//
56// Registering a callback returns an object with a single `stop`
57// method which unregisters the callback.
58//
59// The code is careful to allow a callback to be safely unregistered
60// while the callbacks are being iterated over.
61//
62// If the hook is configured with the `exceptionHandler` option, the
63// handler will be called if a called callback throws an exception.
64// By default (if the exception handler doesn't itself throw an
65// exception, or if the iterator function doesn't return a falsy value
66// to terminate the calling of callbacks), the remaining callbacks
67// will still be called.
68//
69// Alternatively, the `debugPrintExceptions` option can be specified
70// as string describing the callback.  On an exception the string and
71// the exception will be printed to the console log with
72// `Meteor._debug`, and the exception otherwise ignored.
73//
74// If an exception handler isn't specified, exceptions thrown in the
75// callback will propagate up to the iterator function, and will
76// terminate calling the remaining callbacks if not caught.
77var hasOwn = Object.prototype.hasOwnProperty;
78
79var Hook =
80/*#__PURE__*/
81function () {
82  function Hook(options) {
83    options = options || {};
84    this.nextCallbackId = 0;
85    this.callbacks = Object.create(null); // Whether to wrap callbacks with Meteor.bindEnvironment
86
87    this.bindEnvironment = true;
88
89    if (options.bindEnvironment === false) {
90      this.bindEnvironment = false;
91    }
92
93    if (options.exceptionHandler) {
94      this.exceptionHandler = options.exceptionHandler;
95    } else if (options.debugPrintExceptions) {
96      if (typeof options.debugPrintExceptions !== "string") {
97        throw new Error("Hook option debugPrintExceptions should be a string");
98      }
99
100      this.exceptionHandler = options.debugPrintExceptions;
101    }
102  }
103
104  var _proto = Hook.prototype;
105
106  _proto.register = function () {
107    function register(callback) {
108      var _this2 = this;
109
110      var exceptionHandler = this.exceptionHandler || function (exception) {
111        // Note: this relies on the undocumented fact that if bindEnvironment's
112        // onException throws, and you are invoking the callback either in the
113        // browser or from within a Fiber in Node, the exception is propagated.
114        throw exception;
115      };
116
117      if (this.bindEnvironment) {
118        callback = Meteor.bindEnvironment(callback, exceptionHandler);
119      } else {
120        callback = dontBindEnvironment(callback, exceptionHandler);
121      }
122
123      var id = this.nextCallbackId++;
124      this.callbacks[id] = callback;
125      return {
126        stop: function () {
127          delete _this2.callbacks[id];
128        }
129      };
130    }
131
132    return register;
133  }(); // For each registered callback, call the passed iterator function
134  // with the callback.
135  //
136  // The iterator function can choose whether or not to call the
137  // callback.  (For example, it might not call the callback if the
138  // observed object has been closed or terminated).
139  //
140  // The iteration is stopped if the iterator function returns a fal
140sy
141  // value or throws an exception.
142  //
143
144
145  _proto.each = function () {
146    function each(iterator) {
147      // Invoking bindEnvironment'd callbacks outside of a Fiber in Node doesn't
148      // run them to completion (and exceptions thrown from onException are not
149      // propagated), so we need to be in a Fiber.
150      Meteor._nodeCodeMustBeInFiber();
151
152      var ids = Object.keys(this.callbacks);
153
154      for (var i = 0; i < ids.length; ++i) {
155        var id = ids[i]; // check to see if the callback was removed during iteration
156
157        if (hasOwn.call(this.callbacks, id)) {
158          var callback = this.callbacks[id];
159
160          if (!iterator(callback)) {
161            break;
162          }
163        }
164      }
165    }
166
167    return each;
168  }();
169
170  return Hook;
171}();
172
173// Copied from Meteor.bindEnvironment and removed all the env stuff.
174function dontBindEnvironment(func, onException, _this) {
175  if (!onException || typeof onException === 'string') {
176    var description = onException || "callback of async function";
177
178    onException = function (error) {
179      Meteor._debug("Exception in " + description + ":", error && error.stack || error);
180    };
181  }
182
183  return function () {
184    try {
185      for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
186        args[_key] = arguments[_key];
187      }
188
189      var ret = func.apply(_this, args);
190    } catch (e) {
191      onException(e);
192    }
193
194    return ret;
195  };
196}
197/////////////////////////////////////////////////////////////////////////////////////////////////////
198
199}
199}}}},{
200  "extensions": [
201    ".js",
202    ".json"
203  ]
204});
205var exports = require("/node_modules/meteor/callback-hook/hook.js");
206
207/* Exports */
208Package._define("callback-hook", exports, {
209  Hook: Hook
210});
211
212})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.