-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
1,146 additions
and
2 deletions.
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
(function(_,f,g){g=window.dcl;g=g.advices||(g.advices={});g.counter=f(window.dcl);}) | ||
(['../dcl'], function (dcl) { | ||
'use strict'; | ||
|
||
var Counter = new dcl(null, { | ||
declaredClass: 'dcl/advices/counter/Counter', | ||
constructor: function () { | ||
this.reset(); | ||
}, | ||
reset: function () { | ||
this.calls = this.errors = 0; | ||
}, | ||
advice: function () { | ||
var self = this; | ||
return { | ||
before: function () { | ||
++self.calls; | ||
}, | ||
after: function (args, result) { | ||
if (result instanceof Error) { | ||
++self.errors; | ||
} | ||
} | ||
}; | ||
} | ||
}); | ||
|
||
return function(){ return new Counter; }; | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
(function(_,f,g){g=window;g=g.dcl||(g.dcl={});g=g.advices||(g.advices={});g.flow=f();}) | ||
([], function () { | ||
'use strict'; | ||
|
||
var flowStack = [], flowCount = {}; | ||
|
||
return { | ||
advice: function (name) { | ||
return { | ||
before: function () { | ||
flowStack.push(name); | ||
if (flowCount[name]) { | ||
++flowCount[name]; | ||
} else { | ||
flowCount[name] = 1; | ||
} | ||
}, | ||
after: function () { | ||
--flowCount[name]; | ||
flowStack.pop(); | ||
} | ||
}; | ||
}, | ||
inFlowOf: function (name) { | ||
return flowCount[name]; | ||
}, | ||
getStack: function () { | ||
return flowStack; | ||
}, | ||
getCount: function () { | ||
return flowCount; | ||
} | ||
}; | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
(function(_,f,g){g=window;g=g.dcl||(g.dcl={});g=g.advices||(g.advices={});g.memoize=f();}) | ||
([], function () { | ||
'use strict'; | ||
|
||
return { | ||
advice: function (name, keyMaker) { | ||
return keyMaker ? | ||
{ | ||
around: function (sup) { | ||
return function () { | ||
var key = keyMaker(this, arguments), cache = this.__memoizerCache, dict; | ||
if (!cache) { | ||
cache = this.__memoizerCache = {}; | ||
} | ||
if (cache.hasOwnProperty(name)) { | ||
dict = cache[name]; | ||
} else { | ||
dict = cache[name] = {}; | ||
} | ||
if (dict.hasOwnProperty(key)) { | ||
return dict[key]; | ||
} | ||
return dict[key] = sup ? sup.apply(this, arguments) : void 0; | ||
}; | ||
} | ||
} : | ||
{ | ||
around: function (sup) { | ||
return function (first) { | ||
var cache = this.__memoizerCache, dict; | ||
if (!cache) { | ||
cache = this.__memoizerCache = {}; | ||
} | ||
if (cache.hasOwnProperty(name)) { | ||
dict = cache[name]; | ||
} else { | ||
dict = cache[name] = {}; | ||
} | ||
if (dict.hasOwnProperty(first)) { | ||
return dict[first]; | ||
} | ||
return dict[first] = sup ? sup.apply(this, arguments) : undefined; | ||
}; | ||
} | ||
}; | ||
}, | ||
guard: function (name) { | ||
return { | ||
after: function () { | ||
var cache = this.__memoizerCache; | ||
if (cache && name) { | ||
delete cache[name]; | ||
} else { | ||
this.__memoizerCache = {}; | ||
} | ||
} | ||
}; | ||
} | ||
}; | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
(function(_,f,g){g=window;g=g.dcl||(g.dcl={});g=g.advices||(g.advices={});g.time=f();}) | ||
([], function () { | ||
'use strict'; | ||
|
||
var uniq = 0; | ||
|
||
return function (name) { | ||
var inCall = 0, label = name || ('Timer #' + uniq++); | ||
return { | ||
before: function () { | ||
if (!(inCall++)) { | ||
console.time(label); | ||
} | ||
}, | ||
after: function () { | ||
if (!--inCall) { | ||
console.timeEnd(label); | ||
} | ||
} | ||
}; | ||
}; | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
(function(_,f,g){g=window;g=g.dcl||(g.dcl={});g=g.advices||(g.advices={});g.trace=f();}) | ||
([], function () { | ||
'use strict'; | ||
|
||
var lvl = 0; | ||
|
||
function rep (ch, n) { | ||
if (n < 1) { return ''; } | ||
if (n == 1) { return ch; } | ||
var h = rep(ch, Math.floor(n / 2)); | ||
return h + h + ((n & 1) ? ch : ''); | ||
|
||
} | ||
|
||
function pad (value, width, ch) { | ||
var v = value.toString(); | ||
return v + rep(ch || ' ', width - v.length); | ||
} | ||
|
||
return function (name, level) { | ||
return { | ||
before: function () { | ||
++lvl; | ||
console.log((level ? pad(lvl, 2 * lvl) : '') + this + ' => ' + | ||
name + '(' + Array.prototype.join.call(arguments, ', ') + ')'); | ||
}, | ||
after: function (args, result) { | ||
console.log((level ? pad(lvl, 2 * lvl) : '') + this + ' => ' + | ||
name + (result && result instanceof Error ? ' throws' : ' returns') + | ||
' ' + result); | ||
--lvl; | ||
} | ||
}; | ||
}; | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,193 @@ | ||
(function(_,f){window.advise=f();}) | ||
([], function () { | ||
'use strict'; | ||
|
||
function Node (parent) { | ||
this.parent = parent || this; | ||
} | ||
|
||
Node.prototype = { | ||
removeTopic: function (topic) { | ||
var n = 'next_' + topic, p = 'prev_' + topic; | ||
if (this[n] && this[p]) { | ||
this[n][p] = this[p]; | ||
this[p][n] = this[n]; | ||
} | ||
}, | ||
remove: function () { | ||
this.removeTopic('before'); | ||
this.removeTopic('around'); | ||
|
||
// remove & recreate around advices | ||
var parent = this.parent, next = this.next_around; | ||
this.removeTopic('after'); | ||
for (; next && next !== parent; next = next.next_around) { | ||
next.around = next.originalAround(next.prev_around.around); | ||
} | ||
}, | ||
addTopic: function (node, topic) { | ||
var n = 'next_' + topic, p = 'prev_' + topic, | ||
prev = node[p] = this[p] || this; | ||
node[n] = this; | ||
prev[n] = this[p] = node; | ||
}, | ||
addAdvice: function (advice) { | ||
var node = new Node(this); | ||
if (advice.before) { | ||
node.before = advice.before; | ||
this.addTopic(node, 'before'); | ||
} | ||
if (advice.around) { | ||
node.originalAround = advice.around; | ||
this.addTopic(node, 'around'); | ||
node.around = advice.around(node.prev_around.around || null); | ||
} | ||
if (advice.after) { | ||
node.after = advice.after; | ||
this.addTopic(node, 'after'); | ||
} | ||
return node; | ||
} | ||
}; | ||
|
||
Node.prototype.unadvise = Node.prototype.remove; | ||
|
||
function addNode (root, topic) { | ||
return function (f) { | ||
var node = new Node(root); | ||
node[topic] = f; | ||
root.addTopic(node, topic); | ||
}; | ||
} | ||
|
||
function makeStub (value) { | ||
var root = new Node(); | ||
if (value) { | ||
if (typeof value.advices == 'object') { | ||
var advices = value.advices; | ||
advices.before.forEach(addNode(root, 'before')); | ||
advices.after. forEach(addNode(root, 'after')); | ||
advices.around && addNode(root, 'around')(advices.around); | ||
} else { | ||
addNode(root, 'around')(value); | ||
} | ||
} | ||
function stub () { | ||
var result, thrown, p; | ||
// running the before chain | ||
for (p = root.prev_before; p && p !== root; p = p.prev_before) { | ||
p.before.apply(this, arguments); | ||
} | ||
// running the around chain | ||
if (root.prev_around && root.prev_around !== root) { | ||
try { | ||
result = root.prev_around.around.apply(this, arguments); | ||
} catch (error) { | ||
result = error; | ||
thrown = true; | ||
} | ||
} | ||
// running the after chain | ||
for (p = root.next_after; p && p !== root; p = p.next_after) { | ||
p.after.call(this, arguments, result, makeReturn, makeThrow); | ||
} | ||
if (thrown) { | ||
throw result; | ||
} | ||
return result; | ||
|
||
function makeReturn (value) { result = value; thrown = false; } | ||
function makeThrow (value) { result = value; thrown = true; } | ||
}; | ||
stub.node = root; | ||
return stub; | ||
} | ||
|
||
function convert (value, advice, instance, name, type) { | ||
if (!value || !(value.node instanceof Node)) { | ||
value = makeStub(value); | ||
value.node.instance = instance; | ||
value.node.name = name; | ||
value.node.type = type; | ||
} | ||
var node = value.node.addAdvice(advice); | ||
return {value: value, handle: node}; | ||
} | ||
|
||
function combineHandles (handles) { | ||
var handle = { | ||
remove: function () { | ||
handles.forEach(function (handle) { handle.remove(); }); | ||
} | ||
} | ||
handle.unadvise = handle.remove; | ||
return handle; | ||
} | ||
|
||
function advise (instance, name, advice) { | ||
var prop = getPropertyDescriptor(instance, name), handles = []; | ||
if (prop) { | ||
if (prop.get || prop.set) { | ||
var result; | ||
if (prop.get && advice.get) { | ||
result = convert(prop.get, advice.get, instance, name, 'get'); | ||
prop.get = result.value; | ||
handles.push(result.handle); | ||
} | ||
if (prop.set && advice.set) { | ||
result = convert(prop.set, advice.set, instance, name, 'set'); | ||
prop.set = result.value; | ||
handles.push(result.handle); | ||
} | ||
} else { | ||
if (prop.value && advice) { | ||
result = convert(prop.value, advice, instance, name, 'value'); | ||
prop.value = result.value; | ||
handles.push(result.handle); | ||
} | ||
} | ||
} else { | ||
prop = {writable: true, configurable: true, enumerable: true}; | ||
if (advice.get || advice.set) { | ||
if (advice.get) { | ||
result = convert(null, advice.get, instance, name, 'get'); | ||
prop.get = result.value; | ||
handles.push(result.handles); | ||
} | ||
if (advice.set) { | ||
result = convert(null, advice.set, instance, name, 'set'); | ||
prop.set = result.value; | ||
handles.push(result.handles); | ||
} | ||
} else { | ||
result = convert(null, advice, instance, name, 'value'); | ||
prop.value = result.value; | ||
handles.push(result.handles); | ||
} | ||
} | ||
Object.defineProperty(instance, name, prop); | ||
return combineHandles(handles); | ||
} | ||
|
||
// export | ||
|
||
advise.before = function(instance, name, f){ return advise(instance, name, {before: f}); }; | ||
advise.after = function(instance, name, f){ return advise(instance, name, {after: f}); }; | ||
advise.around = function(instance, name, f){ return advise(instance, name, {around: f}); }; | ||
advise.Node = Node; | ||
|
||
advise._instantiate = function(advice, previous, node){ return advice(previous); }; | ||
|
||
return advise; | ||
|
||
// copied from dcl.js so we can be independent | ||
function getPropertyDescriptor (o, name) { | ||
while (o && o !== Object.prototype) { | ||
if (o.hasOwnProperty(name)) { | ||
return Object.getOwnPropertyDescriptor(o, name); | ||
} | ||
o = Object.getPrototypeOf(o); | ||
} | ||
return null; | ||
} | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
(function(_,f,g){g=window.dcl;g=g.bases||(g.bases={});g.Mixer=f(window.dcl);}) | ||
(['../dcl'], function (dcl) { | ||
'use strict'; | ||
|
||
return dcl(null, { | ||
declaredClass: 'dcl/bases/Mixer', | ||
constructor: function (x) { | ||
Object.defineProperties(this, dcl.populatePropsNative({}, x)); | ||
} | ||
}); | ||
}); |
Oops, something went wrong.