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

fix memory leak in asapScheduler (via Immediate) #5183

Merged
merged 1 commit into from
Dec 16, 2019
Merged
Show file tree
Hide file tree
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
14 changes: 13 additions & 1 deletion spec/util/Immediate-spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import { Immediate } from 'rxjs/util/Immediate';
import { Immediate, TestTools } from 'rxjs/internal/util/Immediate';

describe('Immediate', () => {
it('should schedule on the next microtask', (done) => {
Expand Down Expand Up @@ -30,4 +30,16 @@ describe('Immediate', () => {
done();
});
});

it('should clear the task after execution', (done) => {
const results: number[] = [];
Immediate.setImmediate(() => results.push(1));
Immediate.setImmediate(() => results.push(2));

setTimeout(() => {
const number = TestTools.pending();
expect(number).to.equal(0);
done();
});
});
});
36 changes: 27 additions & 9 deletions src/internal/util/Immediate.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,41 @@
let nextHandle = 1;
const RESOLVED = (() => Promise.resolve())();
const activeHandles: { [key: number]: any } = {};

const tasksByHandle: { [handle: string]: () => void } = {};

function runIfPresent(handle: number) {
const cb = tasksByHandle[handle];
if (cb) {
cb();
/**
* Finds the handle in the list of active handles, and removes it.
* Returns `true` if found, `false` otherwise. Used both to clear
* Immediate scheduled tasks, and to identify if a task should be scheduled.
*/
function findAndClearHandle(handle: number): boolean {
if (handle in activeHandles) {
delete activeHandles[handle];
return true;
}
return false;
}

/**
* Helper functions to schedule and unschedule microtasks.
*/
export const Immediate = {
setImmediate(cb: () => void): number {
const handle = nextHandle++;
tasksByHandle[handle] = cb;
Promise.resolve().then(() => runIfPresent(handle));
activeHandles[handle] = true;
RESOLVED.then(() => findAndClearHandle(handle) && cb());
return handle;
},

clearImmediate(handle: number): void {
delete tasksByHandle[handle];
findAndClearHandle(handle);
},
};

/**
* Used for internal testing purposes only. Do not export from library.
*/
export const TestTools = {
pending() {
return Object.keys(activeHandles).length;
}
};