Skip to content

Commit

Permalink
improve garbage collection
Browse files Browse the repository at this point in the history
by shortening `.handle` reference chains

Extending test suite to get coverage for such cases
  • Loading branch information
bergus committed Jun 23, 2016
1 parent 4a58229 commit f10cde4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Promise.js
Expand Up @@ -168,6 +168,7 @@ export class Future extends Core {
}

_become (p) {
/* eslint complexity:[2,6] */
if (p === this) {
p = cycle()
}
Expand All @@ -185,8 +186,11 @@ export class Future extends Core {
// for not unnecessarily creating handles that never see any actions
// works well because it has a near() method
this.handle = p
} else if (p.handle) {
this.handle = p.handle
} else {
this.handle = new Handle(p)
// explicit handle to avoid reference chain between multiple futures
this.handle = p.handle = new Handle(p)
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions test/future-test.js
Expand Up @@ -44,6 +44,29 @@ describe('future', () => {
resolve(reject(expected))
return promise.then(assert.ifError, x => assert.strictEqual(expected, x))
})

it('should resolve with pending promise', () => {
const { resolve: resolve1, promise: promise1 } = future()
const { resolve: resolve2, promise: promise2 } = future()
const { resolve: resolve3, promise: promise3 } = future()
const expected = {}
setTimeout(resolve2, 1, promise1)
setTimeout(resolve1, 2, expected)
setTimeout(resolve3, 2, promise2)
return promise3.then(x => assert.strictEqual(expected, x))
})

it('should resolve with pending promise that already has handle', () => {
const { resolve: resolve1, promise: promise1 } = future()
const { resolve: resolve2, promise: promise2 } = future()
const { resolve: resolve3, promise: promise3 } = future()
const expected = {}
promise1.map(x => x === expected)
setTimeout(resolve2, 1, promise1)
setTimeout(resolve1, 2, expected)
setTimeout(resolve3, 2, promise2)
return promise3.then(x => assert.strictEqual(expected, x))
})
})

describe('when resolved to another promise', () => {
Expand Down

0 comments on commit f10cde4

Please sign in to comment.