import { Observable } from "rxjs";
import { ApolloLink } from "@apollo/client/link/core";
export function setContext(setter) {
    return new ApolloLink((operation, forward) => {
        const { ...request } = operation;
        return new 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();
            };
        });
    });
}
//# sourceMappingURL=index.js.map
