@apollo-orbit/angular
Version:
A full-featured GraphQL client for Angular
157 lines (149 loc) • 7.05 kB
JavaScript
import * as i2 from '@apollo-orbit/core';
import { getActionType, flatten, resolveDispatchResults, MutationManager, partition, addStateToClient, addStateToCache } from '@apollo-orbit/core';
export { state } from '@apollo-orbit/core';
import { filter, map, Observable, Subject } from 'rxjs';
import * as i0 from '@angular/core';
import { inject, Injectable, makeEnvironmentProviders, ENVIRONMENT_INITIALIZER } from '@angular/core';
import * as i1 from '@apollo-orbit/angular';
import { ɵApolloRegistry as _ApolloRegistry, Apollo, ɵAPOLLO_INSTANCE_FACTORY as _APOLLO_INSTANCE_FACTORY } from '@apollo-orbit/angular';
import { tap } from 'rxjs/operators';
function ofActionDispatched(...actions) {
const actionMap = createActionMap(actions);
return source => source.pipe(filter(ctx => ctx.status === 'dispatched' && actionMap[getActionType(ctx.action)]), map(({ action }) => action));
}
function ofActionSuccess(...actions) {
const actionMap = createActionMap(actions);
return source => source.pipe(filter(ctx => ctx.status === 'success' && actionMap[getActionType(ctx.action)]), map(({ action }) => action));
}
function ofActionError(...actions) {
const actionMap = createActionMap(actions);
return source => source.pipe(filter(ctx => ctx.status === 'error' && actionMap[getActionType(ctx.action)]), map(({ action }) => action));
}
function ofActionComplete(...actions) {
const actionMap = createActionMap(actions);
const statuses = ['success', 'error'];
return source => source.pipe(filter((ctx) => statuses.includes(ctx.status) && actionMap[getActionType(ctx.action)]));
}
function createActionMap(actions) {
return actions.reduce((acc, action) => ({ ...acc, [action.type]: true }), {});
}
class ApolloActions extends Observable {
registry = inject(_ApolloRegistry);
actions = new Subject();
constructor() {
super(subscriber => this.actions.subscribe(subscriber));
}
dispatch(action) {
this.actions.next({ action, status: 'dispatched' });
return Promise.all(this.registry.instances
.map(apollo => apollo['manager'].dispatch(action, { cache: apollo.client.cache, dispatch: this.dispatch.bind(this) }))) // eslint-disable-line dot-notation
.then(flatten)
.then(results => {
for (const result of results) {
this.actions.next(result);
}
return resolveDispatchResults(results);
});
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.9", ngImport: i0, type: ApolloActions, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.9", ngImport: i0, type: ApolloActions });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.9", ngImport: i0, type: ApolloActions, decorators: [{
type: Injectable
}], ctorParameters: () => [] });
class ɵApollo extends Apollo {
manager;
constructor(client, manager, defaultOptions) {
super(client, defaultOptions);
this.manager = manager;
}
mutate(options) {
const { manager } = this;
return super.mutate(manager.wrapMutationOptions(options)).pipe(tap({
next: result => manager.runEffects(options, result, undefined),
error: error => manager.runEffects(options, undefined, error)
}));
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.9", ngImport: i0, type: ɵApollo, deps: "invalid", target: i0.ɵɵFactoryTarget.Injectable });
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.9", ngImport: i0, type: ɵApollo });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.9", ngImport: i0, type: ɵApollo, decorators: [{
type: Injectable
}], ctorParameters: () => [{ type: i1.ApolloClient }, { type: i2.MutationManager }, { type: undefined }] });
class StateManager {
clients = {};
pending = [];
/**
* Create a mutation manager for an apollo client
*/
createManager(clientId, client) {
if (this.clients[clientId] !== undefined)
throw new Error(`Apollo clients with duplicate options.id: '${clientId}'`);
const manager = new MutationManager();
this.clients[clientId] = { client, manager };
const [current, pending] = partition(this.pending, state => state.clientId === clientId);
this.pending = pending;
this.addState(client, manager, ...current);
return manager;
}
onAddStates(states) {
for (const state of states) {
const pair = this.clients[state.clientId];
if (pair) {
const { client, manager } = pair;
this.addState(client, manager, state);
}
else {
this.pending = [...this.pending, state];
}
}
}
addState(client, manager, ...states) {
const addToClient = addStateToClient(client);
const addToCache = addStateToCache(client.cache);
states.forEach(state => {
addToClient(state);
addToCache(state);
manager.addState(state);
state.onInit?.(client.cache);
});
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.9", ngImport: i0, type: StateManager, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.9", ngImport: i0, type: StateManager });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.9", ngImport: i0, type: StateManager, decorators: [{
type: Injectable
}] });
function withState(...states) {
return {
kind: 'APOLLO_ORBIT_STATES',
providers: [
ApolloActions,
StateManager,
{ provide: _APOLLO_INSTANCE_FACTORY, useFactory: apolloInstanceFactory, deps: [StateManager] },
getStatesProviders(states)
]
};
}
function provideStates(...states) {
return makeEnvironmentProviders(getStatesProviders(states));
}
function getStatesProviders(states) {
return states.length > 0
? [{ provide: ENVIRONMENT_INITIALIZER, multi: true, useValue: () => addStates(states) }]
: [];
}
function apolloInstanceFactory(stateManager) {
return (clientId, client, defaultOptions) => {
const manager = stateManager.createManager(clientId, client);
return new ɵApollo(client, manager, defaultOptions); // eslint-disable-line new-cap
};
}
function addStates(states) {
inject(StateManager).onAddStates(states.map(state => typeof state === 'function' ? state() : state));
}
/**
* Generated bundle index. Do not edit.
*/
export { ApolloActions, ofActionComplete, ofActionDispatched, ofActionError, ofActionSuccess, provideStates, withState };
//# sourceMappingURL=apollo-orbit.angular.state.mjs.map