Skip to content
This repository was archived by the owner on Apr 6, 2020. It is now read-only.

Commit ccd0af5

Browse files
committed
Factor out Blockchain#_lockUnlock
1 parent 8686de0 commit ccd0af5

File tree

1 file changed

+21
-57
lines changed

1 file changed

+21
-57
lines changed

src/index.ts

Lines changed: 21 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -229,29 +229,13 @@ export default class Blockchain {
229229
* Adds a block to the blockchain
230230
*/
231231
putBlock(block: object, cb: any, isGenesis?: any) {
232-
const self = this;
233-
234232
// make sure init has completed
235-
self._initLock.await(() => {
233+
this._initLock.await(() => {
236234
// perform put with mutex dance
237-
lockUnlock((done: any) => {
238-
self._putBlockOrHeader(block, done, isGenesis);
235+
this._lockUnlock((done: any) => {
236+
this._putBlockOrHeader(block, done, isGenesis);
239237
}, cb);
240238
});
241-
242-
// lock, call fn, unlock
243-
function lockUnlock(fn: any, cb: any) {
244-
// take lock
245-
self._putSemaphore.take(function() {
246-
// call fn
247-
fn(function() {
248-
// leave lock
249-
self._putSemaphore.leave();
250-
// exit
251-
cb.apply(null, arguments);
252-
});
253-
});
254-
}
255239
}
256240

257241
/**
@@ -271,29 +255,13 @@ export default class Blockchain {
271255
* Adds a header to the blockchain
272256
*/
273257
putHeader(header: object, cb: any) {
274-
const self = this;
275-
276258
// make sure init has completed
277-
self._initLock.await(() => {
259+
this._initLock.await(() => {
278260
// perform put with mutex dance
279-
lockUnlock((done: any) => {
280-
self._putBlockOrHeader(header, done);
261+
this._lockUnlock((done: any) => {
262+
this._putBlockOrHeader(header, done);
281263
}, cb);
282264
});
283-
284-
// lock, call fn, unlock
285-
function lockUnlock(fn: any, cb: any) {
286-
// take lock
287-
self._putSemaphore.take(function() {
288-
// call fn
289-
fn(function() {
290-
// leave lock
291-
self._putSemaphore.leave();
292-
// exit
293-
cb.apply(null, arguments);
294-
});
295-
});
296-
}
297265
}
298266

299267
_putBlockOrHeader(item: any, cb: any, isGenesis?: any) {
@@ -728,29 +696,13 @@ export default class Blockchain {
728696
* and any encountered heads are set to the parent block
729697
*/
730698
delBlock(blockHash: Buffer, cb: any) {
731-
const self = this;
732-
733699
// make sure init has completed
734-
self._initLock.await(() => {
700+
this._initLock.await(() => {
735701
// perform put with mutex dance
736-
lockUnlock((done: boolean) => {
737-
self._delBlock(blockHash, done);
702+
this._lockUnlock((done: boolean) => {
703+
this._delBlock(blockHash, done);
738704
}, cb);
739705
});
740-
741-
// lock, call fn, unlock
742-
function lockUnlock(fn: any, cb: any) {
743-
// take lock
744-
self._putSemaphore.take(function() {
745-
// call fn
746-
fn(function() {
747-
// leave lock
748-
self._putSemaphore.leave();
749-
// exit
750-
cb.apply(null, arguments);
751-
});
752-
});
753-
}
754706
}
755707

756708
_delBlock(blockHash: any, cb: any) {
@@ -1057,4 +1009,16 @@ export default class Blockchain {
10571009
}
10581010
);
10591011
}
1012+
1013+
_lockUnlock(fn: any, cb: any) {
1014+
const self = this
1015+
this._putSemaphore.take(() => {
1016+
fn(after)
1017+
1018+
function after() {
1019+
self._putSemaphore.leave();
1020+
cb.apply(null, arguments)
1021+
}
1022+
})
1023+
}
10601024
}

0 commit comments

Comments
 (0)