How to use the belter/src.memoize function in belter

To help you get started, we’ve selected a few belter 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 paypal / paypal-checkout-components / src / lib / meta.js View on Github external
/* @flow */

import { ZalgoPromise } from 'zalgo-promise/src';
import { once, bridge } from 'post-robot/src';
import { isIEIntranet, memoize } from 'belter/src';

import { FUNDING } from '../constants';
import { URLS, DOMAINS } from '../config';

type FrameMetaData = {
    iframeEligible : boolean,
    iframeEligibleReason : string,
    rememberedFunding : Array<$Values>
};

export let openMetaFrame = memoize(() : ZalgoPromise => {
    return ZalgoPromise.try(() => {

        if (isIEIntranet()) {
            return {
                iframeEligible:       false,
                iframeEligibleReason: 'ie_intranet',
                rememberedFunding:    []
            };
        }

        let metaFrameUrl : string = URLS.META;
        let metaFrameDomain : string = DOMAINS.META;

        return ZalgoPromise.try(() => {
            if (!bridge) {
                throw new Error(`Opening meta window without bridge support is not currently supported`);
github paypal / paypal-smart-payment-buttons / src / button / props / onApprove.js View on Github external
function buildXApproveActions({ intent, orderID, paymentID, payerID, restart, subscriptionID, facilitatorAccessToken, buyerAccessToken, partnerAttributionID, forceRestAPI } : ActionOptions) : XOnApproveActionsType {

    // Subscription GET Actions
    const getSubscriptionApi = memoize(() => getSubscription(subscriptionID, { buyerAccessToken }));
    const activateSubscriptionApi = memoize(() => activateSubscription(subscriptionID, { buyerAccessToken }));

    const redirect = (url) => {
        if (!url) {
            throw new Error(`Expected redirect url`);
        }

        if (url.indexOf('://') === -1) {
            getLogger().warn('redir_url_non_scheme', { url }).flush();
            throw new Error(`Invalid redirect url: ${ url } - must be fully qualified url`);
        } else if (!url.match(/^https?:\/\//)) {
            getLogger().warn('redir_url_non_http', { url }).flush();
        }

        return redir(url, window.top);
    };
github paypal / paypal-smart-payment-buttons / src / button / props / onApprove.js View on Github external
function buildXApproveActions({ intent, orderID, paymentID, payerID, restart, subscriptionID, facilitatorAccessToken, buyerAccessToken, partnerAttributionID, forceRestAPI } : ActionOptions) : XOnApproveActionsType {

    // Subscription GET Actions
    const getSubscriptionApi = memoize(() => getSubscription(subscriptionID, { buyerAccessToken }));
    const activateSubscriptionApi = memoize(() => activateSubscription(subscriptionID, { buyerAccessToken }));

    const redirect = (url) => {
        if (!url) {
            throw new Error(`Expected redirect url`);
        }

        if (url.indexOf('://') === -1) {
            getLogger().warn('redir_url_non_scheme', { url }).flush();
            throw new Error(`Invalid redirect url: ${ url } - must be fully qualified url`);
        } else if (!url.match(/^https?:\/\//)) {
            getLogger().warn('redir_url_non_http', { url }).flush();
        }

        return redir(url, window.top);
    };
github paypal / paypal-checkout-components / test / integration / windows / button / index.jsx View on Github external
function renderCheckout(props = {}, context = CONTEXT.POPUP) {
    let approved = false;

    const payment = memoize(() => {
        if (window.xprops.createSubscription) {
            return window.xprops.createSubscription({}, {}).then(generateOrderID);
        } else if (window.xprops.createBillingAgreement) {
            return window.xprops.createBillingAgreement({}, {}).then(generateOrderID);
        } else if (window.xprops.createOrder) {
            return window.xprops.createOrder({}, {
                order: {
                    create: generateOrderID
                }
            });
        } else {
            return ZalgoPromise.try(generateOrderID);
        }
    });

    window.paypal.Checkout({
github paypal / paypal-smart-payment-buttons / src / button / props / onApprove.js View on Github external
const isProcessorDecline = err && err.data && err.data.details && err.data.details.some(detail => {
            return detail.issue === ORDER_API_ERROR.INSTRUMENT_DECLINED || detail.issue === ORDER_API_ERROR.PAYER_ACTION_REQUIRED;
        });

        if (isProcessorDecline) {
            return restart().then(unresolvedPromise);
        }

        throw new Error('Order could not be captured');
    };
    
    const get = memoize(() => {
        return getOrder(orderID, { facilitatorAccessToken, buyerAccessToken, partnerAttributionID, forceRestAPI });
    });

    const capture = memoize(() => {
        if (intent !== INTENT.CAPTURE) {
            throw new Error(`Use ${ SDK_QUERY_KEYS.INTENT }=${ INTENT.CAPTURE } to use client-side capture`);
        }

        return captureOrder(orderID, { facilitatorAccessToken, buyerAccessToken, partnerAttributionID, forceRestAPI })
            .finally(get.reset)
            .finally(capture.reset)
            .catch(handleProcessorError);
    });

    const authorize = memoize(() => {
        if (intent !== INTENT.AUTHORIZE) {
            throw new Error(`Use ${ SDK_QUERY_KEYS.INTENT }=${ INTENT.AUTHORIZE } to use client-side authorize`);
        }

        return authorizeOrder(orderID, { facilitatorAccessToken, buyerAccessToken, partnerAttributionID, forceRestAPI })
github paypal / paypal-smart-payment-buttons / src / button / props / onApprove.js View on Github external
const get = memoize(() => {
        return getOrder(orderID, { facilitatorAccessToken, buyerAccessToken, partnerAttributionID, forceRestAPI });
    });

    const capture = memoize(() => {
        if (intent !== INTENT.CAPTURE) {
            throw new Error(`Use ${ SDK_QUERY_KEYS.INTENT }=${ INTENT.CAPTURE } to use client-side capture`);
        }

        return captureOrder(orderID, { facilitatorAccessToken, buyerAccessToken, partnerAttributionID, forceRestAPI })
            .finally(get.reset)
            .finally(capture.reset)
            .catch(handleProcessorError);
    });

    const authorize = memoize(() => {
        if (intent !== INTENT.AUTHORIZE) {
            throw new Error(`Use ${ SDK_QUERY_KEYS.INTENT }=${ INTENT.AUTHORIZE } to use client-side authorize`);
        }

        return authorizeOrder(orderID, { facilitatorAccessToken, buyerAccessToken, partnerAttributionID, forceRestAPI })
            .finally(get.reset)
            .finally(authorize.reset)
            .catch(handleProcessorError);
    });

    const patch = (data = {}) => {
        return patchOrder(orderID, data, { facilitatorAccessToken, buyerAccessToken, partnerAttributionID, forceRestAPI }).catch(() => {
            throw new Error('Order could not be patched');
        });
    };
github paypal / paypal-smart-payment-buttons / src / menu / interface.js View on Github external
export function renderSmartMenu({ clientID } : SmartMenuProps) : SmartMenu {

    const { Menu } = paypal;

    if (!Menu) {
        throw new Error(`Menu component not found`);
    }

    const { renderTo, updateProps, show, hide } = Menu({ clientID });

    const render = memoize(() => {
        return renderTo(window.xprops.getParent(), '#smart-menu');
    });

    const display = ({ choices, verticalOffset, onChoose }) => {
        return render().then(() => {
            return updateProps({
                verticalOffset,
                choices,
                onChoose: ({ id, win }) => {
                    hide();
                    return onChoose({ id, win });
                }
            });
        }).then(() => {
            return show();
        });
github krakenjs / zoid / dist / module / component / window.js View on Github external
if (!name) {
        throw new Error('Expected component name');
    }

    if (!encodedPayload) {
        throw new Error('Expected encoded payload');
    }

    try {
        return JSON.parse(base64decode(encodedPayload));
    } catch (err) {
        throw new Error('Can not decode window name payload: ' + encodedPayload + ': ' + stringifyError(err));
    }
});

export var isZoidComponentWindow = memoize(function () {
    try {
        parseChildWindowName();
    } catch (err) {
        return false;
    }

    return true;
});
github paypal / paypal-smart-payment-buttons / src / button / props / onClick.js View on Github external
export function getOnClick(xprops : XProps) : OnClick | void {
    const { onClick } = xprops;

    if (!onClick) {
        return;
    }

    return memoize(({ fundingSource } : { fundingSource : $Values }) => {
        return onClick(buildXOnClickData({ fundingSource }), buildXOnClickActions()).then(valid => {
            return (valid !== CLICK_VALID.INVALID);
        });
    });
}