Permalink
Browse files

cache into its own file

  • Loading branch information...
1 parent 7b71292 commit 4327ec8afdf9cab946730e50d603113788b42988 @iamnoah committed May 20, 2012
Showing with 143 additions and 136 deletions.
  1. +49 −0 cache.coffee
  2. +88 −0 cache.js
  3. +0 −49 inject-core.coffee
  4. +1 −83 inject-core.js
  5. +3 −2 inject.coffee
  6. +2 −2 inject.js
View
@@ -0,0 +1,49 @@
+# cache offers a simple mechanism for creating (and clearing) singletons
+# without caching, the injected values are recreated/resolved each time
+Inject.cache = ->
+ results = {}
+
+ singleton = (name,fn) ->
+ cachedFactory = (args...) ->
+ array = results[name] || (results[name] = [])
+ result = matchArgs(array,args || [])
+
+ unless result
+ result = value: fn.apply(this,args), args: args
+ array.push(result);
+
+ result.value;
+
+ singleton.def = (name,fn,eager) ->
+ name: name
+ eager: eager
+ factory: this(name,fn)
+
+ singleton.clear = (keys...)->
+ if keys.length
+ for key in keys
+ if key.args
+ matchArgs(results[key.name],key.args,true)
+ else
+ delete results[key]
+ else
+ results = {}
+
+ singleton
+
+matchArgs = (results,args,del) ->
+ return unless results
+
+ for result, i in results
+ miss = find result.args || [], (index,arg) ->
+ args[index] isnt arg
+
+ unless miss
+ if del
+ delete result[i]
+ return result
+
+find = (array,fn,context) ->
+ fn ?= (it) -> it
+ for value, index in array
+ return value if fn.call(context,value,index)
View
@@ -0,0 +1,88 @@
+// Generated by CoffeeScript 1.3.3
+(function() {
+ var find, matchArgs,
+ __slice = [].slice;
+
+ Inject.cache = function() {
+ var results, singleton;
+ results = {};
+ singleton = function(name, fn) {
+ var cachedFactory;
+ return cachedFactory = function() {
+ var args, array, result;
+ args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
+ array = results[name] || (results[name] = []);
+ result = matchArgs(array, args || []);
+ if (!result) {
+ result = {
+ value: fn.apply(this, args),
+ args: args
+ };
+ array.push(result);
+ }
+ return result.value;
+ };
+ };
+ singleton.def = function(name, fn, eager) {
+ return {
+ name: name,
+ eager: eager,
+ factory: this(name, fn)
+ };
+ };
+ singleton.clear = function() {
+ var key, keys, _i, _len, _results;
+ keys = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
+ if (keys.length) {
+ _results = [];
+ for (_i = 0, _len = keys.length; _i < _len; _i++) {
+ key = keys[_i];
+ if (key.args) {
+ _results.push(matchArgs(results[key.name], key.args, true));
+ } else {
+ _results.push(delete results[key]);
+ }
+ }
+ return _results;
+ } else {
+ return results = {};
+ }
+ };
+ return singleton;
+ };
+
+ matchArgs = function(results, args, del) {
+ var i, miss, result, _i, _len;
+ if (!results) {
+ return;
+ }
+ for (i = _i = 0, _len = results.length; _i < _len; i = ++_i) {
+ result = results[i];
+ miss = find(result.args || [], function(index, arg) {
+ return args[index] !== arg;
+ });
+ if (!miss) {
+ if (del) {
+ delete result[i];
+ }
+ return result;
+ }
+ }
+ };
+
+ find = function(array, fn, context) {
+ var index, value, _i, _len;
+ if (fn == null) {
+ fn = function(it) {
+ return it;
+ };
+ }
+ for (index = _i = 0, _len = array.length; _i < _len; index = ++_i) {
+ value = array[index];
+ if (fn.call(context, value, index)) {
+ return value;
+ }
+ }
+ };
+
+}).call(this);
View
@@ -138,39 +138,6 @@ noContext = ->
throw new Error("""There is no current injector.
You need to call this inside an injected function or an inject.useCurrent function.""")
-# cache offers a simple mechanism for creating (and clearing) singletons
-# without caching, the injected values are recreated/resolved each time
-# TODO plugin
-cache = inject.cache = ->
- results = {}
-
- singleton = (name,fn) ->
- cachedFactory = (args...) ->
- array = results[name] || (results[name] = [])
- result = matchArgs(array,args || [])
-
- unless result
- result = value: fn.apply(this,args), args: args
- array.push(result);
-
- result.value;
-
- singleton.def = (name,fn,eager) ->
- name: name
- eager: eager
- factory: this(name,fn)
-
- singleton.clear = (keys...)->
- if keys.length
- for key in keys
- if key.args
- matchArgs(results[key.name],key.args,true)
- else
- delete results[key]
- else
- results = {}
-
- singleton
whenInjected = (resolver,definition) ->
destroyed = false
@@ -223,18 +190,6 @@ mapper = (config) ->
mapProperty = (property) ->
config?.inject?[property] || property
-matchArgs = (results,args,del) ->
- return unless results
-
- for result, i in results
- miss = find result.args || [], (index,arg) ->
- args[index] isnt arg
-
- unless miss
- if del
- delete result[i]
- return result
-
exports.Inject = inject
@@ -297,7 +252,3 @@ groupBy = (array,fn) ->
last = (array) ->
array?[array?.length - 1]
-find = (array,fn,context) ->
- fn ?= (it) -> it
- for value, index in array
- return value if fn.call(context,value,index)
View
@@ -7,7 +7,7 @@
(function() {
- var CONTEXT, D, PLUGINS, andReturn, bind, cache, error, exports, find, getClass, getName, groupBy, inDef, inject, injectUnbound, last, mapper, matchArgs, noContext, pluginSupport, useInjector, whenInjected, window,
+ var CONTEXT, D, PLUGINS, andReturn, bind, error, exports, getClass, getName, groupBy, inject, injectUnbound, last, mapper, noContext, pluginSupport, useInjector, whenInjected, window,
__slice = [].slice;
window = this;
@@ -182,54 +182,6 @@
throw new Error("There is no current injector.\nYou need to call this inside an injected function or an inject.useCurrent function.");
};
- cache = inject.cache = function() {
- var results, singleton;
- results = {};
- singleton = function(name, fn) {
- var cachedFactory;
- return cachedFactory = function() {
- var args, array, result;
- args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
- array = results[name] || (results[name] = []);
- result = matchArgs(array, args || []);
- if (!result) {
- result = {
- value: fn.apply(this, args),
- args: args
- };
- array.push(result);
- }
- return result.value;
- };
- };
- singleton.def = function(name, fn, eager) {
- return {
- name: name,
- eager: eager,
- factory: this(name, fn)
- };
- };
- singleton.clear = function() {
- var key, keys, _i, _len, _results;
- keys = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
- if (keys.length) {
- _results = [];
- for (_i = 0, _len = keys.length; _i < _len; _i++) {
- key = keys[_i];
- if (key.args) {
- _results.push(matchArgs(results[key.name], key.args, true));
- } else {
- _results.push(delete results[key]);
- }
- }
- return _results;
- } else {
- return results = {};
- }
- };
- return singleton;
- };
-
whenInjected = function(resolver, definition) {
var destroyed, injector, injectorFor;
destroyed = false;
@@ -319,25 +271,6 @@
};
};
- matchArgs = function(results, args, del) {
- var i, miss, result, _i, _len;
- if (!results) {
- return;
- }
- for (i = _i = 0, _len = results.length; _i < _len; i = ++_i) {
- result = results[i];
- miss = find(result.args || [], function(index, arg) {
- return args[index] !== arg;
- });
- if (!miss) {
- if (del) {
- delete result[i];
- }
- return result;
- }
- }
- };
-
exports.Inject = inject;
/*
@@ -410,19 +343,4 @@
return array != null ? array[(array != null ? array.length : void 0) - 1] : void 0;
};
- find = function(array, fn, context) {
- var index, value, _i, _len;
- if (fn == null) {
- fn = function(it) {
- return it;
- };
- }
- for (index = _i = 0, _len = array.length; _i < _len; index = ++_i) {
- value = array[index];
- if (fn.call(context, value, index)) {
- return value;
- }
- }
- };
-
}).call(this);
View
@@ -1,4 +1,5 @@
if steal.plugins
- steal.plugins('jquery')('//inject/inject-core.js')('//inject/controller.js')
+ steal.plugins('jquery')('//inject/inject-core.js')('//inject/controller.js').
+ then('//inject/cache.js')
else
- steal('jquery','jquery/lang','./inject-core.js','./controller.js')
+ steal('jquery','jquery/lang','./inject-core.js','./controller.js').then('./cache.js')
View
@@ -2,9 +2,9 @@
(function() {
if (steal.plugins) {
- steal.plugins('jquery', 'jquery/lang')('inject-core.js','controller.js');
+ steal.plugins('jquery')('//inject/inject-core.js')('//inject/controller.js').then('//inject/cache.js');
} else {
- steal('jquery', 'jquery/lang', './inject-core.js','./controller.js');
+ steal('jquery', 'jquery/lang', './inject-core.js', './controller.js').then('./cache.js');
}
}).call(this);

0 comments on commit 4327ec8

Please sign in to comment.