question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Trying to understand the inner workings of the codebase: where is "position" defined?

See original GitHub issue

I was looking at the example codepen shared here https://codesandbox.io/s/baklavajs-example-jyc6f?file=/src/App.vue:2032-2040

And I see a method that takes in coordinates along with a Node type:

    methods: {
        addNodeWithCoordinates(nodeType, x, y) {
            const n = new nodeType();
            this.editor.addNode(n);
            n.position.x = x;
            n.position.y = y;
            return n;
        }
    }

I also see that node instances have positions defined when looking at nodes in the web dev tools. However, when I do a search where Nodes are defined (node.d.ts) I do not see “position”. I also cannot find “position” when I search all my libraries related to baklavajs.

Does anyone know where position comes from? I think learning about this will help me understand a lot more about inheritance, perhaps.

The reason I ask originally came from me trying Typescript for fun, but having trouble defining types properly in order to effectively use it. For example, in my Editor.vue I have

        getPosition(node) {
            return [node.position.x, node.position.y]
        },

but when I try to define node like

        getPosition(node: Node) {
            console.log(node)
            return [node.position.x, node.position.y]
        },

TypeScript tells me that Node does not have “position”. I saw that too by looking into node.d.ts. But… then how does it exist?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
MSoupcommented, Dec 24, 2021

@newcat Oooh!! Thank you so much!

Using BaklavaJS is teaching me much more than any course I’ve taken 😉 The freedom to be able to build my own node generators, make my own Node classes, etc feels like I’m playing a video game.

Edit: does this mean I need to cast the type node:IViewNode?

0reactions
newcatcommented, Dec 24, 2021

Your nodeType would be nodeType: new () => Node in this case.

Then I’d just use the IViewNode for setting the view-related properties like this:

(n as unknown as IViewNode).customClasses = n.name

This separation between interfaces and actual implementations has historical reasons and won’t exist in Baklava V2 anymore, but for now we have to live with it unfortunately.

Edit: You might be able to cast n to something like this:

const n = new nodeType() as Node & IViewNode

but I can’t test it right now

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do you dive into large code bases?
I realize that becoming acquainted with a code base is a process that happens over time, and familiarity can mean anything from "I'm...
Read more >
How do you orient to a "new to you" code base?
Read the docs - get the most inconvenient thing out of the way first; Read the specs or latest customer requests Try and...
Read more >
What is a code base? - YouTube
In this video, Codecademy team member Mike Dane defines the frequently used but infrequently explained term: code base !
Read more >
How Do I Understand a Complex Codebase At Work?
I just started at a new company. How do I learn to work with their code? How do I get up to speed...
Read more >
Codebase at my work is a complete mess, what should I do?
Once you have a decent understanding of how and why the code is the way it is, and the trust of your teammates,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found