Skip to content

Instantly share code, notes, and snippets.

@birm
Last active February 14, 2018 16:15
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 birm/c24bf3c5376b34a3707f71d3d0787292 to your computer and use it in GitHub Desktop.
Save birm/c24bf3c5376b34a3707f71d3d0787292 to your computer and use it in GitHub Desktop.
Coordinated Drawing on Annotools.
// call cloner with base and a list of items to clone (same type preferably) of the same type
function cloner(base, clones){
var handler = {
get(target, name, reciever){
if (typeof target[name] == "function"){
// call the function with args to all contexts
return function (...args){
clones.forEach((x) => x[name].apply(this, args));
// return whatever base returns
return target[name].apply(this, args)
}
} else {
// just return base context value if it's not a function
return target[name];
}
},
set(obj, prop, val) {
clones.forEach((x) => x[prop] = val);
obj[prop] = val;
return val;
}
}
return new Proxy(base, handler);
}
// call delayer to store then later apply
function delayer(base){
// add our delay functions/objects to base
base.__queue = [];
base.__apply_all = function(new_base){
base.__queue.forEach(function(instruction){
if (instruction[0]==="set"){
new_base[instruction[1]] = instruction[2];
}
else if (instruction[0]==="fcn"){
new_base[instruction[1]](...instruction[2]);
}
})
}
var handler = {
get(obj, prop, val){
// what if we're looking for queue or apply all?
if (prop === "__queue"){
return obj.__queue;
}
else if (prop === "__apply_all"){
return obj.__apply_all;
} else {
return function (...args){
obj.__queue.push(["fcn", prop, args]);
}
}
},
set(obj, prop, val) {
obj.__queue.push(["set", prop, val]);
}
}
return new Proxy(base, handler);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment