kaleidawave / prism

(No longer in development). Experimental compiler for building isomorphic web applications with web components.
MIT License
110 stars 1 forks source link

Element retrieval using indexes rather than identifiers #31

Open kaleidawave opened 3 years ago

kaleidawave commented 3 years ago

Currently elements that need to be retrieved at runtime (for events and bindings) are given a identifier at compile time. Subsequent code gen uses that identifier for building expressions that refer to that element. The identifier starts with p and then several random characters (identifiers are checked across components not to collide although it is not guaranteed across foreign components). For example the following button will get a class with something like p1234:

<template>
    <button @click="alert">{someText}</button>
</template>
..

After analysis the button will become something like <button class="p1234">..</button>

This is so that during hydration adding the event listener is as simple as this.querySelector(".p1234").addEventListener(..) or during runtime if the value of someText is altered then this.querySelector(".p1234").innerText = ... There is also a Map on the component instance which caches them so repeated accesses don't go looking for elements again. This abstraction is through the method Component.getElem

This was a simple early implementation in Prism but there are some issues with this:

A solution:

Instead of adding identifiers and generating lookups via identifiers, elements could be retrieved through getting their index in the tree.

<template>
    <div>
        <div>
            <button @click="alert">{someText}</button>
        </div>
    </div>
</template>

Get element by index:

const button = this.children[0].children[0].children[0]

The indexes can be calculated at build time. This is already the process for referencing elements in for loops.

Things to consider: