Skip to content

Caching stuff #208

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

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions docs/package.json
Original file line number Diff line number Diff line change
@@ -23,8 +23,6 @@
"nuclear-js": "^1.0.5",
"webpack": "^1.9.11",
"webpack-dev-server": "^1.9.0",
"grunt-concurrent": "^1.0.0",
"grunt-contrib-connect": "^0.10.1",
"remarkable": "^1.6.0",
"front-matter": "^1.0.0",
"glob": "^5.0.10",
16 changes: 8 additions & 8 deletions src/console-polyfill.js
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
2 changes: 1 addition & 1 deletion src/create-react-mixin.js
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ export default function(reactor) {
each(this.getDataBindings(), (getter, key) => {
const unwatchFn = reactor.observe(getter, (val) => {
this.setState({
[key]: val
[key]: val,
})
})

39 changes: 39 additions & 0 deletions src/getter.js
Original file line number Diff line number Diff line change
@@ -2,13 +2,50 @@ import Immutable, { List } from 'immutable'
import { isFunction, isArray } from './utils'
import { isKeyPath } from './key-path'

const CACHE_OPTIONS = ['default', 'always', 'never']

/**
* Getter helper functions
* A getter is an array with the form:
* [<KeyPath>, ...<KeyPath>, <function>]
*/
const identity = (x) => x

/**
* Add override options to a getter
* @param {getter} getter
* @param {object} options
* @param {boolean} options.cache
* @param {*} options.cacheKey
* @returns {getter}
*/
function Getter(getter, options={}) {
if (!isKeyPath(getter) && !isGetter(getter)) {
throw new Error('createGetter must be passed a keyPath or Getter')
}

if (getter.hasOwnProperty('__options')) {
throw new Error('Cannot reassign options to getter')
}

getter.__options = {}
getter.__options.cache = CACHE_OPTIONS.indexOf(options.cache) > -1 ? options.cache : 'default'
getter.__options.cacheKey = options.cacheKey !== undefined ? options.cacheKey : null
return getter
}

/**
* Retrieve an option from getter options
* @param {getter} getter
* @param {string} Name of option to retrieve
* @returns {*}
*/
function getGetterOption(getter, option) {
if (getter.__options) {
return getter.__options[option]
}
}

/**
* Checks if something is a getter literal, ex: ['dep1', 'dep2', function(dep1, dep2) {...}]
* @param {*} toTest
@@ -106,7 +143,9 @@ export default {
isGetter,
getComputeFn,
getFlattenedDeps,
getGetterOption,
getStoreDeps,
getDeps,
fromKeyPath,
Getter,
}
3 changes: 2 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import Reactor from './reactor'
import Immutable from 'immutable'
import { toJS, toImmutable, isImmutable } from './immutable-helpers'
import { isKeyPath } from './key-path'
import { isGetter } from './getter'
import { isGetter, Getter } from './getter'
import createReactMixin from './create-react-mixin'

export default {
@@ -17,4 +17,5 @@ export default {
toImmutable,
isImmutable,
createReactMixin,
Getter,
}
23 changes: 21 additions & 2 deletions src/reactor.js
Original file line number Diff line number Diff line change
@@ -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 > 0 ? maxItemsToCache : null
}

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,7 +103,9 @@ 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
}
}

@@ -100,7 +117,9 @@ class Reactor {
throw new Error('Must call unobserve with a Getter')
}

this.observerState = fns.removeObserver(this.observerState, getter, handler)
const { observerState, reactorState } = fns.removeObserver(this.observerState, this.reactorState, getter, handler)
this.observerState = observerState
this.reactorState = reactorState
}

/**
Loading