Skip to content

Commit 625e8ed

Browse files
committed
Use asap instead of raf
1 parent 846cb41 commit 625e8ed

5 files changed

Lines changed: 84 additions & 14 deletions

File tree

flow-typed/npm/asap_vx.x.x.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// flow-typed signature: 707dc8ad343bbbc20eb3bda63ab382df
2+
// flow-typed version: <<STUB>>/asap_v2.0.5/flow_v0.44.2
3+
4+
/**
5+
* This is an autogenerated libdef stub for:
6+
*
7+
* 'asap'
8+
*
9+
* Fill this stub out by replacing all the `any` types.
10+
*
11+
* Once filled out, we encourage you to share your work with the
12+
* community by sending a pull request to:
13+
* https://github.com/flowtype/flow-typed
14+
*/
15+
16+
declare module 'asap' {
17+
declare module.exports: any;
18+
}
19+
20+
/**
21+
* We include stubs for each file inside this npm package in case you need to
22+
* require those files directly. Feel free to delete any files that aren't
23+
* needed.
24+
*/
25+
declare module 'asap/asap' {
26+
declare module.exports: any;
27+
}
28+
29+
declare module 'asap/browser-asap' {
30+
declare module.exports: any;
31+
}
32+
33+
declare module 'asap/browser-raw' {
34+
declare module.exports: any;
35+
}
36+
37+
declare module 'asap/raw' {
38+
declare module.exports: any;
39+
}
40+
41+
// Filename aliases
42+
declare module 'asap/asap.js' {
43+
declare module.exports: $Exports<'asap/asap'>;
44+
}
45+
declare module 'asap/browser-asap.js' {
46+
declare module.exports: $Exports<'asap/browser-asap'>;
47+
}
48+
declare module 'asap/browser-raw.js' {
49+
declare module.exports: $Exports<'asap/browser-raw'>;
50+
}
51+
declare module 'asap/raw.js' {
52+
declare module.exports: $Exports<'asap/raw'>;
53+
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
},
3939
"homepage": "https://github.com/cssinjs/styled-jss#readme",
4040
"dependencies": {
41+
"asap": "^2.0.5",
4142
"is-react-prop": "^0.0.3",
4243
"jss": "^7.1.0",
43-
"jss-preset-default": "^2.0.0",
44-
"raf": "^3.3.0"
44+
"jss-preset-default": "^2.0.0"
4545
},
4646
"peerDependencies": {
4747
"react": "^15.5.4",

src/createStyled.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import asap from 'asap'
2+
13
import styled from './styled'
24

35
import type {
@@ -15,6 +17,26 @@ const createStyled = (jss: Function) => (
1517
let staticSheet
1618
let dynamicSheet
1719

20+
let dynamicCounter = 0
21+
22+
asap(() => {
23+
if (dynamicCounter) {
24+
dynamicCounter = 0
25+
dynamicSheet.attach().link()
26+
}
27+
})
28+
29+
const addRule = (name: string, style: ComponentStyleType, data: Object) => {
30+
if (data) {
31+
dynamicSheet.detach().addRule(name, style)
32+
dynamicSheet.update(name, data)
33+
dynamicCounter++
34+
}
35+
else {
36+
staticSheet.addRule(name, style)
37+
}
38+
}
39+
1840
const mountSheets = () => {
1941
if (!staticSheet) {
2042
staticSheet = jss.createStyleSheet(baseStyles, {
@@ -40,7 +62,7 @@ const createStyled = (jss: Function) => (
4062

4163
const elementStyle = {...style, ...ownStyle}
4264

43-
return styled({tagName, baseStyles, elementStyle, mountSheets})
65+
return styled({tagName, baseStyles, elementStyle, mountSheets, addRule})
4466
}, {mountSheets, styles: baseStyles})
4567
}
4668

src/styled.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ import type {
1515
type StyledArgs = {
1616
tagName: string,
1717
elementStyle: ComponentStyleType,
18-
mountSheets: Function
18+
mountSheets: Function,
19+
addRule: Function
1920
}
2021

21-
const styled = ({tagName, elementStyle, mountSheets}: StyledArgs) => {
22+
const styled = ({tagName, elementStyle, mountSheets, addRule}: StyledArgs) => {
2223
const dynamicStyle = getDynamicStyles(elementStyle)
2324
const staticTagName = generateTagName(tagName)
2425

@@ -43,17 +44,11 @@ const styled = ({tagName, elementStyle, mountSheets}: StyledArgs) => {
4344
Object.assign(this, mountSheets())
4445

4546
if (!this.staticSheet.getRule(staticTagName)) {
46-
this.staticSheet.addRule(staticTagName, elementStyle)
47+
addRule(staticTagName, elementStyle)
4748
}
4849

4950
if (dynamicStyle && !this.dynamicSheet.getRule(this.dynamicTagName)) {
50-
this.dynamicSheet
51-
.detach()
52-
.addRule(this.dynamicTagName, dynamicStyle)
53-
this.dynamicSheet
54-
.update(this.dynamicTagName, this.props)
55-
.attach()
56-
.link()
51+
addRule(this.dynamicTagName, dynamicStyle, this.props)
5752
}
5853
}
5954

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ arrify@^1.0.0, arrify@^1.0.1:
152152
version "1.0.1"
153153
resolved "https://registry.yarnpkg.com/arrify/-/arrify-1.0.1.tgz#898508da2226f380df904728456849c1501a4b0d"
154154

155-
asap@~2.0.3:
155+
asap@^2.0.5, asap@~2.0.3:
156156
version "2.0.5"
157157
resolved "https://registry.yarnpkg.com/asap/-/asap-2.0.5.tgz#522765b50c3510490e52d7dcfe085ef9ba96958f"
158158

0 commit comments

Comments
 (0)