-
Notifications
You must be signed in to change notification settings - Fork 7
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
begin implementation #2
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,17 @@ | ||
const assert = require('assert') | ||
|
||
module.exports = cacheElement | ||
|
||
// Memoize a bel element | ||
// null -> null | ||
function cacheElement () { | ||
function cacheElement (fn) { | ||
const store = {} | ||
|
||
return function render () { | ||
const args = Array.from(arguments) | ||
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.
|
||
const argsAreTheSame = JSON.stringify(store.prev) === JSON.stringify(args) | ||
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 suspect this might be slow for larger data sets; I think we could get away with shallow equality and triple equals. Perhaps we should allow an optional comparison function in the |
||
|
||
if (argsAreTheSame) return store.el | ||
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. In order for this to work we can't return the |
||
|
||
store.prev = args | ||
store.el = fn.apply(this, args) | ||
return store.el | ||
} | ||
} | ||
|
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.
perhaps
assert()
ing the function is actually a function might be useful - a pattern I like is theassert.equal
call which takes two arguments + a custom error message that includes the name of the module where the error occured for better traces