Skip to content

Instantly share code, notes, and snippets.

@benaubin
Last active April 28, 2016 22:38
Show Gist options
  • Save benaubin/3cb12975ebe8c1f4b88111dcabb918cc to your computer and use it in GitHub Desktop.
Save benaubin/3cb12975ebe8c1f4b88111dcabb918cc to your computer and use it in GitHub Desktop.
spy.js - Need to watch calls to your functions? Or calls to THEIR functions? Spy on them.
(function () {
(function () {
var Spy;
Spy = function () {
function Spy(original, callback1) {
var s;
this.original = original;
this.callback = callback1;
s = this;
this.callback || (this.callback = function (_this) {
return function (args, result) {
return console.log(args, result);
};
}(this));
this.status = true;
this.stop = function (_this) {
return function () {
return !(_this.status = false);
};
}(this);
this.start = function (_this) {
return function () {
return _this.status = true;
};
}(this);
this.spy = function () {
var result;
result = s.original.apply(this, arguments);
if (s.status) {
s.callback(arguments, result);
}
return result;
};
this.spy.spy = this;
}
return Spy;
}();
window.spy = function (object, functionName, callback) {
var s;
s = new Spy(object[functionName], callback);
object[functionName] = s.spy;
return s;
};
return Function.prototype.spy = function (callback) {
return new Spy(this, callback).spy;
};
}());
}.call(this));
[incoming message]
[connecting]
[connected]
Hello?
I don't got much time - so let's make this quick.
I'm a spy from the land of CoffeeScriptia.
I can watch calls to any Javascript function.
I've sent you some example code. It should have appeared below this message.
Until we meet again, goodbye. I'm off to make debugging easier.
```

```text
[disconnected]
```

```text
You picked up a [CodePen with spy.js], and a [CodePen with an example].
```

-------

Items:

- [CodePen with spy.js] and spy.coffee
- [CodePen with an example]

-------

Code by [penne12].

[penne12]: github.com/penne12
[CodePen with spy.js]: http://codepen.io/penne12/pen/mPzqGK?editors=0012
[CodePen with an example]: http://codepen.io/penne12/pen/PNyQqN?editors=0012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment