forked from JSKitty/scc-web3
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Refactor] Get rid of control variables (#341)
* Implement LockableFunction class * Use LockableFunction in wallet.js * Implement the lockablefunction with a function in place of a class * Remove isCreatingTx * Forgot to call the function * fix bad merge * Update JSdoc to take in account generic parameters * fix bad merge * add lockable function unit tests * review fix
- Loading branch information
Showing
5 changed files
with
142 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Implement a lockable object | ||
* @template T | ||
* @param {T} f - the function on which we perform the lock | ||
* @returns T & { isLocked: () => bool } | ||
*/ | ||
export const lockableFunction = (f) => { | ||
let lock = false; | ||
const g = async (...args) => { | ||
try { | ||
if (!lock) { | ||
lock = true; | ||
return await f(...args); | ||
} | ||
} finally { | ||
lock = false; | ||
} | ||
}; | ||
g.isLocked = () => lock; | ||
return g; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { lockableFunction } from '../../scripts/lock.js'; | ||
import { sleep } from '../../scripts/utils.js'; | ||
import { beforeEach, it } from 'vitest'; | ||
|
||
describe('Lockable function tests', () => { | ||
let test_function; | ||
const sleep_time = 1000; | ||
beforeEach(() => { | ||
test_function = lockableFunction(async (str_input) => { | ||
await sleep(sleep_time); | ||
return str_input; | ||
}); | ||
}); | ||
it('Lockable function returns the correct value', async () => { | ||
expect(await test_function('test_locks')).toBe('test_locks'); | ||
}); | ||
it('Lockable function gives the correct value for the lock', async () => { | ||
// At the beginning there is no lock | ||
expect(test_function.isLocked()).toBeFalsy(); | ||
test_function('test_locks'); | ||
await sleep(sleep_time / 2); | ||
// When the function is running the lock is acquired | ||
expect(test_function.isLocked()).toBeTruthy(); | ||
await sleep(sleep_time / 2); | ||
// When the function stop running the lock is dropped | ||
expect(test_function.isLocked()).toBeFalsy(); | ||
}); | ||
it("Calling when locked doesn't make the function run twice", async () => { | ||
test_function('test_locks'); | ||
expect(await test_function('test_locks')).toBeUndefined(); | ||
}); | ||
}); |