Skip to content

Commit

Permalink
Removed cache from service. The repository factory caches them
Browse files Browse the repository at this point in the history
  • Loading branch information
fboucquez committed Oct 20, 2020
1 parent 4ac58b4 commit 479a83c
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 20 deletions.
110 changes: 110 additions & 0 deletions e2e/service/TransactionPaginationStreamer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright 2019 NEM
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { concatMap, map, mergeMap, toArray } from 'rxjs/operators';
import { TransactionPaginationStreamer } from '../../src/infrastructure/paginationStreamer/TransactionPaginationStreamer';
import { RepositoryFactory } from '../../src/infrastructure/RepositoryFactory';
import { TransactionSearchCriteria } from '../../src/infrastructure/searchCriteria/TransactionSearchCriteria';
import { TransactionGroup } from '../../src/infrastructure/TransactionGroup';
import { TransactionRepository } from '../../src/infrastructure/TransactionRepository';
import { Account, Address } from '../../src/model/account';
import { BlockInfo } from '../../src/model/blockchain/BlockInfo';
import { MosaicId } from '../../src/model/mosaic';
import { NamespaceId } from '../../src/model/namespace';
import { NetworkType } from '../../src/model/network/NetworkType';
import { TransactionType } from '../../src/model/transaction/TransactionType';
import { TransferTransaction } from '../../src/model/transaction/TransferTransaction';
import { TransactionService } from '../../src/service/TransactionService';
import { IntegrationTestHelper } from '../infrastructure/IntegrationTestHelper';

interface TransactionWithBlock {
transaction: TransferTransaction;
block: BlockInfo;
}

describe('TransactionPaginationStreamer', () => {
const helper = new IntegrationTestHelper();
let generationHash: string;
let addressAlias: NamespaceId;
let mosaicAlias: NamespaceId;
let mosaicId: MosaicId;
let newMosaicId: MosaicId;
let transactionHashes: string[];
let transactionHashesMultiple: string[];
let account: Account;
let account2: Account;
let account3: Account;
let cosignAccount4: Account;
let networkType: NetworkType;
let transactionService: TransactionService;
let transactionRepository: TransactionRepository;

before(() => {
return helper.start({ openListener: true }).then(() => {
account = helper.account;
account2 = helper.account2;
account3 = helper.account3;
cosignAccount4 = helper.cosignAccount4;
generationHash = helper.generationHash;
networkType = helper.networkType;
transactionHashes = [];
transactionHashesMultiple = [];
transactionRepository = helper.repositoryFactory.createTransactionRepository();
transactionService = new TransactionService(
helper.repositoryFactory.createTransactionRepository(),
helper.repositoryFactory.createReceiptRepository(),
);
});
});

after(() => {
return helper.close();
});

const getTransactionWithBlocks = (repositoryFactory: RepositoryFactory, address?: Address): Promise<TransactionWithBlock[]> => {
const transactionRepository = repositoryFactory.createTransactionRepository();
const blockRepository = repositoryFactory.createBlockRepository();
const searchCriteria: TransactionSearchCriteria = {
address: address,
group: TransactionGroup.Confirmed,
type: [TransactionType.TRANSFER],
embedded: true,
pageSize: 10,
};

const streamer = new TransactionPaginationStreamer(transactionRepository);
const observableOfResults = streamer.search(searchCriteria).pipe(
mergeMap((transaction) => {
return blockRepository.getBlockByHeight(transaction.transactionInfo!.height!).pipe(
map((block) => {
return {
transaction: transaction as TransferTransaction,
block,
};
}),
);
}),
);
return observableOfResults.pipe(toArray()).toPromise();
};

describe('Get Transactions', () => {
it('Transfer with blocks', async () => {
const transactionWithBlock = await getTransactionWithBlocks(helper.repositoryFactory);
console.log(transactionWithBlock.length);
});
});
});
24 changes: 5 additions & 19 deletions src/service/CurrencyService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,26 @@
* limitations under the License.
*/

import { defer, forkJoin, Observable } from 'rxjs';
import { forkJoin, Observable } from 'rxjs';
import { flatMap, map } from 'rxjs/internal/operators';
import { shareReplay } from 'rxjs/operators';
import { DtoMapping } from '../core/utils/DtoMapping';
import { RepositoryFactory } from '../infrastructure/RepositoryFactory';
import { MosaicId } from '../model/mosaic';
import { MosaicInfo } from '../model/mosaic';
import { MosaicNames } from '../model/mosaic';
import { Currency } from '../model/mosaic';
import { NetworkCurrencies } from '../model/mosaic';
import { Currency, MosaicId, MosaicInfo, MosaicNames, NetworkCurrencies } from '../model/mosaic';
import { NamespaceId } from '../model/namespace';
import { ICurrencyService } from './interfaces';

/**
* A service used to load Currencies objects.
*/
export class CurrencyService implements ICurrencyService {
/**
* Local cache for symbol network currencies.
*/
private readonly networkCurrenciesObservable: Observable<NetworkCurrencies>;
x;

constructor(private readonly repositoryFactory: RepositoryFactory) {
this.networkCurrenciesObservable = defer(() => this.loadNetworkCurrencies()).pipe(shareReplay(1));
}
constructor(private readonly repositoryFactory: RepositoryFactory) {}

/**
* This method loads and caches the network currencies.
* This method loads the network currencies.
*/
public getNetworkCurrencies(): Observable<NetworkCurrencies> {
return this.networkCurrenciesObservable;
}

private loadNetworkCurrencies(): Observable<NetworkCurrencies> {
return this.repositoryFactory
.createNetworkRepository()
.getNetworkProperties()
Expand Down
2 changes: 1 addition & 1 deletion src/service/interfaces/ICurrencyService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { NetworkCurrencies } from '../../model/mosaic';
*/
export interface ICurrencyService {
/**
* This method loads and caches the network currencies (main currency and harvest).
* This method loads the network currencies (main currency and harvest).
*/
getNetworkCurrencies(): Observable<NetworkCurrencies>;

Expand Down

0 comments on commit 479a83c

Please sign in to comment.