Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

Commit

Permalink
Merge pull request #6 from jonsterling/ready/5
Browse files Browse the repository at this point in the history
Memoize feature detection (resolve #5)
  • Loading branch information
jdegoes committed Sep 8, 2015
2 parents cfd1013 + e889aff commit ba8a025
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/DOM/BrowserFeatures/Detectors.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
// module DOM.BrowserFeatures.Detectors

var _inputTypeSupportMemoTable = {};

exports._detectInputTypeSupport = function(type) {
return function() {
if (_inputTypeSupportMemoTable.hasOwnProperty(type)) {
return _inputTypeSupportMemoTable[type];
}

var el = document.createElement("input");

try {
Expand All @@ -10,6 +16,9 @@ exports._detectInputTypeSupport = function(type) {
return false;
}

return el.type === type;
var result = el.type === type;
_inputTypeSupportMemoTable[type] = result;

return result;
};
};
13 changes: 10 additions & 3 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import DOM.BrowserFeatures.Detectors

main :: Eff (dom :: DOM, console :: CONSOLE) Unit
main = do
features <- detectBrowserFeatures
void $ for IT.allInputTypes \ty ->
log $ show ty ++ if features.inputTypeSupported ty then " supported" else " not supported"
queryFeatureSupport
log "Will query feature support again, using memoized results"
queryFeatureSupport
log "Done"

where
queryFeatureSupport = do
features <- detectBrowserFeatures
void $ for IT.allInputTypes \ty ->
log $ show ty ++ if features.inputTypeSupported ty then " supported" else " not supported"

0 comments on commit ba8a025

Please sign in to comment.