-
Notifications
You must be signed in to change notification settings - Fork 15
Improve mount performance by bulk attachment #9
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b2c9004
Update yarn.lock
lttb 8e57f91
Improve mount performance by queue
lttb 846cb41
Merge branch 'master' into feature/improve-mount-performance
lttb 625e8ed
Use asap instead of raf
lttb d1e7818
Use one just one asap observable
lttb 8f9005d
Add tests for sheetsObserver
lttb 6c9c36a
Rewrite composeClasses
lttb 972d102
Use raf for loops instead asap
lttb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,3 +7,4 @@ | |
|
|
||
| [options] | ||
| all=true | ||
| suppress_comment= \\(.\\|\n\\)*\\$FlowFixMe | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // flow-typed signature: 4de6dbdd4439b8db48934b70acae04b6 | ||
| // flow-typed version: <<STUB>>/raf_v3.3.0/flow_v0.44.2 | ||
|
|
||
| /** | ||
| * This is an autogenerated libdef stub for: | ||
| * | ||
| * 'raf' | ||
| * | ||
| * Fill this stub out by replacing all the `any` types. | ||
| * | ||
| * Once filled out, we encourage you to share your work with the | ||
| * community by sending a pull request to: | ||
| * https://github.com/flowtype/flow-typed | ||
| */ | ||
|
|
||
| declare module 'raf' { | ||
| declare module.exports: any; | ||
| } | ||
|
|
||
| /** | ||
| * We include stubs for each file inside this npm package in case you need to | ||
| * require those files directly. Feel free to delete any files that aren't | ||
| * needed. | ||
| */ | ||
| declare module 'raf/polyfill' { | ||
| declare module.exports: any; | ||
| } | ||
|
|
||
| declare module 'raf/test' { | ||
| declare module.exports: any; | ||
| } | ||
|
|
||
| declare module 'raf/window' { | ||
| declare module.exports: any; | ||
| } | ||
|
|
||
| // Filename aliases | ||
| declare module 'raf/index' { | ||
| declare module.exports: $Exports<'raf'>; | ||
| } | ||
| declare module 'raf/index.js' { | ||
| declare module.exports: $Exports<'raf'>; | ||
| } | ||
| declare module 'raf/polyfill.js' { | ||
| declare module.exports: $Exports<'raf/polyfill'>; | ||
| } | ||
| declare module 'raf/test.js' { | ||
| declare module.exports: $Exports<'raf/test'>; | ||
| } | ||
| declare module 'raf/window.js' { | ||
| declare module.exports: $Exports<'raf/window'>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import styled from './styled' | ||
| import sheetsObserver from './utils/sheetsObserver' | ||
|
|
||
| import type { | ||
| BaseStylesType, | ||
|
|
@@ -9,11 +10,25 @@ import type { | |
| TagNameOrStyledElementType | ||
| } from './types' | ||
|
|
||
| sheetsObserver.listen() | ||
|
|
||
| const createStyled = (jss: Function) => ( | ||
| baseStyles: BaseStylesType = {} | ||
| ): StyledType => { | ||
| let staticSheet | ||
| let dynamicSheet | ||
| let dynamicSheetId | ||
|
|
||
| const addRule = (name: string, style: ComponentStyleType, data: Object) => { | ||
| if (data) { | ||
| dynamicSheet.detach().addRule(name, style) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs a comment, its because of a bug, right? |
||
| dynamicSheet.update(name, data) | ||
| sheetsObserver.update(dynamicSheetId) | ||
| } | ||
| else { | ||
| staticSheet.addRule(name, style) | ||
| } | ||
| } | ||
|
|
||
| const mountSheets = () => { | ||
| if (!staticSheet) { | ||
|
|
@@ -25,6 +40,7 @@ const createStyled = (jss: Function) => ( | |
| link: true, | ||
| meta: 'DynamicComponentSheet', | ||
| }).attach() | ||
| dynamicSheetId = sheetsObserver.add(dynamicSheet) | ||
| } | ||
|
|
||
| return {staticSheet, dynamicSheet} | ||
|
|
@@ -40,7 +56,7 @@ const createStyled = (jss: Function) => ( | |
|
|
||
| const elementStyle = {...style, ...ownStyle} | ||
|
|
||
| return styled({tagName, baseStyles, elementStyle, mountSheets}) | ||
| return styled({tagName, baseStyles, elementStyle, mountSheets, addRule}) | ||
| }, {mountSheets, styles: baseStyles}) | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import sheetsObserver, {observe, dynamicSheets} from '../utils/sheetsObserver' | ||
|
|
||
| const Mocks = { | ||
| // $FlowFixMe: get/set are not supported yet by flow | ||
| get sheetMock() { | ||
| const mock = { | ||
| attach: jest.fn(), | ||
| link: jest.fn(), | ||
| } | ||
| mock.attach.mockReturnValue(mock) | ||
| mock.link.mockReturnValue(mock) | ||
| return mock | ||
| } | ||
| } | ||
|
|
||
| it('should add sheet and return sheetId', () => { | ||
| const {sheetMock} = Mocks | ||
| const sheetId = sheetsObserver.add(sheetMock) | ||
|
|
||
| expect(dynamicSheets).toEqual([sheetMock]) | ||
| expect(sheetId).toEqual(0) | ||
| }) | ||
|
|
||
| it('should reattach updated sheet by observe call', () => { | ||
| const {sheetMock} = Mocks | ||
| const sheetId = sheetsObserver.add(sheetMock) | ||
| sheetsObserver.update(sheetId) | ||
|
|
||
| observe() | ||
|
|
||
| expect(sheetMock.attach).toHaveBeenCalled() | ||
| expect(sheetMock.link).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('should reattach updated sheet by listner', (done) => { | ||
| const {sheetMock} = Mocks | ||
| sheetsObserver.listen() | ||
|
|
||
| const sheetId = sheetsObserver.add(sheetMock) | ||
| sheetsObserver.update(sheetId) | ||
|
|
||
| setTimeout(() => { | ||
| expect(sheetMock.attach).toHaveBeenCalledTimes(1) | ||
| expect(sheetMock.link).toHaveBeenCalledTimes(1) | ||
| done() | ||
| }, 100) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,8 @@ | ||
| export default (...args: any) => args.filter(Boolean).join(' ') | ||
| export default (classes: Array<?string | boolean>) => { | ||
| const filtered = [] | ||
| for (let len = classes.length, index = 0; index < len; index++) { | ||
| if (classes[index]) filtered.push(classes[index]) | ||
| } | ||
|
|
||
| return filtered.join(' ') | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import raf from 'raf' | ||
|
|
||
| import type { | ||
| JssDynamicSheet | ||
| } from '../types' | ||
|
|
||
| export const dynamicSheets = [] | ||
| let sheetsToUpdate = {} | ||
|
|
||
| export const observe = () => { | ||
| const sheetIds = Object.keys(sheetsToUpdate) | ||
| if (sheetIds.length) { | ||
| sheetsToUpdate = {} | ||
| sheetIds.forEach(sheetId => | ||
| dynamicSheets[Number(sheetId)].attach().link()) | ||
| } | ||
| } | ||
|
|
||
| const listen = () => { | ||
| observe() | ||
| raf(listen) | ||
| } | ||
|
|
||
| export default { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. its not really an observer ... also it might be confused with a real Observer . |
||
| listen, | ||
| add(sheet: JssDynamicSheet) { | ||
| dynamicSheets.push(sheet) | ||
| return dynamicSheets.length - 1 | ||
| }, | ||
| update(sheetId: number) { | ||
| sheetsToUpdate[sheetId] = true | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think raf polyfill should be responsibility of the user, like any other new features.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I would just use requestAnimationFrame, its well implemented right now anyways. All required polyfills should be in the readme
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't use just
window.requestAnimationFrame, because we run this code not only in the browser (tests, ssr) - so we need polyfillBut we can make it customizable