blob: f2528613339877a6d7876aecd2526887531e6c16 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview Generic callback manager.
*/
goog.provide('devtools.Callback');
/**
* Generic callback support as a singleton object.
* @constructor
*/
devtools.Callback = function() {
this.lastCallbackId_ = 1;
this.callbacks_ = {};
};
/**
* Assigns id to a callback.
* @param {Function} callback Callback to assign id to.
* @return {number} Callback id.
*/
devtools.Callback.prototype.wrap = function(callback) {
var callbackId = this.lastCallbackId_++;
this.callbacks_[callbackId] = callback || function() {};
return callbackId;
};
/**
* Executes callback with the given id.
* @param {callbackId} callbackId Id of a callback to call.
*/
devtools.Callback.prototype.processCallback = function(callbackId,
opt_vararg) {
var args = Array.prototype.slice.call(arguments, 1);
var callback = this.callbacks_[callbackId];
callback.apply(null, args);
delete this.callbacks_[callbackId];
};
/**
* @type {devtools.Callback} Callback support singleton.
* @private
*/
devtools.Callback.INSTANCE_ = new devtools.Callback();
devtools.Callback.wrap = goog.bind(
devtools.Callback.INSTANCE_.wrap,
devtools.Callback.INSTANCE_);
devtools.Callback.processCallback = goog.bind(
devtools.Callback.INSTANCE_.processCallback,
devtools.Callback.INSTANCE_);
|