当由于某种原因调用两次回调函数时,我遇到了这样的问题。在这里,我将尝试显示调用堆栈:
Provider.prototype._delegateEvent = function(event, collaboration) {
switch (event.type) {
case EventTypes.START_SCREEN_SHARING:
this._isScreenSharingActive = true;
this._isScreenSharingEnding = false;
//STEP 1 (addOnContentSharingStartedCallback will be called)
this.contentSharingHandler.startScreenSharing(collaboration, event); //starting screensharing and addOnContentSharingStartedCallback will be called later
break;
}};
startScreenSharing: function(collaboration, startEvent) {
var startingParticipant = this._getParticipant(collaboration, startEvent);
var sharingState = collaboration._contentSharing._contentSharingService._sharingState;
collaboration._contentSharing._contentSharingService._sharingState = Collaboration.ContentSharingState.SHARING;
if (
collaboration._contentSharing._isPaused === true &&
sharingState !== Collaboration.ContentSharingState.NOT_SHARING
) {
//addOnContentSharingResumedCallback will be called
collaboration._contentSharing._isPaused = false;
collaboration._contentSharing._onContentSharingResumedCallbacks.fire(collaboration._contentSharing, startingParticipant);
} else {
// STEP 2(addOnContentSharingStartedCallback will be called)
collaboration._contentSharing._onContentSharingStartedCallbacks.fire(collaboration._contentSharing, startingParticipant);
}
},
(function(angular) {
'use strict';
angular
.module('collaboration')
.controller('ContentSharingManagementController', ContentSharingManagementController);
ContentSharingManagementController.$inject = ['promisify', '$scope', '$timeout', ];
function ContentSharingManagementController(promisify, $scope, $timeout) {
var vm = this;
vm.isPaused = false;
vm.isReceivingPaused = false;
vm.isStarted = false;
activate();
function activate() {//STEP 3
vm.contentSharing.addOnContentSharingStartedCallback(function() {
console.log("addOnContentSharingStartedCallback is called");
vm.isStarted = true;
showPreview();
$scope.$apply();
});
vm.contentSharing.addOnContentSharingEndedCallback(function() {
console.log("addOnContentSharingEndedCallback is called");
vm.isStarted = false;
vm.isPaused = false;
window.removeEventListener('message', screenSharingListener);
window.removeEventListener('message', windowSharingListener);
$scope.$applyAsync();
});
vm.contentSharing.addOnContentSharingPausedCallback(function() {
console.log("addOnContentSharingPausedCallback is called");
vm.isPaused = true;
$scope.$apply();
});
vm.contentSharing.addOnContentSharingResumedCallback(function() {
console.log("addOnContentSharingResumedCallback is called");
vm.isPaused = false;
$scope.$apply();
});
}
}
调用回调函数时我做错了什么?什么会对它被调用两次的事实产生影响?
脚步:
- 调用函数 _delegateEvent
- 调用 startScreenSharing
- 调用 activate() 函数,然后调用回调函数 addOnContentSharingEndedCallback