Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions setupTests-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from './__tests__/integration/helpers/wallet-precalculation.helper';
import { GenesisWalletHelper } from './__tests__/integration/helpers/genesis-wallet.helper';
import { generateWalletHelper, waitNextBlock, waitTxConfirmed } from './__tests__/integration/helpers/wallet.helper';
import { stopGLLBackgroundTask } from './src/sync/gll';

config.setTxMiningUrl(TX_MINING_URL);

Expand Down Expand Up @@ -97,3 +98,6 @@ expect.extend({
}
}
});

// Stop gll interval to avoid background tasks during tests
stopGLLBackgroundTask();
4 changes: 4 additions & 0 deletions setupTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ process.env.NODE_ENV = 'test';
// Mocking localStorage for tests
import 'jest-localstorage-mock';
import helpers from './src/utils/helpers';
import { stopGLLBackgroundTask } from './src/sync/gll';
// Mocking WebSocket for tests
import { Server, WebSocket } from 'mock-socket';
global.WebSocket = WebSocket;
Expand Down Expand Up @@ -75,3 +76,6 @@ expect.extend({
const { TextEncoder, TextDecoder } = require('util');
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;

// Stop gll interval to avoid background tasks during tests
stopGLLBackgroundTask();
2 changes: 2 additions & 0 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import {
TransactionTemplateBuilder,
WalletTxTemplateInterpreter,
} from './template/transaction';
import { stopGLLBackgroundTask } from './sync/gll';

export {
PartialTx,
Expand Down Expand Up @@ -117,6 +118,7 @@ export {
TransactionTemplate,
TransactionTemplateBuilder,
WalletTxTemplateInterpreter,
stopGLLBackgroundTask,
};

export * from './nano_contracts/types';
Expand Down
13 changes: 11 additions & 2 deletions src/models/promise_queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ export default class PromiseQueue extends EventEmitter {

#intervalId: ReturnType<typeof setTimeout> | null = null;

constructor() {
constructor(autostart: boolean = true) {
super();
this.#queue = new PriorityQueue<() => Promise<void>>();
this.#jobsRunning = 0;
this.#startInterval();
if (autostart) {
this.#startInterval();
}
}

/**
Expand Down Expand Up @@ -75,6 +77,13 @@ export default class PromiseQueue extends EventEmitter {
}
}

/**
* Allow users to stop the background interval so there is no hanging tasks.
*/
stopBackgroundTask() {
this.#clearInterval();
}

/**
* Check if the PromiseQueue is paused.
*/
Expand Down
10 changes: 10 additions & 0 deletions src/sync/gll.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* Copyright (c) Hathor Labs and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import PromiseQueue from '../models/promise_queue';

const MAX_CONCURRENT_LOAD_TASKS = 3;
Expand All @@ -6,3 +12,7 @@ const GLL = new PromiseQueue();
GLL.concurrent = MAX_CONCURRENT_LOAD_TASKS;

export default GLL;

export function stopGLLBackgroundTask() {
GLL.stopBackgroundTask();
}
6 changes: 6 additions & 0 deletions src/sync/stream.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* Copyright (c) Hathor Labs and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { Address as BitcoreAddress, HDPublicKey } from 'bitcore-lib';
import queueMicrotask from 'queue-microtask';
import FullNodeConnection from '../new/connection';
Expand Down
Loading