Skip to content

Instantly share code, notes, and snippets.

@zkochan
Last active November 8, 2015 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zkochan/1a137a69042e22a91758 to your computer and use it in GitHub Desktop.
Save zkochan/1a137a69042e22a91758 to your computer and use it in GitHub Desktop.
requirebin sketch
'use strict';
var magicHook = require('magic-hook');
function Logger() {
magicHook(this, ['log']);
}
Logger.prototype.log = function(msg) {
console.log(msg);
};
var logger = new Logger();
logger.pre('log', function(next, msg) {
console.log('Hello world');
next(msg);
});
logger.log('la-la');
require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({"magic-hook":[function(require,module,exports){"use strict";function magicHook(obj,methods){if(typeof obj!=="object"){throw new Error("obj should be an object")}if(arguments.length>1&&!(methods instanceof Array)){throw new Error("methods should be an Array")}if(typeof obj.pre!=="undefined"){throw new Error("Hooked object already has a pre property")}var pres={};if(!methods){methods=Object.getOwnPropertyNames(obj).filter(function(propName){return typeof obj[propName]==="function"})}function hook(name,fn){pres[name]=pres[name]||[];obj[name]=function(){var current=-1;function next(){current++;if(pres[name].length<=current){return fn.apply(obj,arguments)}var args=Array.prototype.slice.call(arguments);return pres[name][current].apply(obj,[next].concat(args))}return next.apply(obj,arguments)}}for(var i=methods.length;i--;){hook(methods[i],obj[methods[i]])}obj.pre=function(name,fn){if(typeof name!=="string"){throw new Error("name should be a string")}if(typeof fn!=="function"){throw new Error("fn should be a function")}if(!pres[name]){throw new Error("There's no hook with the passed name")}pres[name].push(fn)};obj.removePre=function(name,fnToRemove){if(typeof name!=="string"){throw new Error("name should be a string")}if(!obj[name]){return}if(arguments.length===1){pres[name]=[];return}pres[name]=pres[name].filter(function(currFn){return currFn!==fnToRemove})}}module.exports=magicHook},{}]},{},[]);"use strict";var magicHook=require("magic-hook");function Logger(){magicHook(this,["log"])}Logger.prototype.log=function(msg){console.log(msg)};var logger=new Logger;logger.pre("log",function(next,msg){console.log("Hello world");next(msg)});logger.log("la-la");
{
"name": "requirebin-sketch",
"version": "1.0.0",
"dependencies": {
"magic-hook": "0.1.0"
}
}
<!-- contents of this file will be placed inside the <body> -->
<!-- contents of this file will be placed inside the <head> -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment