Skip to content

Commit

Permalink
feat(transaction): transaction now a an empty observable and it's pos…
Browse files Browse the repository at this point in the history
…sible to retrieve operations
  • Loading branch information
cbourget authored and mbarbeau committed Sep 25, 2019
1 parent 9527600 commit 1199b96
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions packages/common/src/lib/entity/shared/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Observable, of } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';

import { BehaviorSubject } from 'rxjs';

import {
EntityKey,
EntityTransactionOptions,
Expand Down Expand Up @@ -36,13 +38,18 @@ export class EntityTransaction {
/**
* Whether there are pending operations
*/
get empty(): boolean { return this.operations.entities$.value.length === 0; }
get empty$(): BehaviorSubject<boolean> { return this.operations.empty$; }

/**
* Whether there are pending operations
*/
get empty(): boolean { return this.empty$.value; }

/**
* Whether thise store is in commit phase
*/
get inCommitPhase(): boolean { return this._inCommitPhase; }
private _inCommitPhase = false;
get inCommitPhase(): boolean { return this.inCommitPhase$.value; }
readonly inCommitPhase$: BehaviorSubject<boolean> = new BehaviorSubject(false);

constructor(options: EntityTransactionOptions = {}) {
this.getKey = options.getKey ? options.getKey : getEntityId;
Expand Down Expand Up @@ -129,7 +136,7 @@ export class EntityTransaction {
* @returns The handler output (observable)
*/
commit(operations: EntityOperation[], handler: EntityTransactionCommitHandler): Observable<any> {
this._inCommitPhase = true;
this.inCommitPhase$.next(true);

return handler(this, operations)
.pipe(
Expand Down Expand Up @@ -203,7 +210,7 @@ export class EntityTransaction {
});

this.operations.deleteMany(operations);
this._inCommitPhase = false;
this.inCommitPhase$.next(false);
}

/**
Expand All @@ -212,7 +219,16 @@ export class EntityTransaction {
*/
clear() {
this.operations.clear();
this._inCommitPhase = false;
this.inCommitPhase$.next(false);
}

/**
* Get any existing operation on an entity
* @param entity Entity
* @returns Either an insert, update or delete operation
*/
getOperationByEntity(entity: object): EntityOperation {
return this.operations.get(this.getKey(entity));
}

/**
Expand Down Expand Up @@ -307,15 +323,15 @@ export class EntityTransaction {
*/
private onCommitSuccess(operations: EntityOperation[]) {
this.resolveOperations(operations);
this._inCommitPhase = false;
this.inCommitPhase$.next(false);
}

/**
* On commit error, abort transaction
* @param operations Commited operations
*/
private onCommitError(operations: EntityOperation[]) {
this._inCommitPhase = false;
this.inCommitPhase$.next(false);
}

/**
Expand All @@ -340,15 +356,6 @@ export class EntityTransaction {
this.operations.state.update(operation, {added: false});
}

/**
* Get the any existing operation an entity
* @param entity Entity
* @returns Either an insert, update or delete operation
*/
private getOperationByEntity(entity: object): EntityOperation {
return this.operations.get(this.getKey(entity));
}

/**
* Get all the operations to commit
* @returns Operations to commit
Expand Down

0 comments on commit 1199b96

Please sign in to comment.