Skip to content

Commit

Permalink
Use one just one asap observable
Browse files Browse the repository at this point in the history
  • Loading branch information
lttb committed Apr 24, 2017
1 parent 625e8ed commit d1e7818
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/createStyled.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,30 @@ import type {
TagNameOrStyledElementType
} from './types'

const dynamicSheets = []
let sheetsToUpdate = {}

asap(() => {
const sheetNames = Object.keys(sheetsToUpdate)
if (sheetNames.length) {
sheetsToUpdate = {}
sheetNames.forEach(sheetName =>
dynamicSheets[Number(sheetName)].attach().link())
}
})

const createStyled = (jss: Function) => (
baseStyles: BaseStylesType = {}
): StyledType => {
let staticSheet
let dynamicSheet

let dynamicCounter = 0

asap(() => {
if (dynamicCounter) {
dynamicCounter = 0
dynamicSheet.attach().link()
}
})
let dynamicSheetName

const addRule = (name: string, style: ComponentStyleType, data: Object) => {
if (data) {
dynamicSheet.detach().addRule(name, style)
dynamicSheet.update(name, data)
dynamicCounter++
sheetsToUpdate[dynamicSheetName] = true
}
else {
staticSheet.addRule(name, style)
Expand All @@ -47,6 +51,8 @@ const createStyled = (jss: Function) => (
link: true,
meta: 'DynamicComponentSheet',
}).attach()
dynamicSheets.push(dynamicSheet)
dynamicSheetName = dynamicSheets.length - 1
}

return {staticSheet, dynamicSheet}
Expand Down

0 comments on commit d1e7818

Please sign in to comment.