"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SetContextLink = void 0;
exports.setContext = setContext;
const rxjs_1 = require("rxjs");
const link_1 = require("@apollo/client/link");
/**
 * @deprecated
 * Use `SetContextLink` from `@apollo/client/link/context` instead.
 */
function setContext(setter) {
    return new SetContextLink(setter);
}
class SetContextLink extends link_1.ApolloLink {
    constructor(setter) {
        super((operation, forward) => {
            const { ...request } = operation;
            return new rxjs_1.Observable((observer) => {
                let handle;
                let closed = false;
                Promise.resolve(request)
                    .then((req) => setter(req, operation.getContext()))
                    .then(operation.setContext)
                    .then(() => {
                    // if the observer is already closed, no need to subscribe.
                    if (closed)
                        return;
                    handle = forward(operation).subscribe({
                        next: observer.next.bind(observer),
                        error: observer.error.bind(observer),
                        complete: observer.complete.bind(observer),
                    });
                })
                    .catch(observer.error.bind(observer));
                return () => {
                    closed = true;
                    if (handle)
                        handle.unsubscribe();
                };
            });
        });
    }
}
exports.SetContextLink = SetContextLink;
//# sourceMappingURL=index.cjs.map
