-
Notifications
You must be signed in to change notification settings - Fork 5
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
HY-4649 track loaded fonts and add methods to unload fonts #10
Conversation
RavenNumber of Findings: 0 |
General InformationTicket(s): Code Review(s): #10 Additional InformationReviewers: patkujawa-wf, tomconnell-wf
|
@@ -23,7 +23,6 @@ const int DEFAULT_TIMEOUT = 3000; | |||
const String DEFAULT_TEST_STRING = 'BESbswy'; | |||
const String _NORMAL = 'normal'; | |||
const int _NATIVE_FONT_LOADING_CHECK_INTERVAL = 50; | |||
const String _FONT_FACE_CSS_ID = 'FONT_FACE_CSS'; |
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.
constant for css class names moved to ruler.dart
lib/font_face_observer.dart
Outdated
int get uses => _uses; | ||
void set uses(int new_uses) { | ||
_uses = new_uses; | ||
element.attributes['uses'] = _uses.toString(); |
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.
Could be data-uses
, but meh.
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 kinda like the data attributes especially since this isn't a custom element. I'll revamp to use that.
Ok, updated to use data attributes. Dart makes it super easy to read/write data attributes with the dataset Map on Element. so #winning. |
test/font_face_observer_test.dart
Outdated
@@ -70,6 +71,55 @@ main() { | |||
expect(result.isLoaded, isTrue); | |||
}); | |||
|
|||
test('should not leave temp DOM nodes after detecting', () async { |
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.
Which part of this test is "detecting"? ffo.load
? isLoaded
?
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.
.load
loads the fonts and waits for it to be loaded. Should I change the wording in the test desc to "load" ?
test/font_face_observer_test.dart
Outdated
expect(styleElement.dataset['uses'],'1'); | ||
|
||
|
||
}); |
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.
Might be nice to unload multiple times and see if count goes negative.
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.
It will be removed once it is less than 1 and is covered by the test test('should unload a font by key', () async {
lib/font_face_observer.dart
Outdated
var loadedFont = _loadedFonts[k]; | ||
if (loadedFont.group == group || | ||
(group == "" && loadedFont.group == null)) { | ||
keysToRemove.add(k); |
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.
It's very surprising that passing null into this function will unload all fonts that were created without specifying a group. We chatted offline about this.
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 updated to require a group, it uses a default value if you specify none.
lib/font_face_observer.dart
Outdated
return loadedFont != null && loadedFont.uses > 0; | ||
} | ||
|
||
static int unloadGroup(String group) { |
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.
docstrings pls. Specifically, what should the return value represent? And what are invalid inputs for group
?
lib/font_face_observer.dart
Outdated
FontLoadResult r = await _result.future; | ||
if (r.isLoaded) { | ||
_LoadedFont loadedFont = _getLoadedFont(url); | ||
loadedFont.uses++; | ||
} |
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, what if the result isn't loaded? Should this add a then
to increment uses
when it is loaded?
lib/font_face_observer.dart
Outdated
String get testString => _testString; | ||
|
||
_LoadedFont _getLoadedFont(String url) { | ||
var styleElement; |
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.
Would be nice to have this typed, even as just Object or dynamic. EDIT: Why is it even outside the scope of the else
?
lib/font_face_observer.dart
Outdated
|
||
FontFaceObserver(String this.family, | ||
{String this.style: _NORMAL, | ||
String this.weight: _NORMAL, | ||
String this.stretch: _NORMAL, | ||
String testString: DEFAULT_TEST_STRING, | ||
int this.timeout: DEFAULT_TIMEOUT, | ||
bool this.useSimulatedLoadEvents: false}) { | ||
bool this.useSimulatedLoadEvents: false, | ||
String this.group: ""}) { |
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.
As we discussed offline, use a default group and then add validation if group is customized that it must not be null or empty.
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.
Let's clean up grouping, plus some docstrings, then g2g.
lib/font_face_observer.dart
Outdated
bool get isLoaded { | ||
var loadedFont = _loadedFonts[key]; | ||
return loadedFont != null && loadedFont.uses > 0; | ||
} | ||
|
||
/// A list of font keys for all currently loaded fonts | ||
static List<String> get loadedFontKeys { | ||
return _loadedFonts.keys.toList(growable: false); |
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.
Probably better to return this as a Set
, because won't users most likely do a membership check? Or, asking another way, does the order really matter? If not, a set would be a more efficient way to check if a font is in the list of loaded keys.
lib/font_face_observer.dart
Outdated
loadedGroups.add(loadedFont.group); | ||
} | ||
}); | ||
return loadedGroups; |
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.
If this function returned a Set
, it would be easier to code (no need to check if it already contains), plus it would guarantee that the groups are unique (and thus no need for the docstring addendum).
lib/font_face_observer.dart
Outdated
/// A list of font keys for all currently loaded fonts | ||
static List<String> get loadedFontKeys { | ||
return _loadedFonts.keys.toList(growable: false); | ||
} |
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 should return a Set too
+1 CR with suggestions to use a Set |
+1 still |
lib/font_face_observer.dart
Outdated
} | ||
|
||
/// A list of groups that the currently loaded fonts are in | ||
/// There will not be duplicate group entries if there are multiple fonts | ||
/// in the same group. | ||
static Iterable<String> get loadedGroups { | ||
List<String> loadedGroups = []; | ||
Set<String> loadedGroups = new Set<String>(); | ||
_loadedFonts.keys.forEach((k) { | ||
var loadedFont = _loadedFonts[k]; | ||
if (!loadedGroups.contains(loadedFont.group)) { |
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.
No need to do this check now that it's a Set
+1 CR |
QA +1
Merging into master. |
@Workiva/release-management-pp - Rosie, will you automerge? |
+1 from RM |
run_merge_script |
Problem
There is no way to unload fonts once they are loaded
Solution
Track loaded fonts by key and group and provide a way to unload a single font by unique key or multiple fonts by group.
Bonus changes: each font is loaded with a separate style tag with attributes for key, group, and the number of uses.
Areas of Impact
Test Overview (+10 Instructions)
pub get
andpub serve test
Versioning