| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | |
| 5 /** | 4 /** |
| 6 * @constructor | 5 * @unrestricted |
| 7 */ | 6 */ |
| 8 WebInspector.Context = function() | 7 WebInspector.Context = class { |
| 9 { | 8 constructor() { |
| 10 this._flavors = new Map(); | 9 this._flavors = new Map(); |
| 11 this._eventDispatchers = new Map(); | 10 this._eventDispatchers = new Map(); |
| 11 } |
| 12 |
| 13 /** |
| 14 * @param {function(new:T, ...)} flavorType |
| 15 * @param {?T} flavorValue |
| 16 * @template T |
| 17 */ |
| 18 setFlavor(flavorType, flavorValue) { |
| 19 var value = this._flavors.get(flavorType) || null; |
| 20 if (value === flavorValue) |
| 21 return; |
| 22 if (flavorValue) |
| 23 this._flavors.set(flavorType, flavorValue); |
| 24 else |
| 25 this._flavors.remove(flavorType); |
| 26 |
| 27 this._dispatchFlavorChange(flavorType, flavorValue); |
| 28 } |
| 29 |
| 30 /** |
| 31 * @param {function(new:T, ...)} flavorType |
| 32 * @param {?T} flavorValue |
| 33 * @template T |
| 34 */ |
| 35 _dispatchFlavorChange(flavorType, flavorValue) { |
| 36 for (var extension of self.runtime.extensions(WebInspector.ContextFlavorList
ener)) { |
| 37 if (extension.hasContextType(flavorType)) |
| 38 extension.instance().then( |
| 39 instance => /** @type {!WebInspector.ContextFlavorListener} */ (inst
ance).flavorChanged(flavorValue)); |
| 40 } |
| 41 var dispatcher = this._eventDispatchers.get(flavorType); |
| 42 if (!dispatcher) |
| 43 return; |
| 44 dispatcher.dispatchEventToListeners(WebInspector.Context.Events.FlavorChange
d, flavorValue); |
| 45 } |
| 46 |
| 47 /** |
| 48 * @param {function(new:Object, ...)} flavorType |
| 49 * @param {function(!WebInspector.Event)} listener |
| 50 * @param {!Object=} thisObject |
| 51 */ |
| 52 addFlavorChangeListener(flavorType, listener, thisObject) { |
| 53 var dispatcher = this._eventDispatchers.get(flavorType); |
| 54 if (!dispatcher) { |
| 55 dispatcher = new WebInspector.Object(); |
| 56 this._eventDispatchers.set(flavorType, dispatcher); |
| 57 } |
| 58 dispatcher.addEventListener(WebInspector.Context.Events.FlavorChanged, liste
ner, thisObject); |
| 59 } |
| 60 |
| 61 /** |
| 62 * @param {function(new:Object, ...)} flavorType |
| 63 * @param {function(!WebInspector.Event)} listener |
| 64 * @param {!Object=} thisObject |
| 65 */ |
| 66 removeFlavorChangeListener(flavorType, listener, thisObject) { |
| 67 var dispatcher = this._eventDispatchers.get(flavorType); |
| 68 if (!dispatcher) |
| 69 return; |
| 70 dispatcher.removeEventListener(WebInspector.Context.Events.FlavorChanged, li
stener, thisObject); |
| 71 if (!dispatcher.hasEventListeners(WebInspector.Context.Events.FlavorChanged)
) |
| 72 this._eventDispatchers.remove(flavorType); |
| 73 } |
| 74 |
| 75 /** |
| 76 * @param {function(new:T, ...)} flavorType |
| 77 * @return {?T} |
| 78 * @template T |
| 79 */ |
| 80 flavor(flavorType) { |
| 81 return this._flavors.get(flavorType) || null; |
| 82 } |
| 83 |
| 84 /** |
| 85 * @return {!Set.<function(new:Object, ...)>} |
| 86 */ |
| 87 flavors() { |
| 88 return new Set(this._flavors.keys()); |
| 89 } |
| 90 |
| 91 /** |
| 92 * @param {!Array.<!Runtime.Extension>} extensions |
| 93 * @return {!Set.<!Runtime.Extension>} |
| 94 */ |
| 95 applicableExtensions(extensions) { |
| 96 var targetExtensionSet = new Set(); |
| 97 |
| 98 var availableFlavors = this.flavors(); |
| 99 extensions.forEach(function(extension) { |
| 100 if (self.runtime.isExtensionApplicableToContextTypes(extension, availableF
lavors)) |
| 101 targetExtensionSet.add(extension); |
| 102 }); |
| 103 |
| 104 return targetExtensionSet; |
| 105 } |
| 12 }; | 106 }; |
| 13 | 107 |
| 14 /** @enum {symbol} */ | 108 /** @enum {symbol} */ |
| 15 WebInspector.Context.Events = { | 109 WebInspector.Context.Events = { |
| 16 FlavorChanged: Symbol("FlavorChanged") | 110 FlavorChanged: Symbol('FlavorChanged') |
| 17 }; | |
| 18 | |
| 19 WebInspector.Context.prototype = { | |
| 20 /** | |
| 21 * @param {function(new:T, ...)} flavorType | |
| 22 * @param {?T} flavorValue | |
| 23 * @template T | |
| 24 */ | |
| 25 setFlavor: function(flavorType, flavorValue) | |
| 26 { | |
| 27 var value = this._flavors.get(flavorType) || null; | |
| 28 if (value === flavorValue) | |
| 29 return; | |
| 30 if (flavorValue) | |
| 31 this._flavors.set(flavorType, flavorValue); | |
| 32 else | |
| 33 this._flavors.remove(flavorType); | |
| 34 | |
| 35 this._dispatchFlavorChange(flavorType, flavorValue); | |
| 36 }, | |
| 37 | |
| 38 /** | |
| 39 * @param {function(new:T, ...)} flavorType | |
| 40 * @param {?T} flavorValue | |
| 41 * @template T | |
| 42 */ | |
| 43 _dispatchFlavorChange: function(flavorType, flavorValue) | |
| 44 { | |
| 45 for (var extension of self.runtime.extensions(WebInspector.ContextFlavor
Listener)) { | |
| 46 if (extension.hasContextType(flavorType)) | |
| 47 extension.instance().then(instance => /** @type {!WebInspector.C
ontextFlavorListener} */ (instance).flavorChanged(flavorValue)); | |
| 48 } | |
| 49 var dispatcher = this._eventDispatchers.get(flavorType); | |
| 50 if (!dispatcher) | |
| 51 return; | |
| 52 dispatcher.dispatchEventToListeners(WebInspector.Context.Events.FlavorCh
anged, flavorValue); | |
| 53 }, | |
| 54 | |
| 55 /** | |
| 56 * @param {function(new:Object, ...)} flavorType | |
| 57 * @param {function(!WebInspector.Event)} listener | |
| 58 * @param {!Object=} thisObject | |
| 59 */ | |
| 60 addFlavorChangeListener: function(flavorType, listener, thisObject) | |
| 61 { | |
| 62 var dispatcher = this._eventDispatchers.get(flavorType); | |
| 63 if (!dispatcher) { | |
| 64 dispatcher = new WebInspector.Object(); | |
| 65 this._eventDispatchers.set(flavorType, dispatcher); | |
| 66 } | |
| 67 dispatcher.addEventListener(WebInspector.Context.Events.FlavorChanged, l
istener, thisObject); | |
| 68 }, | |
| 69 | |
| 70 /** | |
| 71 * @param {function(new:Object, ...)} flavorType | |
| 72 * @param {function(!WebInspector.Event)} listener | |
| 73 * @param {!Object=} thisObject | |
| 74 */ | |
| 75 removeFlavorChangeListener: function(flavorType, listener, thisObject) | |
| 76 { | |
| 77 var dispatcher = this._eventDispatchers.get(flavorType); | |
| 78 if (!dispatcher) | |
| 79 return; | |
| 80 dispatcher.removeEventListener(WebInspector.Context.Events.FlavorChanged
, listener, thisObject); | |
| 81 if (!dispatcher.hasEventListeners(WebInspector.Context.Events.FlavorChan
ged)) | |
| 82 this._eventDispatchers.remove(flavorType); | |
| 83 }, | |
| 84 | |
| 85 /** | |
| 86 * @param {function(new:T, ...)} flavorType | |
| 87 * @return {?T} | |
| 88 * @template T | |
| 89 */ | |
| 90 flavor: function(flavorType) | |
| 91 { | |
| 92 return this._flavors.get(flavorType) || null; | |
| 93 }, | |
| 94 | |
| 95 /** | |
| 96 * @return {!Set.<function(new:Object, ...)>} | |
| 97 */ | |
| 98 flavors: function() | |
| 99 { | |
| 100 return new Set(this._flavors.keys()); | |
| 101 }, | |
| 102 | |
| 103 /** | |
| 104 * @param {!Array.<!Runtime.Extension>} extensions | |
| 105 * @return {!Set.<!Runtime.Extension>} | |
| 106 */ | |
| 107 applicableExtensions: function(extensions) | |
| 108 { | |
| 109 var targetExtensionSet = new Set(); | |
| 110 | |
| 111 var availableFlavors = this.flavors(); | |
| 112 extensions.forEach(function(extension) { | |
| 113 if (self.runtime.isExtensionApplicableToContextTypes(extension, avai
lableFlavors)) | |
| 114 targetExtensionSet.add(extension); | |
| 115 }); | |
| 116 | |
| 117 return targetExtensionSet; | |
| 118 } | |
| 119 }; | 111 }; |
| 120 | 112 |
| 121 /** | 113 /** |
| 122 * @interface | 114 * @interface |
| 123 */ | 115 */ |
| 124 WebInspector.ContextFlavorListener = function() { }; | 116 WebInspector.ContextFlavorListener = function() {}; |
| 125 | 117 |
| 126 WebInspector.ContextFlavorListener.prototype = { | 118 WebInspector.ContextFlavorListener.prototype = { |
| 127 /** | 119 /** |
| 128 * @param {?Object} object | 120 * @param {?Object} object |
| 129 */ | 121 */ |
| 130 flavorChanged: function(object) { } | 122 flavorChanged: function(object) {} |
| 131 }; | 123 }; |
| 132 | 124 |
| 133 WebInspector.context = new WebInspector.Context(); | 125 WebInspector.context = new WebInspector.Context(); |
| OLD | NEW |