How to use the @lwc/shared.defineProperty 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 / node-reactions / src / global / init.ts View on Github external
function initialize() {
    let init = (DocumentConstructor as any)[InitializationSlot];
    if (isUndefined(init)) {
        patchDomApi();
        init = () => {
            return {
                connected: reactWhenConnected,
                disconnected: reactWhenDisconnected,
            };
        };
        // Defined as an arrow function to avoid anybody walking the prototype chain from
        // accidentally discovering the cached apis
        defineProperty(DocumentConstructor, InitializationSlot, { value: init });
    }
    const cachedApis = init();
    reactWhenConnectedCached = cachedApis.connected;
    reactWhenDisconnectedCached = cachedApis.disconnected;
}
github salesforce / lwc / packages / @lwc / synthetic-shadow / src / shared / node-ownership.ts View on Github external
export function setNodeOwnerKey(node: Node, value: number) {
    if (process.env.NODE_ENV !== 'production') {
        // in dev-mode, we are more restrictive about what you can do with the owner key
        defineProperty(node, OwnerKey, {
            value,
            configurable: true,
        });
    } else {
        // in prod, for better perf, we just let it roll
        node[OwnerKey] = value;
    }
}
github salesforce / lwc / packages / @lwc / synthetic-shadow / src / faux-shadow / events.ts View on Github external
let propagationStopped = false;
    const { type, stopImmediatePropagation, stopPropagation } = evt;
    // currentTarget is always defined
    const currentTarget = eventCurrentTargetGetter.call(evt) as EventTarget;
    const listenerMap = getEventMap(currentTarget);
    const listeners = listenerMap![type] as WrappedListener[]; // it must have listeners at this point
    defineProperty(evt, 'stopImmediatePropagation', {
        value() {
            immediatePropagationStopped = true;
            stopImmediatePropagation.call(evt);
        },
        writable: true,
        enumerable: true,
        configurable: true,
    });
    defineProperty(evt, 'stopPropagation', {
        value() {
            propagationStopped = true;
            stopPropagation.call(evt);
        },
        writable: true,
        enumerable: true,
        configurable: true,
    });
    // in case a listener adds or removes other listeners during invocation
    const bookkeeping: WrappedListener[] = ArraySlice.call(listeners);

    function invokeListenersByPlacement(placement: EventListenerContext) {
        forEach.call(bookkeeping, (listener: WrappedListener) => {
            if (isFalse(immediatePropagationStopped) && listener.placement === placement) {
                // making sure that the listener was not removed from the original listener queue
                if (ArrayIndexOf.call(listeners, listener) !== -1) {
github salesforce / lwc / packages / @lwc / synthetic-shadow / src / polyfills / document-shadow / polyfill.ts View on Github external
const elements = arrayFromCollection(
            documentQuerySelectorAll.apply(this, ArraySlice.call(arguments) as [string])
        );
        const filtered = ArrayFilter.call(
            elements,
            // TODO [#1222]: remove global bypass
            elm => isUndefined(getNodeOwnerKey(elm)) || isGlobalPatchingSkipped(elm)
        );
        return createStaticNodeList(filtered);
    },
    writable: true,
    enumerable: true,
    configurable: true,
});

defineProperty(Document.prototype, 'getElementsByClassName', {
    value(this: Document): HTMLCollectionOf<element> {
        const elements = arrayFromCollection(
            documentGetElementsByClassName.apply(this, ArraySlice.call(arguments) as [string])
        );
        const filtered = ArrayFilter.call(
            elements,
            // TODO [#1222]: remove global bypass
            elm =&gt; isUndefined(getNodeOwnerKey(elm)) || isGlobalPatchingSkipped(elm)
        );
        return createStaticHTMLCollection(filtered);
    },
    writable: true,
    enumerable: true,
    configurable: true,
});
</element>
github salesforce / lwc / packages / @lwc / synthetic-shadow / src / faux-shadow / events.ts View on Github external
function domListener(evt: Event) {
    patchEvent(evt);
    let immediatePropagationStopped = false;
    let propagationStopped = false;
    const { type, stopImmediatePropagation, stopPropagation } = evt;
    // currentTarget is always defined
    const currentTarget = eventCurrentTargetGetter.call(evt) as EventTarget;
    const listenerMap = getEventMap(currentTarget);
    const listeners = listenerMap![type] as WrappedListener[]; // it must have listeners at this point
    defineProperty(evt, 'stopImmediatePropagation', {
        value() {
            immediatePropagationStopped = true;
            stopImmediatePropagation.call(evt);
        },
        writable: true,
        enumerable: true,
        configurable: true,
    });
    defineProperty(evt, 'stopPropagation', {
        value() {
            propagationStopped = true;
            stopPropagation.call(evt);
        },
        writable: true,
        enumerable: true,
        configurable: true,
github salesforce / lwc / packages / @lwc / synthetic-shadow / src / faux-shadow / node.ts View on Github external
}
        : function(node: Node): NodeListOf {
              return node.childNodes;
          };

// IE11 extra patches for wrong prototypes
if (hasOwnProperty.call(HTMLElement.prototype, 'contains')) {
    defineProperty(
        HTMLElement.prototype,
        'contains',
        getOwnPropertyDescriptor(Node.prototype, 'contains') as PropertyDescriptor
    );
}

if (hasOwnProperty.call(HTMLElement.prototype, 'parentElement')) {
    defineProperty(
        HTMLElement.prototype,
        'parentElement',
        getOwnPropertyDescriptor(Node.prototype, 'parentElement') as PropertyDescriptor
    );
}
github salesforce / lwc / packages / @lwc / synthetic-shadow / src / faux-shadow / element.ts View on Github external
}
            return lastElementChildGetter.call(this);
        },
        enumerable: true,
        configurable: true,
    },
    assignedSlot: {
        get: assignedSlotGetterPatched,
        enumerable: true,
        configurable: true,
    },
});

// IE11 extra patches for wrong prototypes
if (hasOwnProperty.call(HTMLElement.prototype, 'innerHTML')) {
    defineProperty(
        HTMLElement.prototype,
        'innerHTML',
        getOwnPropertyDescriptor(Element.prototype, 'innerHTML') as PropertyDescriptor
    );
}
if (hasOwnProperty.call(HTMLElement.prototype, 'outerHTML')) {
    defineProperty(
        HTMLElement.prototype,
        'outerHTML',
        getOwnPropertyDescriptor(Element.prototype, 'outerHTML') as PropertyDescriptor
    );
}
if (hasOwnProperty.call(HTMLElement.prototype, 'children')) {
    defineProperty(
        HTMLElement.prototype,
        'children',
github salesforce / lwc / packages / @lwc / synthetic-shadow / src / shared / static-html-collection.ts View on Github external
forEach.call(items, (item: T, index: number) => {
        defineProperty(collection, index, {
            value: item,
            enumerable: true,
            configurable: true,
        });
    });
    return collection;
github salesforce / lwc / packages / @lwc / synthetic-shadow / src / polyfills / focus-event / polyfill.ts View on Github external
* SPDX-License-Identifier: MIT
 * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
 */
import { defineProperty, isNull, getOwnPropertyDescriptor } from '@lwc/shared';
import { pathComposer } from '../../3rdparty/polymer/path-composer';
import { retarget } from '../../3rdparty/polymer/retarget';
import { eventCurrentTargetGetter } from '../../env/dom';
import { isNodeShadowed } from '../../shared/node-ownership';
import { getOwnerDocument } from '../../shared/utils';

const focusEventRelatedTargetGetter = getOwnPropertyDescriptor(
    FocusEvent.prototype,
    'relatedTarget'
)!.get as () => EventTarget | null;

defineProperty(FocusEvent.prototype, 'relatedTarget', {
    get(this: Event): EventTarget | null | undefined {
        const relatedTarget = focusEventRelatedTargetGetter.call(this);
        if (isNull(relatedTarget)) {
            return null;
        }
        if (!(relatedTarget instanceof Node) || !isNodeShadowed(relatedTarget as Node)) {
            return relatedTarget;
        }
        let pointOfReference = eventCurrentTargetGetter.call(this);
        if (isNull(pointOfReference)) {
            pointOfReference = getOwnerDocument(relatedTarget as Node);
        }
        return retarget(pointOfReference, pathComposer(relatedTarget, true));
    },
    enumerable: true,
    configurable: true,
github salesforce / lwc / packages / @lwc / synthetic-shadow / src / polyfills / mutation-observer / polyfill.ts View on Github external
return originalObserve.call(this, target, options);
}

/**
 * Patch the takeRecords() api to filter MutationRecords based on the observed targets
 */
function patchedTakeRecords(this: MutationObserver): MutationRecord[] {
    return filterMutationRecords(originalTakeRecords.call(this), this);
}

PatchedMutationObserver.prototype = OriginalMutationObserver.prototype;
PatchedMutationObserver.prototype.disconnect = patchedDisconnect;
PatchedMutationObserver.prototype.observe = patchedObserve;
PatchedMutationObserver.prototype.takeRecords = patchedTakeRecords;

defineProperty(window, 'MutationObserver', {
    value: PatchedMutationObserver,
    configurable: true,
    writable: true,
});