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

simplify stream emitter code #1479

Merged
merged 2 commits into from Feb 1, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,8 @@ All notable changes to this project will be documented in this file. If a contri

## Unreleased

- Bugfix for the last style tag sometimes being emitted multiple times during streaming ([see #1479](https://github.com/styled-components/styled-components/pull/1479))

## [v3.1.5] - 2018-02-01

- Apply a workaround to re-enable "speedy" mode for IE/Edge ([see #1468](https://github.com/styled-components/styled-components/pull/1468))
Expand Down
15 changes: 7 additions & 8 deletions src/models/ServerStyleSheet.js
Expand Up @@ -133,7 +133,6 @@ export default class ServerStyleSheet {
closed: boolean
instance: StyleSheet
isStreaming: boolean
lastIndex: number

constructor() {
this.instance = StyleSheet.clone(StyleSheet.instance)
Expand Down Expand Up @@ -182,17 +181,17 @@ export default class ServerStyleSheet {
ourStream._read = () => {}

this.isStreaming = true
this.lastIndex = 0

readableStream.on('data', chunk => {
ourStream.push(
this.instance.tags
.slice(this.lastIndex)
.map(tag => tag.toHTML())
.join('') + chunk
)
this.instance.tags.reduce((html, tag) => {
if (!tag.isSealed()) {
html += tag.toHTML() // eslint-disable-line no-param-reassign
}

this.lastIndex = this.instance.tags.length - 1
return html
}, '') + chunk
)
})

readableStream.on('end', () => {
Expand Down