Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unregister player (in classroom module) interval and timeout on refresh #353

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion src/main/js/modules/classroom/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
/*global require, module, __plugin, __dirname, echo, persist, isOp, events, Packages, command, global */
/*global require, module, __plugin, __dirname, echo, persist, isOp, events, Packages, command, global, setInterval, clearInterval, setTimeout, clearTimeout */
var utils = require('utils'),
watcher = require('watcher'),
autoload = require('plugin').autoload,
Expand Down Expand Up @@ -134,6 +134,8 @@ function revokeScripting ( player ) {
var autoloadTime = {};

var playerEventHandlers = {};
var playerIntervals = {};
var playerTimeouts = {};

function reloadPlayerModules( playerContext, playerDir ){
/*
Expand All @@ -150,6 +152,32 @@ function reloadPlayerModules( playerContext, playerDir ){
playerEventHandlers[playerDirPath] = [];
eventHandlers = playerEventHandlers[playerDirPath];
}
/*
Glorf 20171003 also unregister any timeout and interval registered
*/
var intervals = playerIntervals[playerDirPath];
if (intervals){
for (var i = 0;i < intervals.length; i++){
clearInterval(intervals[i]);
}
intervals.length = 0;
} else {
playerIntervals[playerDirPath] = [];
intervals = playerIntervals[playerDirPath];
}


var timeouts = playerTimeouts[playerDirPath];
if (timeouts){
for (var i = 0;i < timeouts.length; i++){
clearTimeout(timeouts[i]);
}
timeouts.length = 0;
} else {
playerTimeouts[playerDirPath] = [];
timeouts = playerTimeouts[playerDirPath];
}

/*
override events.on() so that the listener is stored here so it can be unregistered.
*/
Expand All @@ -159,8 +187,30 @@ function reloadPlayerModules( playerContext, playerDir ){
eventHandlers.push(handler);
};
events.on = newOn;
/*
Gloorf 20171003 override setInterval()/setTimeout() so the timeout/interval object is stored to be unregistered
*/
var oldInterval = global.setInterval;
var newInterval = function(callback, delay) {
var handler = oldInterval(callback, delay);
intervals.push(handler);
return handler;
};
global.setInterval = newInterval;

var oldTimeout = global.setTimeout;
var newTimeout = function(callback, delay) {
var handler = oldTimeout(callback, delay);
timeouts.push(handler);
return handler;
};
global.setTimeout = newTimeout;

autoload( playerContext, playerDir, { cache: false });
events.on = oldOn;
global.setInterval = oldInterval;
global.setTimeout = oldTimeout;

}
function grantScripting( player ) {
console.log('Enabling scripting for player ' + player.name);
Expand Down