-
Notifications
You must be signed in to change notification settings - Fork 141
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
Caching stuff #208
Open
scjackson
wants to merge
14
commits into
master
Choose a base branch
from
sam/lru-cache
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Caching stuff #208
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
bacc9d4
adding basic lru logic
836b4e6
comments/formatting
3c940f5
addGetterOptions
5351733
add return type to comment
da98531
clear cache values
5025e2c
Getter, delete from cache on observe
28cc547
comments
372f8b7
more tests, fixes
fe630e9
formatting
69e6904
adding default max
ce555ed
removing duplicate packages
2ea41bb
fixing lint errors
90b35c6
removing Getter object
5aef5eb
fixing lint errors
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
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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
try { | ||
if (!(window.console && console.log)) { | ||
if (!(window.console && console.log)) { // eslint-disable-line | ||
console = { | ||
log: function(){}, | ||
debug: function(){}, | ||
info: function(){}, | ||
warn: function(){}, | ||
error: function(){} | ||
}; | ||
log: function() {}, | ||
debug: function() {}, | ||
info: function() {}, | ||
warn: function() {}, | ||
error: function() {}, | ||
} | ||
} | ||
} catch(e) {} | ||
} catch(e) {} // eslint-disable-line |
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
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
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
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 |
---|---|---|
|
@@ -2,7 +2,7 @@ import Immutable from 'immutable' | |
import createReactMixin from './create-react-mixin' | ||
import * as fns from './reactor/fns' | ||
import { isKeyPath } from './key-path' | ||
import { isGetter } from './getter' | ||
import { isGetter, isGetterObject } from './getter' | ||
import { toJS } from './immutable-helpers' | ||
import { toFactory } from './utils' | ||
import { | ||
|
@@ -12,6 +12,8 @@ import { | |
PROD_OPTIONS, | ||
} from './reactor/records' | ||
|
||
export const DEFAULT_MAX_ITEMS_TO_CACHE = 1000 | ||
|
||
/** | ||
* State is stored in NuclearJS Reactors. Reactors | ||
* contain a 'state' object which is an Immutable.Map | ||
|
@@ -24,11 +26,24 @@ import { | |
class Reactor { | ||
constructor(config = {}) { | ||
const debug = !!config.debug | ||
const useCache = config.useCache === undefined ? true : !!config.useCache | ||
|
||
// use default # of items in cache | ||
let maxItemsToCache = DEFAULT_MAX_ITEMS_TO_CACHE | ||
|
||
// if user has defined maxItemsToCache, use the number provide or set to null (ie no max) | ||
if (config.maxItemsToCache !== undefined) { | ||
maxItemsToCache = Number(config.maxItemsToCache) | ||
maxItemsToCache = maxItemsToCache && maxItemsToCache > 0 ? maxItemsToCache : null | ||
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. minor: is there a difference between |
||
} | ||
|
||
const baseOptions = debug ? DEBUG_OPTIONS : PROD_OPTIONS | ||
const initialReactorState = new ReactorState({ | ||
debug: debug, | ||
maxItemsToCache: maxItemsToCache, | ||
// merge config options with the defaults | ||
options: baseOptions.merge(config.options || {}), | ||
useCache: useCache, | ||
}) | ||
|
||
this.prevReactorState = initialReactorState | ||
|
@@ -88,19 +103,23 @@ class Reactor { | |
let { observerState, entry } = fns.addObserver(this.observerState, getter, handler) | ||
this.observerState = observerState | ||
return () => { | ||
this.observerState = fns.removeObserverByEntry(this.observerState, entry) | ||
let [ observerState, reactorState ] = fns.removeObserverByEntry(this.observerState, this.reactorState, entry) | ||
this.observerState = observerState | ||
this.reactorState = reactorState | ||
} | ||
} | ||
|
||
unobserve(getter, handler) { | ||
if (arguments.length === 0) { | ||
throw new Error('Must call unobserve with a Getter') | ||
} | ||
if (!isGetter(getter) && !isKeyPath(getter)) { | ||
if (!isGetterObject(getter) && !isGetter(getter) && !isKeyPath(getter)) { | ||
throw new Error('Must call unobserve with a Getter') | ||
} | ||
|
||
this.observerState = fns.removeObserver(this.observerState, getter, handler) | ||
let [ observerState, reactorState ] = fns.removeObserver(this.observerState, this.reactorState, getter, handler) | ||
this.observerState = observerState | ||
this.reactorState = reactorState | ||
} | ||
|
||
/** | ||
|
Oops, something went wrong.
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.
I think this would be better as a pseudo-constructor. IE I dont like the idea of changing the Getter type because now we have to handle both a
Getter
instance and a plain array getter in every interface.Instead
Getter(['items'])
returning simply['items']
makes a lot of sense to me for backwards compat and simplicity.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 basically what i had before?
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.
yes, but called
Getter
instead ofaddOptionsToGetter
. The latter name implied that you could mutate an existing Getter (potentially globally) versus a function to create a Getter.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.
Ok --
addOptionsToGetter
was by ref. Which would modify it globally. We could return a cloned copy with options added instead. Thoughts?