This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 141
Allow lazy fragment initializaiton through promise #94
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
|
@@ -12,7 +12,7 @@ var Pipe = (function (doc, perf) { //eslint-disable-line no-unused-vars, strict | |
return script; | ||
} | ||
} | ||
}; | ||
} | ||
function placeholder (index) { | ||
placeholders[index] = currentScript(); | ||
} | ||
|
@@ -46,21 +46,48 @@ var Pipe = (function (doc, perf) { //eslint-disable-line no-unused-vars, strict | |
start.parentNode.removeChild(start); | ||
end.parentNode.removeChild(end); | ||
script && require([script], function (i) { | ||
// Exposed fragment initialization Function/Promise | ||
var init = i && i.__esModule ? i.default : i; | ||
if (typeof init === 'function') { | ||
// capture initializaion cost of each fragment on the page | ||
if (perf && 'mark' in perf) { | ||
perf.mark(fragmentId); | ||
// early return | ||
if (!init) { | ||
return; | ||
} | ||
// check if User Timing API is supported | ||
var isUTSupported = perf && 'mark' in perf; | ||
|
||
function isPromise(obj) { | ||
return typeof obj === 'object' | ||
&& typeof obj.then === 'function'; | ||
} | ||
|
||
function measureInitCost(fragmentId, metricName) { | ||
if (!isUTSupported) { | ||
return; | ||
} | ||
perf.mark(fragmentId + '-end'); | ||
perf.measure(metricName + fragmentId, fragmentId, fragmentId + '-end'); | ||
// Clear the perf entries buffer after measuring | ||
perf.clearMarks(fragmentId); | ||
perf.clearMarks(fragmentId + '-end'); | ||
} | ||
|
||
(function executeAndCapture() { | ||
isUTSupported && perf.mark(fragmentId); | ||
// Check if the exposed function is a Promise to allow lazy rendering | ||
// Capture initializaion cost of each fragment on the page using User Timing API if available | ||
if (isPromise(init)) { | ||
init.then(function(module) { | ||
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. I think that |
||
module(node); | ||
measureInitCost(fragmentId, 'fragment-'); | ||
}); | ||
} else if (typeof init === 'function') { | ||
init(node); | ||
perf.mark(fragmentId + '-end'); | ||
perf.measure('fragment-' + fragmentId, fragmentId, fragmentId + '-end'); | ||
// Clear the perf entries buffer after measuring | ||
perf.clearMarks(fragmentId); | ||
perf.clearMarks(fragmentId + '-end'); | ||
measureInitCost(fragmentId, 'fragment-'); | ||
} else { | ||
init(node); | ||
// this case should be ideally caught by early return | ||
isUTSupported && perf.clearMarks(fragmentId); | ||
} | ||
} | ||
})(); | ||
}); | ||
} | ||
/* @preserve - loadCSS: load a CSS file asynchronously. [c]2016 @scottjehl, Filament Group, Inc. Licensed MIT */ | ||
|
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.
pull this out into a library function and import 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.
I would keep it simple.. will probably even inline it than keeping it as fn
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.
maybe, but it's probably something that should be covered by a test, so pulling it out into a library func is going to help with that.
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.
Yeah i didnt do that since its not used anywhere other than this one place..
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.
oki