TargetManager.js 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. export default class TargetManager extends Common.Object{constructor(){super();this._targets=[];this._observers=[];this._modelListeners=new Platform.Multimap();this._modelObservers=new Platform.Multimap();this._isSuspended=false;}
  2. suspendAllTargets(reason){if(this._isSuspended){return Promise.resolve();}
  3. this._isSuspended=true;this.dispatchEventToListeners(Events.SuspendStateChanged);return Promise.all(this._targets.map(target=>target.suspend(reason)));}
  4. resumeAllTargets(){if(!this._isSuspended){return Promise.resolve();}
  5. this._isSuspended=false;this.dispatchEventToListeners(Events.SuspendStateChanged);return Promise.all(this._targets.map(target=>target.resume()));}
  6. allTargetsSuspended(){return this._isSuspended;}
  7. models(modelClass){const result=[];for(let i=0;i<this._targets.length;++i){const model=this._targets[i].model(modelClass);if(model){result.push(model);}}
  8. return result;}
  9. inspectedURL(){return this._targets[0]?this._targets[0].inspectedURL():'';}
  10. observeModels(modelClass,observer){const models=this.models(modelClass);this._modelObservers.set(modelClass,observer);for(const model of models){observer.modelAdded(model);}}
  11. unobserveModels(modelClass,observer){this._modelObservers.delete(modelClass,observer);}
  12. modelAdded(target,modelClass,model){for(const observer of this._modelObservers.get(modelClass).valuesArray()){observer.modelAdded(model);}}
  13. _modelRemoved(target,modelClass,model){for(const observer of this._modelObservers.get(modelClass).valuesArray()){observer.modelRemoved(model);}}
  14. addModelListener(modelClass,eventType,listener,thisObject){for(let i=0;i<this._targets.length;++i){const model=this._targets[i].model(modelClass);if(model){model.addEventListener(eventType,listener,thisObject);}}
  15. this._modelListeners.set(eventType,{modelClass:modelClass,thisObject:thisObject,listener:listener});}
  16. removeModelListener(modelClass,eventType,listener,thisObject){if(!this._modelListeners.has(eventType)){return;}
  17. for(let i=0;i<this._targets.length;++i){const model=this._targets[i].model(modelClass);if(model){model.removeEventListener(eventType,listener,thisObject);}}
  18. for(const info of this._modelListeners.get(eventType)){if(info.modelClass===modelClass&&info.listener===listener&&info.thisObject===thisObject){this._modelListeners.delete(eventType,info);}}}
  19. observeTargets(targetObserver){if(this._observers.indexOf(targetObserver)!==-1){throw new Error('Observer can only be registered once');}
  20. for(const target of this._targets){targetObserver.targetAdded(target);}
  21. this._observers.push(targetObserver);}
  22. unobserveTargets(targetObserver){this._observers.remove(targetObserver);}
  23. createTarget(id,name,type,parentTarget,sessionId,waitForDebuggerInPage,connection){const target=new SDK.Target(this,id,name,type,parentTarget,sessionId||'',this._isSuspended,connection||null);if(waitForDebuggerInPage){target.pageAgent().waitForDebugger();}
  24. target.createModels(new Set(this._modelObservers.keysArray()));this._targets.push(target);const copy=this._observers.slice(0);for(const observer of copy){observer.targetAdded(target);}
  25. for(const modelClass of target.models().keys()){this.modelAdded(target,modelClass,target.models().get(modelClass));}
  26. for(const key of this._modelListeners.keysArray()){for(const info of this._modelListeners.get(key)){const model=target.model(info.modelClass);if(model){model.addEventListener(key,info.listener,info.thisObject);}}}
  27. return target;}
  28. removeTarget(target){if(!this._targets.includes(target)){return;}
  29. this._targets.remove(target);for(const modelClass of target.models().keys()){this._modelRemoved(target,modelClass,target.models().get(modelClass));}
  30. const copy=this._observers.slice(0);for(const observer of copy){observer.targetRemoved(target);}
  31. for(const key of this._modelListeners.keysArray()){for(const info of this._modelListeners.get(key)){const model=target.model(info.modelClass);if(model){model.removeEventListener(key,info.listener,info.thisObject);}}}}
  32. targets(){return this._targets.slice();}
  33. targetById(id){for(let i=0;i<this._targets.length;++i){if(this._targets[i].id()===id){return this._targets[i];}}
  34. return null;}
  35. mainTarget(){return this._targets[0]||null;}}
  36. export const Events={AvailableTargetsChanged:Symbol('AvailableTargetsChanged'),InspectedURLChanged:Symbol('InspectedURLChanged'),NameChanged:Symbol('NameChanged'),SuspendStateChanged:Symbol('SuspendStateChanged')};export class Observer{targetAdded(target){}
  37. targetRemoved(target){}}
  38. export class SDKModelObserver{modelAdded(model){}
  39. modelRemoved(model){}}
  40. self.SDK=self.SDK||{};SDK=SDK||{};SDK.TargetManager=TargetManager;SDK.TargetManager.Events=Events;SDK.TargetManager.Observer=Observer;SDK.SDKModelObserver=SDKModelObserver;SDK.targetManager=new TargetManager();