Created
November 2, 2019 21:54
-
-
Save tvcutsem/37c5fb9a39dca993c8ce3c5115e27678 to your computer and use it in GitHub Desktop.
layer-cake without generators
This file contains hidden or 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
// https://github.com/Agoric/layer-cake without generators | |
const { | |
create: objCreate, | |
getOwnPropertyDescriptors: gopds, | |
defineProperties, | |
prototype: objPrototype, | |
freeze, | |
} = Object; | |
function makeCakeMaker(combineLayers) { | |
return function makeCake(layerGens) { | |
let self = {}; | |
layerGens.forEach(layerGen => { | |
const supr = {}; | |
const layer = layerGen(self, supr); | |
Object.assign(supr, self); | |
combineLayers(self, layer); | |
}); | |
return self; | |
}; | |
} | |
const makeClassCake = makeCakeMaker((upper = {}, lower = {}) => | |
Object.assign(upper, lower) | |
); | |
function BasePointLayer(x, y) { | |
return (self) => ({ | |
getX() { return x; }, | |
getY() { return y; }, | |
toString() { return `<${self.getX()},${self.getY()}>`; }, | |
}); | |
} | |
function WobblyPointLayer(wobble) { | |
return (_self, supr) => ({ | |
getX() { return supr.getX() + wobble++; } | |
}); | |
} | |
function makeWobblyPoint(x, y, wobble) { | |
return makeClassCake([BasePointLayer(x, y), WobblyPointLayer(wobble)]); | |
} | |
let point = makeWobblyPoint(1,1,1); | |
console.log(point.toString()); // <2,1> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment