mirror of
https://github.com/react/react-native-devtools-frontend.git
synced 2026-10-10 05:35:05 +08:00
Move and rename files. NOAUTOREVERT=true NOPRESUBMIT=true NOTREECHECKS=true Bug: 768828 TBR=darin@chromium.org NOTRY=true Change-Id: I66d3b155808bc5bdbf237b80208e1e552bcf7f28 Reviewed-on: https://chromium-review.googlesource.com/1001153 Reviewed-by: Blink Reformat <blink-reformat@chromium.org> Commit-Queue: Blink Reformat <blink-reformat@chromium.org> Cr-Original-Commit-Position: refs/heads/master@{#549061} Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src Cr-Mirrored-Commit: 0aee4434a4dba42a42abaea9bfbc0cd196a63bc1
127 lines
3.3 KiB
JavaScript
127 lines
3.3 KiB
JavaScript
// Copyright 2014 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.
|
|
/**
|
|
* @unrestricted
|
|
*/
|
|
UI.Context = class {
|
|
constructor() {
|
|
this._flavors = new Map();
|
|
this._eventDispatchers = new Map();
|
|
}
|
|
|
|
/**
|
|
* @param {function(new:T, ...)} flavorType
|
|
* @param {?T} flavorValue
|
|
* @template T
|
|
*/
|
|
setFlavor(flavorType, flavorValue) {
|
|
const value = this._flavors.get(flavorType) || null;
|
|
if (value === flavorValue)
|
|
return;
|
|
if (flavorValue)
|
|
this._flavors.set(flavorType, flavorValue);
|
|
else
|
|
this._flavors.remove(flavorType);
|
|
|
|
this._dispatchFlavorChange(flavorType, flavorValue);
|
|
}
|
|
|
|
/**
|
|
* @param {function(new:T, ...)} flavorType
|
|
* @param {?T} flavorValue
|
|
* @template T
|
|
*/
|
|
_dispatchFlavorChange(flavorType, flavorValue) {
|
|
for (const extension of self.runtime.extensions(UI.ContextFlavorListener)) {
|
|
if (extension.hasContextType(flavorType)) {
|
|
extension.instance().then(
|
|
instance => /** @type {!UI.ContextFlavorListener} */ (instance).flavorChanged(flavorValue));
|
|
}
|
|
}
|
|
const dispatcher = this._eventDispatchers.get(flavorType);
|
|
if (!dispatcher)
|
|
return;
|
|
dispatcher.dispatchEventToListeners(UI.Context.Events.FlavorChanged, flavorValue);
|
|
}
|
|
|
|
/**
|
|
* @param {function(new:Object, ...)} flavorType
|
|
* @param {function(!Common.Event)} listener
|
|
* @param {!Object=} thisObject
|
|
*/
|
|
addFlavorChangeListener(flavorType, listener, thisObject) {
|
|
let dispatcher = this._eventDispatchers.get(flavorType);
|
|
if (!dispatcher) {
|
|
dispatcher = new Common.Object();
|
|
this._eventDispatchers.set(flavorType, dispatcher);
|
|
}
|
|
dispatcher.addEventListener(UI.Context.Events.FlavorChanged, listener, thisObject);
|
|
}
|
|
|
|
/**
|
|
* @param {function(new:Object, ...)} flavorType
|
|
* @param {function(!Common.Event)} listener
|
|
* @param {!Object=} thisObject
|
|
*/
|
|
removeFlavorChangeListener(flavorType, listener, thisObject) {
|
|
const dispatcher = this._eventDispatchers.get(flavorType);
|
|
if (!dispatcher)
|
|
return;
|
|
dispatcher.removeEventListener(UI.Context.Events.FlavorChanged, listener, thisObject);
|
|
if (!dispatcher.hasEventListeners(UI.Context.Events.FlavorChanged))
|
|
this._eventDispatchers.remove(flavorType);
|
|
}
|
|
|
|
/**
|
|
* @param {function(new:T, ...)} flavorType
|
|
* @return {?T}
|
|
* @template T
|
|
*/
|
|
flavor(flavorType) {
|
|
return this._flavors.get(flavorType) || null;
|
|
}
|
|
|
|
/**
|
|
* @return {!Set.<function(new:Object, ...)>}
|
|
*/
|
|
flavors() {
|
|
return new Set(this._flavors.keys());
|
|
}
|
|
|
|
/**
|
|
* @param {!Array.<!Runtime.Extension>} extensions
|
|
* @return {!Set.<!Runtime.Extension>}
|
|
*/
|
|
applicableExtensions(extensions) {
|
|
const targetExtensionSet = new Set();
|
|
|
|
const availableFlavors = this.flavors();
|
|
extensions.forEach(function(extension) {
|
|
if (self.runtime.isExtensionApplicableToContextTypes(extension, availableFlavors))
|
|
targetExtensionSet.add(extension);
|
|
});
|
|
|
|
return targetExtensionSet;
|
|
}
|
|
};
|
|
|
|
/** @enum {symbol} */
|
|
UI.Context.Events = {
|
|
FlavorChanged: Symbol('FlavorChanged')
|
|
};
|
|
|
|
/**
|
|
* @interface
|
|
*/
|
|
UI.ContextFlavorListener = function() {};
|
|
|
|
UI.ContextFlavorListener.prototype = {
|
|
/**
|
|
* @param {?Object} object
|
|
*/
|
|
flavorChanged(object) {}
|
|
};
|
|
|
|
UI.context = new UI.Context();
|