How to use the @lwc/shared.defineProperties function in @lwc/shared

To help you get started, we’ve selected a few @lwc/shared examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github salesforce / lwc / packages / @lwc / synthetic-shadow / src / polyfills / event-target / polyfill.ts View on Github external
]);
    } else {
        superRemoveEventListener.apply(this, args as [
            string,
            EventListener,
            (EventListenerOptions | boolean | undefined)?
        ]);
    }
}

// IE11 doesn't have EventTarget, so we have to patch it conditionally
// on the wrong prototypes.
const protoToBePatched =
    typeof EventTarget !== 'undefined' ? EventTarget.prototype : Node.prototype;

defineProperties(protoToBePatched, {
    addEventListener: {
        value: addEventListenerPatched,
        enumerable: true,
        writable: true,
        configurable: true,
    },
    removeEventListener: {
        value: removeEventListenerPatched,
        enumerable: true,
        writable: true,
        configurable: true,
    },
});
github salesforce / lwc / packages / @lwc / synthetic-shadow / src / faux-shadow / node.ts View on Github external
*  2. The host identification logic returns null in two cases:
 *     i. The node does not belong to a shadow tree created by engine
 *     ii. The engine is running in native shadow dom mode
 *     If so, use the original Node.prototype.getRootNode to fetch the root node(or manually climb up the dom tree where getRootNode() is unsupported)
 *
 * _Spec_: https://dom.spec.whatwg.org/#dom-node-getrootnode
 *
 **/
function getRootNodePatched(this: Node, options?: GetRootNodeOptions): Node {
    const composed: boolean = isUndefined(options) ? false : !!options.composed;
    return isTrue(composed) ? getDocumentOrRootNode.call(this, options) : getNearestRoot(this);
}

// Non-deep-traversing patches: this descriptor map includes all descriptors that
// do not give access to nodes beyond the immediate children.
defineProperties(Node.prototype, {
    firstChild: {
        get(this: Node): ChildNode | null {
            if (hasMountedChildren(this)) {
                return firstChildGetterPatched.call(this);
            }
            return firstChildGetter.call(this);
        },
        enumerable: true,
        configurable: true,
    },
    lastChild: {
        get(this: Node): ChildNode | null {
            if (hasMountedChildren(this)) {
                return lastChildGetterPatched.call(this);
            }
            return lastChildGetter.call(this);
github salesforce / lwc / packages / @lwc / node-reactions / src / dom-patching / node.ts View on Github external
export default function() {
    // caching few DOM APIs
    const { appendChild, insertBefore, removeChild, replaceChild } = Node.prototype;

    const isConnectedGetter = getOwnPropertyDescriptor(Node.prototype, 'isConnected')!.get!;

    defineProperties(Node.prototype, {
        appendChild: {
            writable: true,
            enumerable: true,
            configurable: true,
            value: function(this: Node, aChild: Node) {
                if (!isQualifyingElement(aChild)) {
                    return appendChild.call(this, aChild);
                }

                let qualifiedReactionTypes: QualifyingReactionTypes = 0;
                if (isConnectedGetter.call(aChild)) {
                    qualifiedReactionTypes = qualifiedReactionTypes | 2;
                }
                // Get the children before appending
                const qualifyingChildren = (aChild as Element | DocumentFragment).querySelectorAll(
                    `[${marker}]`
github salesforce / lwc / packages / @lwc / engine / src / framework / def.ts View on Github external
if (!isNull(superDef)) {
        props = assign(create(null), superDef.props, props);
        methods = assign(create(null), superDef.methods, methods);
        wire = superDef.wire || wire ? assign(create(null), superDef.wire, wire) : undefined;
        track = assign(create(null), superDef.track, track);
        connectedCallback = connectedCallback || superDef.connectedCallback;
        disconnectedCallback = disconnectedCallback || superDef.disconnectedCallback;
        renderedCallback = renderedCallback || superDef.renderedCallback;
        errorCallback = errorCallback || superDef.errorCallback;
        render = render || superDef.render;
        template = template || superDef.template;
    }
    props = assign(create(null), HTML_PROPS, props);

    if (!isUndefined(fields)) {
        defineProperties(proto, createObservedFieldsDescriptorMap(fields));
    }

    if (isUndefined(template)) {
        // default template
        template = defaultEmptyTemplate;
    }

    const def: ComponentDef = {
        ctor: Ctor,
        name,
        wire,
        track,
        props,
        methods,
        bridge,
        template,
github salesforce / lwc / packages / @lwc / engine / src / framework / base-bridge-element.ts View on Github external
get: createGetter(propName),
            set: createSetter(propName),
            enumerable: true,
            configurable: true,
        };
    }
    // expose public methods as props on the new Element Bridge
    for (let i = 0, len = methods.length; i < len; i += 1) {
        const methodName = methods[i];
        descriptors[methodName] = {
            value: createMethodCaller(methodName),
            writable: true,
            configurable: true,
        };
    }
    defineProperties(HTMLBridgeElement.prototype, descriptors);
    return HTMLBridgeElement as HTMLElementConstructor;
}
github salesforce / lwc / packages / @lwc / synthetic-shadow / src / faux-shadow / event-target.ts View on Github external
this: Element,
    type: string,
    listener: EventListener,
    options?: boolean | AddEventListenerOptions
) {
    if (isHostElement(this)) {
        removeCustomElementEventListener(this, type, listener, options);
    } else {
        superRemoveEventListener.call(this, type, listener, options);
    }
}

// IE11 doesn't have EventTarget, so we have to patch it conditionally:

if (typeof EventTarget !== 'undefined') {
    defineProperties(EventTarget.prototype, {
        addEventListener: {
            value: addEventListenerPatched,
            enumerable: true,
            writable: true,
            configurable: true,
        },
        removeEventListener: {
            value: removeEventListenerPatched,
            enumerable: true,
            writable: true,
            configurable: true,
        },
    });
} else {
    // IE11 extra patches for wrong prototypes
    defineProperties(Node.prototype, {
github salesforce / lwc / packages / @lwc / synthetic-shadow / src / faux-shadow / slot.ts View on Github external
* if it doesn't have a parent node,
     * or the parent is not an slot element
     * or they both belong to the same template (default content)
     * we should assume that it is not slotted
     */
    if (
        isNull(parentNode) ||
        !isSlotElement(parentNode) ||
        getNodeNearestOwnerKey(parentNode) === getNodeNearestOwnerKey(this)
    ) {
        return null;
    }
    return parentNode as HTMLSlotElement;
}

defineProperties(HTMLSlotElement.prototype, {
    addEventListener: {
        value(
            this: HTMLSlotElement,
            type: string,
            listener: EventListener,
            options?: boolean | AddEventListenerOptions
        ) {
            // super.addEventListener - but that doesn't work with typescript
            HTMLElement.prototype.addEventListener.call(this, type, listener, options);
            if (type === 'slotchange' && !getHiddenField(this, SlotChangeKey)) {
                setHiddenField(this, SlotChangeKey, true);
                if (!observer) {
                    observer = initSlotObserver();
                }
                MutationObserverObserve.call(observer, this as Node, observerConfig);
            }
github salesforce / lwc / packages / @lwc / synthetic-shadow / src / faux-shadow / events.ts View on Github external
export function patchEvent(event: Event) {
    if (eventToContextMap.has(event)) {
        return; // already patched
    }
    defineProperties(event, {
        target: {
            get: targetGetter,
            enumerable: true,
            configurable: true,
        },
        composedPath: {
            value: composedPathValue,
            writable: true,
            enumerable: true,
            configurable: true,
        },
        // non-standard but important accessor
        srcElement: {
            get: targetGetter,
            enumerable: true,
            configurable: true,
github salesforce / lwc / packages / @lwc / synthetic-shadow / src / polyfills / event-listener / polyfill.ts View on Github external
function removeEventListener(this: EventTarget, type, fnOrObj, optionsOrCapture) {
    const wrapperFn = getEventListenerWrapper(fnOrObj);
    nativeRemoveEventListener.call(this, type, wrapperFn || fnOrObj, optionsOrCapture);
}

// TODO [#1305]: these patches should be on EventTarget.prototype instead of win and node prototypes
//       but IE doesn't support that.
window.addEventListener = windowAddEventListener;
window.removeEventListener = windowRemoveEventListener;

// IE11 doesn't have EventTarget, so we have to patch it conditionally:

const protoToBePatched =
    typeof EventTarget !== 'undefined' ? EventTarget.prototype : Node.prototype;

defineProperties(protoToBePatched, {
    addEventListener: {
        value: addEventListener,
        enumerable: true,
        writable: true,
        configurable: true,
    },
    removeEventListener: {
        value: removeEventListener,
        enumerable: true,
        writable: true,
        configurable: true,
    },
});