Skip to content

Commit

Permalink
Merge pull request #1475 from probablyup/fix-mem-leak
Browse files Browse the repository at this point in the history
fix streaming memory leak
  • Loading branch information
quantizor committed Feb 1, 2018
2 parents 58a2f5d + 30b98e1 commit 76d7c69
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ All notable changes to this project will be documented in this file. If a contri

## Unreleased

- Fix memory leak in the server-side streaming logic ([see #1475](https://github.com/styled-components/styled-components/pull/1475))

## [v3.1.4] - 2018-01-29

- Disable "speedy" mode for IE and Edge. There seems to be some incompatibility with how the `insertRule` API functions in their rendering stack compared to the other vendors. (see [#1465](https://github.com/styled-components/styled-components/pull/1465)) _Note: this is currently being investigated for reimplementation._
Expand Down
14 changes: 9 additions & 5 deletions src/models/ServerStyleSheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,19 +153,22 @@ export default class ServerStyleSheet {
)
}

close() {
clones.splice(clones.indexOf(this.instance), 1)
this.closed = true
}

getStyleTags(): string {
if (!this.closed) {
clones.splice(clones.indexOf(this.instance), 1)
this.closed = true
this.close()
}

return this.instance.toHTML()
}

getStyleElement() {
if (!this.closed) {
clones.splice(clones.indexOf(this.instance), 1)
this.closed = true
this.close()
}

return this.instance.toReactElements()
Expand Down Expand Up @@ -193,12 +196,13 @@ export default class ServerStyleSheet {
})

readableStream.on('end', () => {
this.closed = true
ourStream.push(null)
this.close()
})

readableStream.on('error', err => {
ourStream.emit('error', err)
this.close()
})

return ourStream
Expand Down
4 changes: 4 additions & 0 deletions src/test/ssr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ describe('ssr', () => {
`

const sheet = new ServerStyleSheet()

jest.spyOn(sheet, 'close')

const jsx = sheet.collectStyles(<Heading>Hello SSR!</Heading>)
const stream = sheet.interleaveWithNodeStream(renderToNodeStream(jsx))

Expand All @@ -251,6 +254,7 @@ describe('ssr', () => {

stream.on('end', () => {
expect(received).toMatchSnapshot()
expect(sheet.close).toHaveBeenCalled()
resolve()
})

Expand Down
Loading

0 comments on commit 76d7c69

Please sign in to comment.