Skip to content

Commit

Permalink
Inventories work! close #106
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanja-4732 committed Sep 9, 2019
1 parent b5240a3 commit ff2b925
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class InventoriesComponent implements OnInit {
console.log("Loading...");

try {
await this.is.ready;
console.log(this.is.inventories);

this.inventories = Object.keys(this.is.inventories).map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class EventSourcingService {
* The event-logs (every inventory has its own)
*/
private static eventLogs: { [uuid: string]: Event[] };

/**
* Sneaky stuff
*
Expand Down
36 changes: 24 additions & 12 deletions frontend/src/app/services/inventory/inventory.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@ import { AuthService } from "../auth/auth.service";
})
export class InventoryService {
constructor(private ess: EventSourcingService, private as: AuthService) {
console.log("Constructing some InventoryService");

if (InventoryService.inventoriesProjection == null) {
console.log("Do fetchInventoryEvents");
this.fetchInventoryEvents();
console.log("Did fetchInventoryEvents");
} else {
console.log("Don't fetchInventoryEvents");
}
this.ready = new Promise((resolve, reject) => {
console.log("Constructing some InventoryService");

if (InventoryService.inventoriesProjection == null) {
console.log("Do fetchInventoryEvents");
this.fetchInventoryEvents().then(result => {
console.log("Did fetchInventoryEvents");
resolve(null);
});
} else {
console.log("Don't fetchInventoryEvents");
resolve(null);
}
});
}

/**
Expand All @@ -42,18 +47,25 @@ export class InventoryService {
*/
private static inventoriesProjection: { [uuid: string]: Inventory };

/**
* Sneaky stuff
*
* Used to get around the "no async constructors" limitation
*/
public ready: Promise<any>;

/**
* Obtains the inventories event log from the db and parses them
*/
private async fetchInventoryEvents() {
// Initialize the inventory projection dictionary
InventoryService.inventoriesProjection = {};

console.log("The events right here:");

await this.ess.ready;
await this.ess.ready;

console.log(JSON.stringify(this.ess.events));
console.log("The events right here:");
console.log(this.ess.events);

// Iterate over all event logs
for (const [uuid, eventLog] of Object.entries(this.ess.events)) {
Expand Down

0 comments on commit ff2b925

Please sign in to comment.