-
-
Notifications
You must be signed in to change notification settings - Fork 399
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
fix memory leak in dynamic nested rule #1563
Conversation
to test both addRule and replaceRule
packages/jss/src/RuleList.js
Outdated
// Rules registry for access by .get() method. | ||
// It contains the same rule registered by name and by selector. | ||
map = {} | ||
nameMap = {} | ||
|
||
selectorMap = {} |
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 splitted map
to avoid accidental replace via selector.
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.
do you have a test that shows how it was possible before to accidentally replace by a selector? I don't quite understand when this is the case
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.
This one was the case.
https://github.com/cssinjs/jss/pull/1563/files#diff-97551384ad757770e360d597385f79789efdd71d55352d2f8b17e0c3ad7f2ec7R131-R157
I fixed name like following and it now passes. So I reverted the splitting.
https://github.com/cssinjs/jss/pull/1563/files#diff-59742e48378650fbbebc47efbcbb17001e1b0926fb6e44742d23969c2973152aL91-R91
To change name, we needed to fix following.
https://github.com/cssinjs/jss/pull/1563/files#diff-e2d69de7d5a671488a4eb6dfe6341d04942a698f362efe143b20008d84908c1aL149-R159
Wow this looks great! I am going to investigate every change to see if everything is correct. The only thing that I don't like so far is the increase in bundle size. I have to think if we can achieve the same thing without adding that much logic. |
Thank you for your comment. |
// It will be inserted all together when .attach is called. | ||
if (this.renderer) { | ||
if (!newRule) { | ||
this.renderer.deleteRule(oldRule) |
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 wonder why do we delete an oldRule if a new rule was not created?
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.
The only case newRule doesn't exist is when user tried to create an unexisting rule type, like some typo or something.
I am not sure but maybe in this case an expected behavior would be to not remove the old rule .
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 am not sure but maybe in this case an expected behavior would be to not remove the old rule.
I see.
I intended that "when user replace rule by invalid rule, the old rule will replaced by 'nothing'. it results deleting the rule".
Is it better to warn it without rendering effect?
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.
actually, I think you are correct, returning null in a function is also removing the rule from dom
packages/jss-plugin-rule-value-function/src/plugin-nested.test.js
Outdated
Show resolved
Hide resolved
packages/jss-plugin-rule-value-function/src/plugin-nested.test.js
Outdated
Show resolved
Hide resolved
packages/jss-plugin-rule-value-function/src/plugin-nested.test.js
Outdated
Show resolved
Hide resolved
packages/jss-plugin-rule-value-function/src/plugin-nested.test.js
Outdated
Show resolved
Hide resolved
packages/jss-plugin-rule-value-function/src/plugin-nested.test.js
Outdated
Show resolved
Hide resolved
packages/jss-plugin-rule-value-function/src/plugin-nested.test.js
Outdated
Show resolved
Hide resolved
packages/jss-plugin-rule-value-function/src/plugin-nested.test.js
Outdated
Show resolved
Hide resolved
/** | ||
* Replace a rule in the current stylesheet. | ||
*/ | ||
replaceRule(name, decl, options) { |
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.
please correct me if I am wrong, but it seems this entire method could be replaced by something this (I haven't tried this to run yet):
const oldRule = this.getRule(name)
const oldRuleIndex = this.indexOf(oldRule)
if (oldRuleIndex !== -1) options = {...options, index: oldRuleIndex}
this.addRule(name, decl, options)
if (oldRule) this.deleteRule(oldRule)
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 it will unregister
new rule from RuleList
.
I'll try it.
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.
My comment might be wrong.
addRule
will generate new name so that we will be able to get new rule by name.
get by selector might not be work.
I'll try registering new rule one more time.
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.
Tests showed some problems.
- new rule is registered as name different from the argument
- if name is
a
, new rule are registered asa-d0
.
- if name is
- replacing rule multiple times, it will render more and more rules
- consider we use
a
as a name - first
replaceRule
will register rule asa
- second
replaceRule
will register rule asa-d0
and removea
- third
replaceRule
will register rule asa
- forth
replaceRule
will register rule asa-d1
and removea
- fifth
replaceRule
will register rule asa
- now, we have
a-d0
,a-1
anda
- consider we use
Thank you for your careful review 👍 |
Co-authored-by: Oleg Isonen <oleg008@gmail.com>
Co-authored-by: Oleg Isonen <oleg008@gmail.com>
include related change - use almost unique name rather than selector name in nested plugin to prevent accidental replacement - fix strange behavior of global plugin (probably bug)
For each comment, I fixed or replied. |
merged, thanks a lot, this is a huge help! |
Corresponding Issue(s):
#1360
What Would You Like to Add/Fix?
Fix memory leak in nested rule in function rule
Todo
Expectations on Changes
Resolve memory leak nested rule in function rule
Changelog
Fix memory leak with dynamic nested style.
Note