Last active
September 20, 2022 10:11
-
-
Save MarinPostma/7670b732fafa452cdd0a97a085bbbe35 to your computer and use it in GitHub Desktop.
POC lasy chisel entities
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
| abstract class ChiselEntity { | |
| __data: any; | |
| __ctx_id: number; | |
| public Ready: Promise.IThenable<any>; | |
| public isReady = false; | |
| } | |
| class Person extends ChiselEntity { | |
| // these just end up as dummies | |
| name: string; | |
| other: Person; | |
| // this is generated | |
| constructor(id, ctx) { | |
| super(); | |
| super.Ready = new Promise((resolve, reject) => { | |
| fetch_entity(id).then(val => { | |
| // profiling probe: report to the runtime how we got access to this object, so the runtime can make | |
| // smart decisions about smart loading entities next time. | |
| // deno.code.opSync("prof_probe", ctx); | |
| console.log(`called in the context: ${ctx}`); | |
| this.__data = val.data; | |
| this.__ctx_id = val.ctx_id; | |
| this.isReady = true; | |
| resolve(undefined) | |
| }); | |
| Object.defineProperty(this, "name", { | |
| get: () => { | |
| if (!this.isReady) { | |
| throw "not ready"; | |
| } | |
| return this.__data["name"]; | |
| } | |
| }); | |
| Object.defineProperty(this, "other", { | |
| get: () => { | |
| if (!this.isReady) { | |
| throw "not ready"; | |
| } | |
| return new Person(20, { | |
| type: "PropAccess", | |
| parent: this, | |
| prop: "other", | |
| }); | |
| } | |
| }); | |
| }) | |
| } | |
| } | |
| async function fetch_entity(id) { | |
| console.log(id) | |
| await new Promise(resolve => setTimeout(resolve, 1000)); | |
| return { | |
| data: { | |
| name: "marin", | |
| other: id + 1, | |
| }, | |
| ctx_id: getRandomInt(100), | |
| } | |
| } | |
| function getRandomInt(max) { | |
| return Math.floor(Math.random() * max); | |
| } | |
| let person = new Person(10, undefined); | |
| (async () => { | |
| await person.Ready; | |
| let other = person.other; | |
| console.log("here"); | |
| await other.Ready; | |
| console.log(other.name); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment