typedefs?????
parent
f3c06ad1be
commit
d77b0956e9
@ -0,0 +1,11 @@
|
|||||||
|
[ignore]
|
||||||
|
|
||||||
|
[include]
|
||||||
|
|
||||||
|
[libs]
|
||||||
|
|
||||||
|
[lints]
|
||||||
|
|
||||||
|
[options]
|
||||||
|
|
||||||
|
[strict]
|
@ -0,0 +1 @@
|
|||||||
|
node_modules
|
@ -0,0 +1 @@
|
|||||||
|
"use strict";function monkeyPatch(e,t,o){var n=Symbol();const i=o.before,c=o.instead,f=o.after,r=new Proxy(t[e],{apply:(e,t,[n,r])=>{void 0!==i&&i.apply(n,r);const a=void 0!==o.instead?c.apply(n,[e.bind(n),...r]):e.apply(n,r);return void 0===f?a:f.apply(n,[a].concat(r))}}),a=t[e];t[e]=function(){return r(this,arguments)};var u=()=>{t[e]=a};return t[n]={name:e,orig:a,unpatch:u},u}function before(e,t,n){return monkeyPatch(e,t,{before:n})}function instead(e,t,n){return monkeyPatch(e,t,{instead:n})}function after(e,t,n){return monkeyPatch(e,t,{after:n})}module.exports={monkeyPatch:monkeyPatch,before:before,instead:instead,after:after};
|
@ -0,0 +1,49 @@
|
|||||||
|
// @flow
|
||||||
|
function monkeyPatch(name: string, parentObj: Object, patches: Object): ()=>void {
|
||||||
|
const injId = Symbol()
|
||||||
|
const before = patches["before"];
|
||||||
|
const instead = patches["instead"];
|
||||||
|
const after = patches["after"];
|
||||||
|
const handler = {
|
||||||
|
apply: (target, thisArg, [ctx, args]) => {
|
||||||
|
if (before !== undefined) before.apply(ctx, args);
|
||||||
|
const res = (patches["instead"] !== undefined) ? instead.apply(ctx, [target.bind(ctx), ...args]) : target.apply(ctx, args)
|
||||||
|
if (after === undefined) return res
|
||||||
|
return after.apply(ctx, [res].concat(args));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const prox = new Proxy(parentObj[name], handler);
|
||||||
|
const orig = parentObj[name];
|
||||||
|
parentObj[name] = function() {
|
||||||
|
return prox(this, arguments);
|
||||||
|
};
|
||||||
|
const unpatch = () => {
|
||||||
|
parentObj[name] = orig;
|
||||||
|
}
|
||||||
|
parentObj[injId] = {
|
||||||
|
name: name,
|
||||||
|
orig: orig,
|
||||||
|
unpatch: unpatch
|
||||||
|
};
|
||||||
|
return unpatch;
|
||||||
|
}
|
||||||
|
|
||||||
|
function before(name: string, parentObj: Object, func: (...args: mixed[])=>void): ()=>void {
|
||||||
|
return monkeyPatch(name, parentObj, {before: func})
|
||||||
|
}
|
||||||
|
|
||||||
|
function instead(name: string, parentObj: Object, func: (orig: (any)=>any, ...args: mixed[])=>any): ()=>void {
|
||||||
|
return monkeyPatch(name, parentObj, {instead: func})
|
||||||
|
}
|
||||||
|
|
||||||
|
function after(name: string, parentObj: Object, func: (res: any, ...args: mixed[])=>any): ()=>void {
|
||||||
|
return monkeyPatch(name, parentObj, {after: func})
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
monkeyPatch,
|
||||||
|
before,
|
||||||
|
instead,
|
||||||
|
after
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,11 @@
|
|||||||
|
import flow from 'rollup-plugin-flow'
|
||||||
|
import { uglify } from 'rollup-plugin-uglify'
|
||||||
|
|
||||||
|
export default {
|
||||||
|
input: 'src/patcher.js',
|
||||||
|
plugins: [flow(), uglify()],
|
||||||
|
output: {
|
||||||
|
file: 'dist/patcher.js',
|
||||||
|
format: "cjs"
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue