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