@robinmalfait/event-source 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1650 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
-
7
- var crypto = require('crypto');
8
- var yamlify = _interopDefault(require('yamlify-object'));
9
-
10
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
11
- try {
12
- var info = gen[key](arg);
13
- var value = info.value;
14
- } catch (error) {
15
- reject(error);
16
- return;
17
- }
18
-
19
- if (info.done) {
20
- resolve(value);
21
- } else {
22
- Promise.resolve(value).then(_next, _throw);
23
- }
24
- }
25
-
26
- function _asyncToGenerator(fn) {
27
- return function () {
28
- var self = this,
29
- args = arguments;
30
- return new Promise(function (resolve, reject) {
31
- var gen = fn.apply(self, args);
32
-
33
- function _next(value) {
34
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
35
- }
36
-
37
- function _throw(err) {
38
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
39
- }
40
-
41
- _next(undefined);
42
- });
43
- };
44
- }
45
-
46
- function _defineProperties(target, props) {
47
- for (var i = 0; i < props.length; i++) {
48
- var descriptor = props[i];
49
- descriptor.enumerable = descriptor.enumerable || false;
50
- descriptor.configurable = true;
51
- if ("value" in descriptor) descriptor.writable = true;
52
- Object.defineProperty(target, descriptor.key, descriptor);
53
- }
54
- }
55
-
56
- function _createClass(Constructor, protoProps, staticProps) {
57
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
58
- if (staticProps) _defineProperties(Constructor, staticProps);
59
- return Constructor;
60
- }
61
-
62
- function _extends() {
63
- _extends = Object.assign || function (target) {
64
- for (var i = 1; i < arguments.length; i++) {
65
- var source = arguments[i];
66
-
67
- for (var key in source) {
68
- if (Object.prototype.hasOwnProperty.call(source, key)) {
69
- target[key] = source[key];
70
- }
71
- }
72
- }
73
-
74
- return target;
75
- };
76
-
77
- return _extends.apply(this, arguments);
78
- }
79
-
80
- function _unsupportedIterableToArray(o, minLen) {
81
- if (!o) return;
82
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
83
- var n = Object.prototype.toString.call(o).slice(8, -1);
84
- if (n === "Object" && o.constructor) n = o.constructor.name;
85
- if (n === "Map" || n === "Set") return Array.from(o);
86
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
87
- }
88
-
89
- function _arrayLikeToArray(arr, len) {
90
- if (len == null || len > arr.length) len = arr.length;
91
-
92
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
93
-
94
- return arr2;
95
- }
96
-
97
- function _createForOfIteratorHelperLoose(o, allowArrayLike) {
98
- var it;
99
-
100
- if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
101
- if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
102
- if (it) o = it;
103
- var i = 0;
104
- return function () {
105
- if (i >= o.length) return {
106
- done: true
107
- };
108
- return {
109
- done: false,
110
- value: o[i++]
111
- };
112
- };
113
- }
114
-
115
- throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
116
- }
117
-
118
- it = o[Symbol.iterator]();
119
- return it.next.bind(it);
120
- }
121
-
122
- function createError(message, attributes) {
123
- var error = Object.assign(new Error(message), attributes); // This allows us to remove this createError function from the stack trace.
124
- // This means that your stack trace will be useful and point to the place
125
- // where the error is possibly thrown and _not_ to this function!
126
-
127
- if (Error.captureStackTrace) Error.captureStackTrace(error, createError);
128
- return error;
129
- }
130
-
131
- function abort(message, attributes) {
132
- var error = createError(message, attributes); // This allows us to remove this abort function from the stack trace.
133
- // This means that your stack trace will be useful and point to the place
134
- // where the error is possibly thrown and _not_ to this function!
135
-
136
- if (Error.captureStackTrace) Error.captureStackTrace(error, abort);
137
- throw error;
138
- }
139
-
140
- var Aggregate = /*#__PURE__*/function () {
141
- function Aggregate() {
142
- this.version = 0;
143
- this.recordedEvents = [];
144
- }
145
-
146
- var _proto = Aggregate.prototype;
147
-
148
- _proto.replayEvents = function replayEvents(events) {
149
- if (events === void 0) {
150
- events = [];
151
- }
152
-
153
- for (var _iterator = _createForOfIteratorHelperLoose(events), _step; !(_step = _iterator()).done;) {
154
- var event = _step.value;
155
- this.applyAnEvent(event);
156
- }
157
-
158
- return this;
159
- };
160
-
161
- _proto.applyAnEvent = function applyAnEvent(event) {
162
-
163
- if (this[event.eventName] === undefined) {
164
- if (event.eventName.match(/^[$A-Z_][0-9A-Z_$]*$/i)) {
165
- abort("Aggregate \"" + this.constructor.name + "\" has no method:\n\n" + event.eventName + "(event) {\n\t// Code goes here...\n}");
166
- } else {
167
- abort("Aggregate \"" + this.constructor.name + "\" has no method:\n\n['" + event.eventName + "'](event) {\n\t// Code goes here...\n}");
168
- }
169
- }
170
-
171
- try {
172
- ;
173
- this[event.eventName](event);
174
- } catch (err) {
175
- console.error("An error occurred inside your \"%s\" function:\n", event.eventName, err.stack.split('\n').map(function (line) {
176
- return " " + line;
177
- }).join('\n'));
178
- } finally {
179
- this.version++;
180
- }
181
-
182
- return this;
183
- };
184
-
185
- _proto.recordThat = function recordThat(event) {
186
- var eventToStore = _extends({}, event, {
187
- version: this.version
188
- });
189
-
190
- this.applyAnEvent(eventToStore);
191
- this.recordedEvents.push(eventToStore);
192
- return this;
193
- };
194
-
195
- _proto.releaseEvents = function releaseEvents() {
196
- return this.recordedEvents.splice(0);
197
- };
198
-
199
- return Aggregate;
200
- }();
201
-
202
- function Command(type, payload) {
203
- return {
204
- type: type,
205
- payload: payload
206
- };
207
- }
208
-
209
- function Event(eventName, aggregateId, payload) {
210
- return {
211
- aggregateId: aggregateId,
212
- eventId: crypto.randomUUID(),
213
- eventName: eventName,
214
- payload: payload,
215
- recordedAt: new Date()
216
- };
217
- }
218
-
219
- function createCommonjsModule(fn, module) {
220
- return module = { exports: {} }, fn(module, module.exports), module.exports;
221
- }
222
-
223
- var runtime_1 = createCommonjsModule(function (module) {
224
- /**
225
- * Copyright (c) 2014-present, Facebook, Inc.
226
- *
227
- * This source code is licensed under the MIT license found in the
228
- * LICENSE file in the root directory of this source tree.
229
- */
230
-
231
- var runtime = (function (exports) {
232
-
233
- var Op = Object.prototype;
234
- var hasOwn = Op.hasOwnProperty;
235
- var undefined$1; // More compressible than void 0.
236
- var $Symbol = typeof Symbol === "function" ? Symbol : {};
237
- var iteratorSymbol = $Symbol.iterator || "@@iterator";
238
- var asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator";
239
- var toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag";
240
-
241
- function define(obj, key, value) {
242
- Object.defineProperty(obj, key, {
243
- value: value,
244
- enumerable: true,
245
- configurable: true,
246
- writable: true
247
- });
248
- return obj[key];
249
- }
250
- try {
251
- // IE 8 has a broken Object.defineProperty that only works on DOM objects.
252
- define({}, "");
253
- } catch (err) {
254
- define = function(obj, key, value) {
255
- return obj[key] = value;
256
- };
257
- }
258
-
259
- function wrap(innerFn, outerFn, self, tryLocsList) {
260
- // If outerFn provided and outerFn.prototype is a Generator, then outerFn.prototype instanceof Generator.
261
- var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator;
262
- var generator = Object.create(protoGenerator.prototype);
263
- var context = new Context(tryLocsList || []);
264
-
265
- // The ._invoke method unifies the implementations of the .next,
266
- // .throw, and .return methods.
267
- generator._invoke = makeInvokeMethod(innerFn, self, context);
268
-
269
- return generator;
270
- }
271
- exports.wrap = wrap;
272
-
273
- // Try/catch helper to minimize deoptimizations. Returns a completion
274
- // record like context.tryEntries[i].completion. This interface could
275
- // have been (and was previously) designed to take a closure to be
276
- // invoked without arguments, but in all the cases we care about we
277
- // already have an existing method we want to call, so there's no need
278
- // to create a new function object. We can even get away with assuming
279
- // the method takes exactly one argument, since that happens to be true
280
- // in every case, so we don't have to touch the arguments object. The
281
- // only additional allocation required is the completion record, which
282
- // has a stable shape and so hopefully should be cheap to allocate.
283
- function tryCatch(fn, obj, arg) {
284
- try {
285
- return { type: "normal", arg: fn.call(obj, arg) };
286
- } catch (err) {
287
- return { type: "throw", arg: err };
288
- }
289
- }
290
-
291
- var GenStateSuspendedStart = "suspendedStart";
292
- var GenStateSuspendedYield = "suspendedYield";
293
- var GenStateExecuting = "executing";
294
- var GenStateCompleted = "completed";
295
-
296
- // Returning this object from the innerFn has the same effect as
297
- // breaking out of the dispatch switch statement.
298
- var ContinueSentinel = {};
299
-
300
- // Dummy constructor functions that we use as the .constructor and
301
- // .constructor.prototype properties for functions that return Generator
302
- // objects. For full spec compliance, you may wish to configure your
303
- // minifier not to mangle the names of these two functions.
304
- function Generator() {}
305
- function GeneratorFunction() {}
306
- function GeneratorFunctionPrototype() {}
307
-
308
- // This is a polyfill for %IteratorPrototype% for environments that
309
- // don't natively support it.
310
- var IteratorPrototype = {};
311
- IteratorPrototype[iteratorSymbol] = function () {
312
- return this;
313
- };
314
-
315
- var getProto = Object.getPrototypeOf;
316
- var NativeIteratorPrototype = getProto && getProto(getProto(values([])));
317
- if (NativeIteratorPrototype &&
318
- NativeIteratorPrototype !== Op &&
319
- hasOwn.call(NativeIteratorPrototype, iteratorSymbol)) {
320
- // This environment has a native %IteratorPrototype%; use it instead
321
- // of the polyfill.
322
- IteratorPrototype = NativeIteratorPrototype;
323
- }
324
-
325
- var Gp = GeneratorFunctionPrototype.prototype =
326
- Generator.prototype = Object.create(IteratorPrototype);
327
- GeneratorFunction.prototype = Gp.constructor = GeneratorFunctionPrototype;
328
- GeneratorFunctionPrototype.constructor = GeneratorFunction;
329
- GeneratorFunction.displayName = define(
330
- GeneratorFunctionPrototype,
331
- toStringTagSymbol,
332
- "GeneratorFunction"
333
- );
334
-
335
- // Helper for defining the .next, .throw, and .return methods of the
336
- // Iterator interface in terms of a single ._invoke method.
337
- function defineIteratorMethods(prototype) {
338
- ["next", "throw", "return"].forEach(function(method) {
339
- define(prototype, method, function(arg) {
340
- return this._invoke(method, arg);
341
- });
342
- });
343
- }
344
-
345
- exports.isGeneratorFunction = function(genFun) {
346
- var ctor = typeof genFun === "function" && genFun.constructor;
347
- return ctor
348
- ? ctor === GeneratorFunction ||
349
- // For the native GeneratorFunction constructor, the best we can
350
- // do is to check its .name property.
351
- (ctor.displayName || ctor.name) === "GeneratorFunction"
352
- : false;
353
- };
354
-
355
- exports.mark = function(genFun) {
356
- if (Object.setPrototypeOf) {
357
- Object.setPrototypeOf(genFun, GeneratorFunctionPrototype);
358
- } else {
359
- genFun.__proto__ = GeneratorFunctionPrototype;
360
- define(genFun, toStringTagSymbol, "GeneratorFunction");
361
- }
362
- genFun.prototype = Object.create(Gp);
363
- return genFun;
364
- };
365
-
366
- // Within the body of any async function, `await x` is transformed to
367
- // `yield regeneratorRuntime.awrap(x)`, so that the runtime can test
368
- // `hasOwn.call(value, "__await")` to determine if the yielded value is
369
- // meant to be awaited.
370
- exports.awrap = function(arg) {
371
- return { __await: arg };
372
- };
373
-
374
- function AsyncIterator(generator, PromiseImpl) {
375
- function invoke(method, arg, resolve, reject) {
376
- var record = tryCatch(generator[method], generator, arg);
377
- if (record.type === "throw") {
378
- reject(record.arg);
379
- } else {
380
- var result = record.arg;
381
- var value = result.value;
382
- if (value &&
383
- typeof value === "object" &&
384
- hasOwn.call(value, "__await")) {
385
- return PromiseImpl.resolve(value.__await).then(function(value) {
386
- invoke("next", value, resolve, reject);
387
- }, function(err) {
388
- invoke("throw", err, resolve, reject);
389
- });
390
- }
391
-
392
- return PromiseImpl.resolve(value).then(function(unwrapped) {
393
- // When a yielded Promise is resolved, its final value becomes
394
- // the .value of the Promise<{value,done}> result for the
395
- // current iteration.
396
- result.value = unwrapped;
397
- resolve(result);
398
- }, function(error) {
399
- // If a rejected Promise was yielded, throw the rejection back
400
- // into the async generator function so it can be handled there.
401
- return invoke("throw", error, resolve, reject);
402
- });
403
- }
404
- }
405
-
406
- var previousPromise;
407
-
408
- function enqueue(method, arg) {
409
- function callInvokeWithMethodAndArg() {
410
- return new PromiseImpl(function(resolve, reject) {
411
- invoke(method, arg, resolve, reject);
412
- });
413
- }
414
-
415
- return previousPromise =
416
- // If enqueue has been called before, then we want to wait until
417
- // all previous Promises have been resolved before calling invoke,
418
- // so that results are always delivered in the correct order. If
419
- // enqueue has not been called before, then it is important to
420
- // call invoke immediately, without waiting on a callback to fire,
421
- // so that the async generator function has the opportunity to do
422
- // any necessary setup in a predictable way. This predictability
423
- // is why the Promise constructor synchronously invokes its
424
- // executor callback, and why async functions synchronously
425
- // execute code before the first await. Since we implement simple
426
- // async functions in terms of async generators, it is especially
427
- // important to get this right, even though it requires care.
428
- previousPromise ? previousPromise.then(
429
- callInvokeWithMethodAndArg,
430
- // Avoid propagating failures to Promises returned by later
431
- // invocations of the iterator.
432
- callInvokeWithMethodAndArg
433
- ) : callInvokeWithMethodAndArg();
434
- }
435
-
436
- // Define the unified helper method that is used to implement .next,
437
- // .throw, and .return (see defineIteratorMethods).
438
- this._invoke = enqueue;
439
- }
440
-
441
- defineIteratorMethods(AsyncIterator.prototype);
442
- AsyncIterator.prototype[asyncIteratorSymbol] = function () {
443
- return this;
444
- };
445
- exports.AsyncIterator = AsyncIterator;
446
-
447
- // Note that simple async functions are implemented on top of
448
- // AsyncIterator objects; they just return a Promise for the value of
449
- // the final result produced by the iterator.
450
- exports.async = function(innerFn, outerFn, self, tryLocsList, PromiseImpl) {
451
- if (PromiseImpl === void 0) PromiseImpl = Promise;
452
-
453
- var iter = new AsyncIterator(
454
- wrap(innerFn, outerFn, self, tryLocsList),
455
- PromiseImpl
456
- );
457
-
458
- return exports.isGeneratorFunction(outerFn)
459
- ? iter // If outerFn is a generator, return the full iterator.
460
- : iter.next().then(function(result) {
461
- return result.done ? result.value : iter.next();
462
- });
463
- };
464
-
465
- function makeInvokeMethod(innerFn, self, context) {
466
- var state = GenStateSuspendedStart;
467
-
468
- return function invoke(method, arg) {
469
- if (state === GenStateExecuting) {
470
- throw new Error("Generator is already running");
471
- }
472
-
473
- if (state === GenStateCompleted) {
474
- if (method === "throw") {
475
- throw arg;
476
- }
477
-
478
- // Be forgiving, per 25.3.3.3.3 of the spec:
479
- // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-generatorresume
480
- return doneResult();
481
- }
482
-
483
- context.method = method;
484
- context.arg = arg;
485
-
486
- while (true) {
487
- var delegate = context.delegate;
488
- if (delegate) {
489
- var delegateResult = maybeInvokeDelegate(delegate, context);
490
- if (delegateResult) {
491
- if (delegateResult === ContinueSentinel) continue;
492
- return delegateResult;
493
- }
494
- }
495
-
496
- if (context.method === "next") {
497
- // Setting context._sent for legacy support of Babel's
498
- // function.sent implementation.
499
- context.sent = context._sent = context.arg;
500
-
501
- } else if (context.method === "throw") {
502
- if (state === GenStateSuspendedStart) {
503
- state = GenStateCompleted;
504
- throw context.arg;
505
- }
506
-
507
- context.dispatchException(context.arg);
508
-
509
- } else if (context.method === "return") {
510
- context.abrupt("return", context.arg);
511
- }
512
-
513
- state = GenStateExecuting;
514
-
515
- var record = tryCatch(innerFn, self, context);
516
- if (record.type === "normal") {
517
- // If an exception is thrown from innerFn, we leave state ===
518
- // GenStateExecuting and loop back for another invocation.
519
- state = context.done
520
- ? GenStateCompleted
521
- : GenStateSuspendedYield;
522
-
523
- if (record.arg === ContinueSentinel) {
524
- continue;
525
- }
526
-
527
- return {
528
- value: record.arg,
529
- done: context.done
530
- };
531
-
532
- } else if (record.type === "throw") {
533
- state = GenStateCompleted;
534
- // Dispatch the exception by looping back around to the
535
- // context.dispatchException(context.arg) call above.
536
- context.method = "throw";
537
- context.arg = record.arg;
538
- }
539
- }
540
- };
541
- }
542
-
543
- // Call delegate.iterator[context.method](context.arg) and handle the
544
- // result, either by returning a { value, done } result from the
545
- // delegate iterator, or by modifying context.method and context.arg,
546
- // setting context.delegate to null, and returning the ContinueSentinel.
547
- function maybeInvokeDelegate(delegate, context) {
548
- var method = delegate.iterator[context.method];
549
- if (method === undefined$1) {
550
- // A .throw or .return when the delegate iterator has no .throw
551
- // method always terminates the yield* loop.
552
- context.delegate = null;
553
-
554
- if (context.method === "throw") {
555
- // Note: ["return"] must be used for ES3 parsing compatibility.
556
- if (delegate.iterator["return"]) {
557
- // If the delegate iterator has a return method, give it a
558
- // chance to clean up.
559
- context.method = "return";
560
- context.arg = undefined$1;
561
- maybeInvokeDelegate(delegate, context);
562
-
563
- if (context.method === "throw") {
564
- // If maybeInvokeDelegate(context) changed context.method from
565
- // "return" to "throw", let that override the TypeError below.
566
- return ContinueSentinel;
567
- }
568
- }
569
-
570
- context.method = "throw";
571
- context.arg = new TypeError(
572
- "The iterator does not provide a 'throw' method");
573
- }
574
-
575
- return ContinueSentinel;
576
- }
577
-
578
- var record = tryCatch(method, delegate.iterator, context.arg);
579
-
580
- if (record.type === "throw") {
581
- context.method = "throw";
582
- context.arg = record.arg;
583
- context.delegate = null;
584
- return ContinueSentinel;
585
- }
586
-
587
- var info = record.arg;
588
-
589
- if (! info) {
590
- context.method = "throw";
591
- context.arg = new TypeError("iterator result is not an object");
592
- context.delegate = null;
593
- return ContinueSentinel;
594
- }
595
-
596
- if (info.done) {
597
- // Assign the result of the finished delegate to the temporary
598
- // variable specified by delegate.resultName (see delegateYield).
599
- context[delegate.resultName] = info.value;
600
-
601
- // Resume execution at the desired location (see delegateYield).
602
- context.next = delegate.nextLoc;
603
-
604
- // If context.method was "throw" but the delegate handled the
605
- // exception, let the outer generator proceed normally. If
606
- // context.method was "next", forget context.arg since it has been
607
- // "consumed" by the delegate iterator. If context.method was
608
- // "return", allow the original .return call to continue in the
609
- // outer generator.
610
- if (context.method !== "return") {
611
- context.method = "next";
612
- context.arg = undefined$1;
613
- }
614
-
615
- } else {
616
- // Re-yield the result returned by the delegate method.
617
- return info;
618
- }
619
-
620
- // The delegate iterator is finished, so forget it and continue with
621
- // the outer generator.
622
- context.delegate = null;
623
- return ContinueSentinel;
624
- }
625
-
626
- // Define Generator.prototype.{next,throw,return} in terms of the
627
- // unified ._invoke helper method.
628
- defineIteratorMethods(Gp);
629
-
630
- define(Gp, toStringTagSymbol, "Generator");
631
-
632
- // A Generator should always return itself as the iterator object when the
633
- // @@iterator function is called on it. Some browsers' implementations of the
634
- // iterator prototype chain incorrectly implement this, causing the Generator
635
- // object to not be returned from this call. This ensures that doesn't happen.
636
- // See https://github.com/facebook/regenerator/issues/274 for more details.
637
- Gp[iteratorSymbol] = function() {
638
- return this;
639
- };
640
-
641
- Gp.toString = function() {
642
- return "[object Generator]";
643
- };
644
-
645
- function pushTryEntry(locs) {
646
- var entry = { tryLoc: locs[0] };
647
-
648
- if (1 in locs) {
649
- entry.catchLoc = locs[1];
650
- }
651
-
652
- if (2 in locs) {
653
- entry.finallyLoc = locs[2];
654
- entry.afterLoc = locs[3];
655
- }
656
-
657
- this.tryEntries.push(entry);
658
- }
659
-
660
- function resetTryEntry(entry) {
661
- var record = entry.completion || {};
662
- record.type = "normal";
663
- delete record.arg;
664
- entry.completion = record;
665
- }
666
-
667
- function Context(tryLocsList) {
668
- // The root entry object (effectively a try statement without a catch
669
- // or a finally block) gives us a place to store values thrown from
670
- // locations where there is no enclosing try statement.
671
- this.tryEntries = [{ tryLoc: "root" }];
672
- tryLocsList.forEach(pushTryEntry, this);
673
- this.reset(true);
674
- }
675
-
676
- exports.keys = function(object) {
677
- var keys = [];
678
- for (var key in object) {
679
- keys.push(key);
680
- }
681
- keys.reverse();
682
-
683
- // Rather than returning an object with a next method, we keep
684
- // things simple and return the next function itself.
685
- return function next() {
686
- while (keys.length) {
687
- var key = keys.pop();
688
- if (key in object) {
689
- next.value = key;
690
- next.done = false;
691
- return next;
692
- }
693
- }
694
-
695
- // To avoid creating an additional object, we just hang the .value
696
- // and .done properties off the next function object itself. This
697
- // also ensures that the minifier will not anonymize the function.
698
- next.done = true;
699
- return next;
700
- };
701
- };
702
-
703
- function values(iterable) {
704
- if (iterable) {
705
- var iteratorMethod = iterable[iteratorSymbol];
706
- if (iteratorMethod) {
707
- return iteratorMethod.call(iterable);
708
- }
709
-
710
- if (typeof iterable.next === "function") {
711
- return iterable;
712
- }
713
-
714
- if (!isNaN(iterable.length)) {
715
- var i = -1, next = function next() {
716
- while (++i < iterable.length) {
717
- if (hasOwn.call(iterable, i)) {
718
- next.value = iterable[i];
719
- next.done = false;
720
- return next;
721
- }
722
- }
723
-
724
- next.value = undefined$1;
725
- next.done = true;
726
-
727
- return next;
728
- };
729
-
730
- return next.next = next;
731
- }
732
- }
733
-
734
- // Return an iterator with no values.
735
- return { next: doneResult };
736
- }
737
- exports.values = values;
738
-
739
- function doneResult() {
740
- return { value: undefined$1, done: true };
741
- }
742
-
743
- Context.prototype = {
744
- constructor: Context,
745
-
746
- reset: function(skipTempReset) {
747
- this.prev = 0;
748
- this.next = 0;
749
- // Resetting context._sent for legacy support of Babel's
750
- // function.sent implementation.
751
- this.sent = this._sent = undefined$1;
752
- this.done = false;
753
- this.delegate = null;
754
-
755
- this.method = "next";
756
- this.arg = undefined$1;
757
-
758
- this.tryEntries.forEach(resetTryEntry);
759
-
760
- if (!skipTempReset) {
761
- for (var name in this) {
762
- // Not sure about the optimal order of these conditions:
763
- if (name.charAt(0) === "t" &&
764
- hasOwn.call(this, name) &&
765
- !isNaN(+name.slice(1))) {
766
- this[name] = undefined$1;
767
- }
768
- }
769
- }
770
- },
771
-
772
- stop: function() {
773
- this.done = true;
774
-
775
- var rootEntry = this.tryEntries[0];
776
- var rootRecord = rootEntry.completion;
777
- if (rootRecord.type === "throw") {
778
- throw rootRecord.arg;
779
- }
780
-
781
- return this.rval;
782
- },
783
-
784
- dispatchException: function(exception) {
785
- if (this.done) {
786
- throw exception;
787
- }
788
-
789
- var context = this;
790
- function handle(loc, caught) {
791
- record.type = "throw";
792
- record.arg = exception;
793
- context.next = loc;
794
-
795
- if (caught) {
796
- // If the dispatched exception was caught by a catch block,
797
- // then let that catch block handle the exception normally.
798
- context.method = "next";
799
- context.arg = undefined$1;
800
- }
801
-
802
- return !! caught;
803
- }
804
-
805
- for (var i = this.tryEntries.length - 1; i >= 0; --i) {
806
- var entry = this.tryEntries[i];
807
- var record = entry.completion;
808
-
809
- if (entry.tryLoc === "root") {
810
- // Exception thrown outside of any try block that could handle
811
- // it, so set the completion value of the entire function to
812
- // throw the exception.
813
- return handle("end");
814
- }
815
-
816
- if (entry.tryLoc <= this.prev) {
817
- var hasCatch = hasOwn.call(entry, "catchLoc");
818
- var hasFinally = hasOwn.call(entry, "finallyLoc");
819
-
820
- if (hasCatch && hasFinally) {
821
- if (this.prev < entry.catchLoc) {
822
- return handle(entry.catchLoc, true);
823
- } else if (this.prev < entry.finallyLoc) {
824
- return handle(entry.finallyLoc);
825
- }
826
-
827
- } else if (hasCatch) {
828
- if (this.prev < entry.catchLoc) {
829
- return handle(entry.catchLoc, true);
830
- }
831
-
832
- } else if (hasFinally) {
833
- if (this.prev < entry.finallyLoc) {
834
- return handle(entry.finallyLoc);
835
- }
836
-
837
- } else {
838
- throw new Error("try statement without catch or finally");
839
- }
840
- }
841
- }
842
- },
843
-
844
- abrupt: function(type, arg) {
845
- for (var i = this.tryEntries.length - 1; i >= 0; --i) {
846
- var entry = this.tryEntries[i];
847
- if (entry.tryLoc <= this.prev &&
848
- hasOwn.call(entry, "finallyLoc") &&
849
- this.prev < entry.finallyLoc) {
850
- var finallyEntry = entry;
851
- break;
852
- }
853
- }
854
-
855
- if (finallyEntry &&
856
- (type === "break" ||
857
- type === "continue") &&
858
- finallyEntry.tryLoc <= arg &&
859
- arg <= finallyEntry.finallyLoc) {
860
- // Ignore the finally entry if control is not jumping to a
861
- // location outside the try/catch block.
862
- finallyEntry = null;
863
- }
864
-
865
- var record = finallyEntry ? finallyEntry.completion : {};
866
- record.type = type;
867
- record.arg = arg;
868
-
869
- if (finallyEntry) {
870
- this.method = "next";
871
- this.next = finallyEntry.finallyLoc;
872
- return ContinueSentinel;
873
- }
874
-
875
- return this.complete(record);
876
- },
877
-
878
- complete: function(record, afterLoc) {
879
- if (record.type === "throw") {
880
- throw record.arg;
881
- }
882
-
883
- if (record.type === "break" ||
884
- record.type === "continue") {
885
- this.next = record.arg;
886
- } else if (record.type === "return") {
887
- this.rval = this.arg = record.arg;
888
- this.method = "return";
889
- this.next = "end";
890
- } else if (record.type === "normal" && afterLoc) {
891
- this.next = afterLoc;
892
- }
893
-
894
- return ContinueSentinel;
895
- },
896
-
897
- finish: function(finallyLoc) {
898
- for (var i = this.tryEntries.length - 1; i >= 0; --i) {
899
- var entry = this.tryEntries[i];
900
- if (entry.finallyLoc === finallyLoc) {
901
- this.complete(entry.completion, entry.afterLoc);
902
- resetTryEntry(entry);
903
- return ContinueSentinel;
904
- }
905
- }
906
- },
907
-
908
- "catch": function(tryLoc) {
909
- for (var i = this.tryEntries.length - 1; i >= 0; --i) {
910
- var entry = this.tryEntries[i];
911
- if (entry.tryLoc === tryLoc) {
912
- var record = entry.completion;
913
- if (record.type === "throw") {
914
- var thrown = record.arg;
915
- resetTryEntry(entry);
916
- }
917
- return thrown;
918
- }
919
- }
920
-
921
- // The context.catch method must only be called with a location
922
- // argument that corresponds to a known catch block.
923
- throw new Error("illegal catch attempt");
924
- },
925
-
926
- delegateYield: function(iterable, resultName, nextLoc) {
927
- this.delegate = {
928
- iterator: values(iterable),
929
- resultName: resultName,
930
- nextLoc: nextLoc
931
- };
932
-
933
- if (this.method === "next") {
934
- // Deliberately forget the last sent value so that we don't
935
- // accidentally pass it on to the delegate.
936
- this.arg = undefined$1;
937
- }
938
-
939
- return ContinueSentinel;
940
- }
941
- };
942
-
943
- // Regardless of whether this script is executing as a CommonJS module
944
- // or not, return the runtime object so that we can declare the variable
945
- // regeneratorRuntime in the outer scope, which allows this module to be
946
- // injected easily by `bin/regenerator --include-runtime script.js`.
947
- return exports;
948
-
949
- }(
950
- // If this script is executing as a CommonJS module, use module.exports
951
- // as the regeneratorRuntime namespace. Otherwise create a new empty
952
- // object. Either way, the resulting object will be used to initialize
953
- // the regeneratorRuntime variable at the top of this file.
954
- module.exports
955
- ));
956
-
957
- try {
958
- regeneratorRuntime = runtime;
959
- } catch (accidentalStrictMode) {
960
- // This module should not be running in strict mode, so the above
961
- // assignment should always work unless something is misconfigured. Just
962
- // in case runtime.js accidentally runs in strict mode, we can escape
963
- // strict mode using a global Function call. This could conceivably fail
964
- // if a Content Security Policy forbids using Function, but in that case
965
- // the proper solution is to fix the accidental strict mode problem. If
966
- // you've misconfigured your bundler to force strict mode and applied a
967
- // CSP to forbid Function, and you're not willing to fix either of those
968
- // problems, please detail your unique predicament in a GitHub issue.
969
- Function("r", "regeneratorRuntime = r")(runtime);
970
- }
971
- });
972
-
973
- function createEventSource(config) {
974
- var commandHandlers = config.commandHandlers,
975
- _config$eventHandlers = config.eventHandlers,
976
- eventHandlers = _config$eventHandlers === void 0 ? [] : _config$eventHandlers,
977
- _config$projectors = config.projectors,
978
- projectors = _config$projectors === void 0 ? [] : _config$projectors,
979
- storePromise = config.store;
980
- var startupPromises = [];
981
- var api = {
982
- dispatch: function dispatch(command) {
983
- return _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee() {
984
- var handle;
985
- return runtime_1.wrap(function _callee$(_context) {
986
- while (1) {
987
- switch (_context.prev = _context.next) {
988
- case 0:
989
- if (commandHandlers[command.type] === undefined) {
990
- abort("There is no command handler for the \"" + command.type + "\" command");
991
- }
992
-
993
- handle = commandHandlers[command.type];
994
- _context.next = 4;
995
- return handle(command, api);
996
-
997
- case 4:
998
- return _context.abrupt("return", command);
999
-
1000
- case 5:
1001
- case "end":
1002
- return _context.stop();
1003
- }
1004
- }
1005
- }, _callee);
1006
- }))();
1007
- },
1008
- loadEvents: function loadEvents() {
1009
- return _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2() {
1010
- var store;
1011
- return runtime_1.wrap(function _callee2$(_context2) {
1012
- while (1) {
1013
- switch (_context2.prev = _context2.next) {
1014
- case 0:
1015
- _context2.next = 2;
1016
- return storePromise;
1017
-
1018
- case 2:
1019
- store = _context2.sent;
1020
- _context2.next = 5;
1021
- return store.loadEvents();
1022
-
1023
- case 5:
1024
- return _context2.abrupt("return", _context2.sent);
1025
-
1026
- case 6:
1027
- case "end":
1028
- return _context2.stop();
1029
- }
1030
- }
1031
- }, _callee2);
1032
- }))();
1033
- },
1034
- load: function load(aggregate, aggregateId) {
1035
- return _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3() {
1036
- var store, events;
1037
- return runtime_1.wrap(function _callee3$(_context3) {
1038
- while (1) {
1039
- switch (_context3.prev = _context3.next) {
1040
- case 0:
1041
- _context3.next = 2;
1042
- return storePromise;
1043
-
1044
- case 2:
1045
- store = _context3.sent;
1046
- _context3.next = 5;
1047
- return store.load(aggregateId);
1048
-
1049
- case 5:
1050
- events = _context3.sent;
1051
-
1052
- if (events.length <= 0) {
1053
- abort("Aggregate(" + aggregate.constructor.name + ") with ID(" + aggregateId + ") does not exist.", {
1054
- aggregate: aggregate.constructor.name,
1055
- aggregateId: aggregateId
1056
- });
1057
- }
1058
-
1059
- return _context3.abrupt("return", aggregate.replayEvents(events));
1060
-
1061
- case 8:
1062
- case "end":
1063
- return _context3.stop();
1064
- }
1065
- }
1066
- }, _callee3);
1067
- }))();
1068
- },
1069
- persist: function persist(aggregate) {
1070
- return _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee6() {
1071
- var store, events, _loop, _iterator, _step;
1072
-
1073
- return runtime_1.wrap(function _callee6$(_context7) {
1074
- while (1) {
1075
- switch (_context7.prev = _context7.next) {
1076
- case 0:
1077
- _context7.next = 2;
1078
- return Promise.all(startupPromises);
1079
-
1080
- case 2:
1081
- _context7.next = 4;
1082
- return storePromise;
1083
-
1084
- case 4:
1085
- store = _context7.sent;
1086
- // Get all the events that have been produced by the aggregate
1087
- events = aggregate.releaseEvents(); // Let's persist all the events
1088
-
1089
- _context7.next = 8;
1090
- return store.persist(events);
1091
-
1092
- case 8:
1093
- _loop = /*#__PURE__*/runtime_1.mark(function _loop() {
1094
- var event;
1095
- return runtime_1.wrap(function _loop$(_context6) {
1096
- while (1) {
1097
- switch (_context6.prev = _context6.next) {
1098
- case 0:
1099
- event = _step.value;
1100
- _context6.next = 3;
1101
- return Promise.all(projectors.map( /*#__PURE__*/function () {
1102
- var _ref = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee4(projector) {
1103
- return runtime_1.wrap(function _callee4$(_context4) {
1104
- while (1) {
1105
- switch (_context4.prev = _context4.next) {
1106
- case 0:
1107
- _context4.prev = 0;
1108
- _context4.next = 3;
1109
- return projector.update(event);
1110
-
1111
- case 3:
1112
- _context4.next = 9;
1113
- break;
1114
-
1115
- case 5:
1116
- _context4.prev = 5;
1117
- _context4.t0 = _context4["catch"](0);
1118
- console.error("An error occurred in one of your projections: " + projector.name + ", given an event", _context4.t0.stack.split('\n').map(function (line) {
1119
- return " " + line;
1120
- }).join('\n'));
1121
- throw _context4.t0;
1122
-
1123
- case 9:
1124
- case "end":
1125
- return _context4.stop();
1126
- }
1127
- }
1128
- }, _callee4, null, [[0, 5]]);
1129
- }));
1130
-
1131
- return function (_x) {
1132
- return _ref.apply(this, arguments);
1133
- };
1134
- }()));
1135
-
1136
- case 3:
1137
- _context6.next = 5;
1138
- return Promise.all(eventHandlers.map( /*#__PURE__*/function () {
1139
- var _ref2 = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee5(eventHandler) {
1140
- return runtime_1.wrap(function _callee5$(_context5) {
1141
- while (1) {
1142
- switch (_context5.prev = _context5.next) {
1143
- case 0:
1144
- _context5.prev = 0;
1145
- _context5.next = 3;
1146
- return eventHandler(event, api);
1147
-
1148
- case 3:
1149
- _context5.next = 9;
1150
- break;
1151
-
1152
- case 5:
1153
- _context5.prev = 5;
1154
- _context5.t0 = _context5["catch"](0);
1155
- console.error("An error occurred in one of your event handlers: " + eventHandler.name + ", given an event", _context5.t0.stack.split('\n').map(function (line) {
1156
- return " " + line;
1157
- }).join('\n'));
1158
- throw _context5.t0;
1159
-
1160
- case 9:
1161
- case "end":
1162
- return _context5.stop();
1163
- }
1164
- }
1165
- }, _callee5, null, [[0, 5]]);
1166
- }));
1167
-
1168
- return function (_x2) {
1169
- return _ref2.apply(this, arguments);
1170
- };
1171
- }()));
1172
-
1173
- case 5:
1174
- case "end":
1175
- return _context6.stop();
1176
- }
1177
- }
1178
- }, _loop);
1179
- });
1180
- _iterator = _createForOfIteratorHelperLoose(events);
1181
-
1182
- case 10:
1183
- if ((_step = _iterator()).done) {
1184
- _context7.next = 14;
1185
- break;
1186
- }
1187
-
1188
- return _context7.delegateYield(_loop(), "t0", 12);
1189
-
1190
- case 12:
1191
- _context7.next = 10;
1192
- break;
1193
-
1194
- case 14:
1195
- case "end":
1196
- return _context7.stop();
1197
- }
1198
- }
1199
- }, _callee6);
1200
- }))();
1201
- },
1202
- loadPersist: function loadPersist(aggregate, aggregateId, handle) {
1203
- var _this = this;
1204
-
1205
- return _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee7() {
1206
- return runtime_1.wrap(function _callee7$(_context8) {
1207
- while (1) {
1208
- switch (_context8.prev = _context8.next) {
1209
- case 0:
1210
- _context8.next = 2;
1211
- return _this.load(aggregate, aggregateId);
1212
-
1213
- case 2:
1214
- _context8.next = 4;
1215
- return handle(aggregate);
1216
-
1217
- case 4:
1218
- return _context8.abrupt("return", _this.persist(aggregate));
1219
-
1220
- case 5:
1221
- case "end":
1222
- return _context8.stop();
1223
- }
1224
- }
1225
- }, _callee7);
1226
- }))();
1227
- }
1228
- }; // Run all projection initializers
1229
-
1230
- for (var _iterator2 = _createForOfIteratorHelperLoose(projectors), _step2; !(_step2 = _iterator2()).done;) {
1231
- var projector = _step2.value;
1232
- startupPromises.push(projector.init(api));
1233
- }
1234
-
1235
- return api;
1236
- }
1237
-
1238
- function createEventMapper(mapper) {
1239
- return function (event, es) {
1240
- var _mapper$event$eventNa;
1241
-
1242
- return (_mapper$event$eventNa = mapper[event.eventName]) == null ? void 0 : _mapper$event$eventNa.call(mapper, event, es);
1243
- };
1244
- }
1245
-
1246
- var instances = /*#__PURE__*/new WeakMap();
1247
- /**
1248
- * This code allows us to store secret/private values for objects. In JavaScript
1249
- * you can't really store secrets on an object. There is this convention with
1250
- * for naming a variable `this._somePrivateField` but that is still accessible.
1251
- *
1252
- * Another trick you can use is to use symbols, but if you get access to the
1253
- * symbol, you can still retrieve the data.
1254
- *
1255
- * This approach uses a WeakMap. The WeakMap has a key which should be an
1256
- * object, so that you can store avlues specifically for this reference.
1257
- */
1258
-
1259
- function set(key, data) {
1260
- if (instances.has(key)) {
1261
- var original = instances.get(key);
1262
-
1263
- for (var _key in data) {
1264
- original[_key] = data[_key];
1265
- }
1266
- } else {
1267
- instances.set(key, data);
1268
- }
1269
- }
1270
- function get(key) {
1271
- return instances.get(key);
1272
- }
1273
-
1274
- var State;
1275
-
1276
- (function (State) {
1277
- State[State["IDLE"] = 0] = "IDLE";
1278
- State[State["STARTED"] = 1] = "STARTED";
1279
- })(State || (State = {}));
1280
-
1281
- var Queue = /*#__PURE__*/function () {
1282
- function Queue() {
1283
- set(this, {
1284
- jobs: [],
1285
- state: State.IDLE
1286
- });
1287
- }
1288
-
1289
- var _proto = Queue.prototype;
1290
-
1291
- _proto.start = /*#__PURE__*/function () {
1292
- var _start = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee() {
1293
- var _get, state, jobs, job, settled;
1294
-
1295
- return runtime_1.wrap(function _callee$(_context) {
1296
- while (1) {
1297
- switch (_context.prev = _context.next) {
1298
- case 0:
1299
- _get = get(this), state = _get.state, jobs = _get.jobs;
1300
-
1301
- if (!(state === State.STARTED || jobs.length <= 0)) {
1302
- _context.next = 3;
1303
- break;
1304
- }
1305
-
1306
- return _context.abrupt("return");
1307
-
1308
- case 3:
1309
- set(this, {
1310
- state: State.STARTED
1311
- });
1312
-
1313
- case 4:
1314
- if (!(jobs.length > 0)) {
1315
- _context.next = 11;
1316
- break;
1317
- }
1318
-
1319
- job = jobs.shift(); // Handle the job
1320
-
1321
- settled = Promise.resolve().then(job.handle); // Resolve / reject the job promise wrapper
1322
-
1323
- _context.next = 9;
1324
- return settled.then(job.resolve, job.reject);
1325
-
1326
- case 9:
1327
- _context.next = 4;
1328
- break;
1329
-
1330
- case 11:
1331
- set(this, {
1332
- state: State.IDLE
1333
- });
1334
-
1335
- case 12:
1336
- case "end":
1337
- return _context.stop();
1338
- }
1339
- }
1340
- }, _callee, this);
1341
- }));
1342
-
1343
- function start() {
1344
- return _start.apply(this, arguments);
1345
- }
1346
-
1347
- return start;
1348
- }();
1349
-
1350
- _proto.push = function push(handle) {
1351
- var _this = this;
1352
-
1353
- return new Promise(function (resolve, reject) {
1354
- var _get2 = get(_this),
1355
- jobs = _get2.jobs;
1356
-
1357
- jobs.push({
1358
- handle: handle,
1359
- resolve: resolve,
1360
- reject: reject
1361
- });
1362
- setImmediate(function () {
1363
- return _this.start();
1364
- });
1365
- });
1366
- };
1367
-
1368
- _createClass(Queue, [{
1369
- key: "length",
1370
- get: function get$1() {
1371
- return get(this).jobs.length;
1372
- }
1373
- }]);
1374
-
1375
- return Queue;
1376
- }();
1377
-
1378
- function noop() {}
1379
-
1380
- function createProjector(name, mapper, initializer) {
1381
- if (initializer === void 0) {
1382
- initializer = noop;
1383
- }
1384
-
1385
- var q = new Queue();
1386
- return {
1387
- name: name,
1388
- init: function init(es) {
1389
- return _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee() {
1390
- var initialized, events;
1391
- return runtime_1.wrap(function _callee$(_context) {
1392
- while (1) {
1393
- switch (_context.prev = _context.next) {
1394
- case 0:
1395
- // Let's run the initializer
1396
- initialized = q.push(initializer); // Re-build the projection from scratch
1397
-
1398
- _context.next = 3;
1399
- return es.loadEvents();
1400
-
1401
- case 3:
1402
- events = _context.sent;
1403
- _context.next = 6;
1404
- return Promise.all(events.map(function (event) {
1405
- return q.push(function () {
1406
- var _mapper$event$eventNa;
1407
-
1408
- return (_mapper$event$eventNa = mapper[event.eventName]) == null ? void 0 : _mapper$event$eventNa.call(mapper, event);
1409
- });
1410
- }));
1411
-
1412
- case 6:
1413
- _context.next = 8;
1414
- return initialized;
1415
-
1416
- case 8:
1417
- case "end":
1418
- return _context.stop();
1419
- }
1420
- }
1421
- }, _callee);
1422
- }))();
1423
- },
1424
- update: function update(event) {
1425
- return q.push(function () {
1426
- var _mapper$event$eventNa2;
1427
-
1428
- return (_mapper$event$eventNa2 = mapper[event.eventName]) == null ? void 0 : _mapper$event$eventNa2.call(mapper, event);
1429
- });
1430
- }
1431
- };
1432
- }
1433
-
1434
- var YAMLIFY_OPTIONS = {
1435
- indent: ' ',
1436
- colors: {
1437
- date: identity,
1438
- error: identity,
1439
- symbol: identity,
1440
- string: identity,
1441
- number: identity,
1442
- "boolean": identity,
1443
- "null": identity,
1444
- undefined: identity
1445
- }
1446
- };
1447
-
1448
- function identity(input) {
1449
- return input;
1450
- }
1451
-
1452
- function objectToYaml(object) {
1453
- if (object instanceof Error) {
1454
- return objectToYaml(_extends({}, object));
1455
- }
1456
-
1457
- return yamlify(object, YAMLIFY_OPTIONS).split('\n').slice(1).join('\n');
1458
- }
1459
-
1460
- var PLACEHOLDER = /*#__PURE__*/Symbol('__placeholder__');
1461
-
1462
- function cleanThrow(cb, fn) {
1463
- try {
1464
- return cb();
1465
- } catch (e) {
1466
- if (Error.captureStackTrace) Error.captureStackTrace(e, fn);
1467
- throw e;
1468
- }
1469
- }
1470
-
1471
- function createTestEventStore(commandHandlers, projectors) {
1472
- if (projectors === void 0) {
1473
- projectors = [];
1474
- }
1475
-
1476
- var db = [];
1477
- var producedEvents = [];
1478
-
1479
- function createTestRecordingProjector() {
1480
- return {
1481
- name: 'test-recording-projector',
1482
- init: function init() {
1483
- db.splice(0);
1484
- },
1485
- update: function update(event) {
1486
- producedEvents.push(event);
1487
- }
1488
- };
1489
- }
1490
-
1491
- var es = createEventSource({
1492
- store: {
1493
- load: function load(aggregateId) {
1494
- return _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee() {
1495
- return runtime_1.wrap(function _callee$(_context) {
1496
- while (1) {
1497
- switch (_context.prev = _context.next) {
1498
- case 0:
1499
- return _context.abrupt("return", db.filter(function (event) {
1500
- return event.aggregateId === aggregateId;
1501
- }));
1502
-
1503
- case 1:
1504
- case "end":
1505
- return _context.stop();
1506
- }
1507
- }
1508
- }, _callee);
1509
- }))();
1510
- },
1511
- loadEvents: function loadEvents() {
1512
- return db;
1513
- },
1514
- persist: function persist(events) {
1515
- db.push.apply(db, events);
1516
- }
1517
- },
1518
- commandHandlers: commandHandlers,
1519
- projectors: [].concat(projectors, [createTestRecordingProjector()])
1520
- });
1521
- var caughtError;
1522
- var returnValue = {
1523
- ___: PLACEHOLDER,
1524
- given: function given(events) {
1525
- return _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2() {
1526
- return runtime_1.wrap(function _callee2$(_context2) {
1527
- while (1) {
1528
- switch (_context2.prev = _context2.next) {
1529
- case 0:
1530
- if (events === void 0) {
1531
- events = [];
1532
- }
1533
-
1534
- db.push.apply(db, events);
1535
-
1536
- case 2:
1537
- case "end":
1538
- return _context2.stop();
1539
- }
1540
- }
1541
- }, _callee2);
1542
- }))();
1543
- },
1544
- when: function when(command) {
1545
- return _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3() {
1546
- return runtime_1.wrap(function _callee3$(_context3) {
1547
- while (1) {
1548
- switch (_context3.prev = _context3.next) {
1549
- case 0:
1550
- _context3.prev = 0;
1551
- _context3.next = 3;
1552
- return es.dispatch(command);
1553
-
1554
- case 3:
1555
- return _context3.abrupt("return", _context3.sent);
1556
-
1557
- case 6:
1558
- _context3.prev = 6;
1559
- _context3.t0 = _context3["catch"](0);
1560
- caughtError = _context3.t0;
1561
- return _context3.abrupt("return", command);
1562
-
1563
- case 10:
1564
- case "end":
1565
- return _context3.stop();
1566
- }
1567
- }
1568
- }, _callee3, null, [[0, 6]]);
1569
- }))();
1570
- },
1571
- then: function then(events) {
1572
- return _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee4() {
1573
- return runtime_1.wrap(function _callee4$(_context4) {
1574
- while (1) {
1575
- switch (_context4.prev = _context4.next) {
1576
- case 0:
1577
-
1578
- if (!(events instanceof Error)) {
1579
- _context4.next = 4;
1580
- break;
1581
- }
1582
-
1583
- cleanThrow(function () {
1584
- return expect(caughtError).toEqual(events);
1585
- }, returnValue.then);
1586
- return _context4.abrupt("return");
1587
-
1588
- case 4:
1589
- if (!caughtError) {
1590
- _context4.next = 7;
1591
- break;
1592
- }
1593
-
1594
- if (Object.keys(caughtError).length > 0) {
1595
- caughtError.message = ['With properties:', "\n" + objectToYaml(caughtError) + "\n\n---\n\n"].join('\n');
1596
- }
1597
-
1598
- throw caughtError;
1599
-
1600
- case 7:
1601
- cleanThrow(function () {
1602
- // Verify that the actual events and expected events have the same length
1603
- expect(events).toHaveLength(producedEvents.length); // Verify each individual event
1604
-
1605
- events.forEach(function (event, index) {
1606
- var _producedEvents$index = producedEvents[index],
1607
- aggregateId = _producedEvents$index.aggregateId,
1608
- eventName = _producedEvents$index.eventName,
1609
- payload = _producedEvents$index.payload;
1610
- expect(event.aggregateId).toEqual(aggregateId);
1611
- expect(event.eventName).toEqual(eventName);
1612
-
1613
- if (event.payload === null || event.payload === undefined) {
1614
- expect(event.payload).toEqual(payload);
1615
- }
1616
-
1617
- for (var key in event.payload) {
1618
- var value = event.payload[key];
1619
-
1620
- if (value === PLACEHOLDER) {
1621
- expect(payload).toHaveProperty(key);
1622
- expect(payload[key]).toBeDefined();
1623
- } else {
1624
- expect(payload[key]).toEqual(value);
1625
- }
1626
- }
1627
- });
1628
- }, returnValue.then);
1629
-
1630
- case 8:
1631
- case "end":
1632
- return _context4.stop();
1633
- }
1634
- }
1635
- }, _callee4);
1636
- }))();
1637
- }
1638
- };
1639
- return returnValue;
1640
- }
1641
-
1642
- exports.Aggregate = Aggregate;
1643
- exports.Command = Command;
1644
- exports.Event = Event;
1645
- exports.abort = abort;
1646
- exports.createEventMapper = createEventMapper;
1647
- exports.createEventSource = createEventSource;
1648
- exports.createProjector = createProjector;
1649
- exports.createTestEventStore = createTestEventStore;
1650
- //# sourceMappingURL=event-source.cjs.development.js.map