mutationCache.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var notifyManager = require('./notifyManager.js');
  4. var mutation = require('./mutation.js');
  5. var utils = require('./utils.js');
  6. var subscribable = require('./subscribable.js');
  7. // CLASS
  8. class MutationCache extends subscribable.Subscribable {
  9. constructor(config) {
  10. super();
  11. this.config = config || {};
  12. this.mutations = [];
  13. this.mutationId = 0;
  14. }
  15. build(client, options, state) {
  16. const mutation$1 = new mutation.Mutation({
  17. mutationCache: this,
  18. logger: client.getLogger(),
  19. mutationId: ++this.mutationId,
  20. options: client.defaultMutationOptions(options),
  21. state,
  22. defaultOptions: options.mutationKey ? client.getMutationDefaults(options.mutationKey) : undefined
  23. });
  24. this.add(mutation$1);
  25. return mutation$1;
  26. }
  27. add(mutation) {
  28. this.mutations.push(mutation);
  29. this.notify({
  30. type: 'added',
  31. mutation
  32. });
  33. }
  34. remove(mutation) {
  35. this.mutations = this.mutations.filter(x => x !== mutation);
  36. this.notify({
  37. type: 'removed',
  38. mutation
  39. });
  40. }
  41. clear() {
  42. notifyManager.notifyManager.batch(() => {
  43. this.mutations.forEach(mutation => {
  44. this.remove(mutation);
  45. });
  46. });
  47. }
  48. getAll() {
  49. return this.mutations;
  50. }
  51. find(filters) {
  52. if (typeof filters.exact === 'undefined') {
  53. filters.exact = true;
  54. }
  55. return this.mutations.find(mutation => utils.matchMutation(filters, mutation));
  56. }
  57. findAll(filters) {
  58. return this.mutations.filter(mutation => utils.matchMutation(filters, mutation));
  59. }
  60. notify(event) {
  61. notifyManager.notifyManager.batch(() => {
  62. this.listeners.forEach(({
  63. listener
  64. }) => {
  65. listener(event);
  66. });
  67. });
  68. }
  69. resumePausedMutations() {
  70. var _this$resuming;
  71. this.resuming = ((_this$resuming = this.resuming) != null ? _this$resuming : Promise.resolve()).then(() => {
  72. const pausedMutations = this.mutations.filter(x => x.state.isPaused);
  73. return notifyManager.notifyManager.batch(() => pausedMutations.reduce((promise, mutation) => promise.then(() => mutation.continue().catch(utils.noop)), Promise.resolve()));
  74. }).then(() => {
  75. this.resuming = undefined;
  76. });
  77. return this.resuming;
  78. }
  79. }
  80. exports.MutationCache = MutationCache;
  81. //# sourceMappingURL=mutationCache.js.map