All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.src.magics.js Maven / Gradle / Ivy

There is a newer version: 3.14.1
Show newest version
import { getElementBoundUtilities } from './directives'
import { interceptor } from './interceptor'
import { onElRemoved } from './mutation'

let magics = {}

export function magic(name, callback) {
    magics[name] = callback
}

export function injectMagics(obj, el) {
    Object.entries(magics).forEach(([name, callback]) => {
        let memoizedUtilities = null;
        function getUtilities() {
            if (memoizedUtilities) {
                return memoizedUtilities;
            } else {
                let [utilities, cleanup] = getElementBoundUtilities(el)

                memoizedUtilities = {interceptor, ...utilities}

                onElRemoved(el, cleanup)
                return memoizedUtilities;
            }
        }

        Object.defineProperty(obj, `$${name}`, {
            get() {
                return callback(el, getUtilities());
            },
            enumerable: false,
        })
    })

    return obj
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy