123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- import { notifyManager } from './notifyManager.mjs';
- import { Mutation } from './mutation.mjs';
- import { matchMutation, noop } from './utils.mjs';
- import { Subscribable } from './subscribable.mjs';
- // CLASS
- class MutationCache extends Subscribable {
- constructor(config) {
- super();
- this.config = config || {};
- this.mutations = [];
- this.mutationId = 0;
- }
- build(client, options, state) {
- const mutation = new Mutation({
- mutationCache: this,
- logger: client.getLogger(),
- mutationId: ++this.mutationId,
- options: client.defaultMutationOptions(options),
- state,
- defaultOptions: options.mutationKey ? client.getMutationDefaults(options.mutationKey) : undefined
- });
- this.add(mutation);
- return mutation;
- }
- add(mutation) {
- this.mutations.push(mutation);
- this.notify({
- type: 'added',
- mutation
- });
- }
- remove(mutation) {
- this.mutations = this.mutations.filter(x => x !== mutation);
- this.notify({
- type: 'removed',
- mutation
- });
- }
- clear() {
- notifyManager.batch(() => {
- this.mutations.forEach(mutation => {
- this.remove(mutation);
- });
- });
- }
- getAll() {
- return this.mutations;
- }
- find(filters) {
- if (typeof filters.exact === 'undefined') {
- filters.exact = true;
- }
- return this.mutations.find(mutation => matchMutation(filters, mutation));
- }
- findAll(filters) {
- return this.mutations.filter(mutation => matchMutation(filters, mutation));
- }
- notify(event) {
- notifyManager.batch(() => {
- this.listeners.forEach(({
- listener
- }) => {
- listener(event);
- });
- });
- }
- resumePausedMutations() {
- var _this$resuming;
- this.resuming = ((_this$resuming = this.resuming) != null ? _this$resuming : Promise.resolve()).then(() => {
- const pausedMutations = this.mutations.filter(x => x.state.isPaused);
- return notifyManager.batch(() => pausedMutations.reduce((promise, mutation) => promise.then(() => mutation.continue().catch(noop)), Promise.resolve()));
- }).then(() => {
- this.resuming = undefined;
- });
- return this.resuming;
- }
- }
- export { MutationCache };
- //# sourceMappingURL=mutationCache.mjs.map
|