Skip to content

Commit ae50b35

Browse files
committed
refactor: use asynclocalstorage for context management
1 parent fa32c77 commit ae50b35

File tree

3 files changed

+21
-23
lines changed

3 files changed

+21
-23
lines changed
Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1+
import { AsyncLocalStorage } from 'node:async_hooks'
12
import type { TestExecutionContext } from '../test-execution-context'
23

3-
declare global {
4-
// eslint-disable-next-line no-var
5-
var puyaTsExecutionContext: TestExecutionContext | undefined
6-
}
4+
export class ContextManager {
5+
private static asyncStore = new AsyncLocalStorage<TestExecutionContext>()
76

8-
export const ctxMgr = {
9-
set instance(ctx: TestExecutionContext) {
10-
const instance = global.puyaTsExecutionContext
11-
if (instance != undefined) throw new Error('Execution context has already been set')
12-
global.puyaTsExecutionContext = ctx
13-
},
14-
get instance() {
15-
const instance = global.puyaTsExecutionContext
16-
if (instance == undefined) throw new Error('No execution context has been set')
7+
static set instance(ctx: TestExecutionContext) {
8+
const instance = this.asyncStore.getStore()
9+
if (instance !== undefined) throw new Error('Execution context has already been set')
10+
this.asyncStore.enterWith(ctx)
11+
}
12+
static get instance() {
13+
const instance = this.asyncStore.getStore()
14+
if (instance === undefined) throw new Error('No execution context has been set')
1715
return instance
18-
},
19-
reset() {
20-
global.puyaTsExecutionContext = undefined
21-
},
16+
}
17+
static reset() {
18+
this.asyncStore.disable()
19+
}
2220
}

src/context-helpers/internal-context.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { AccountData, ApplicationData, AssetData } from '../impl/reference'
77
import type { VoterData } from '../impl/voter-params'
88
import type { TransactionGroup } from '../subcontexts/transaction-context'
99
import type { TestExecutionContext } from '../test-execution-context'
10-
import { ctxMgr } from './context-manager'
10+
import { ContextManager } from './context-manager'
1111

1212
/**
1313
* For accessing implementation specific functions, with a convenient single entry
@@ -16,7 +16,7 @@ import { ctxMgr } from './context-manager'
1616
*/
1717
class InternalContext {
1818
get value() {
19-
return ctxMgr.instance as TestExecutionContext
19+
return ContextManager.instance as TestExecutionContext
2020
}
2121

2222
get defaultSender() {

src/test-execution-context.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Account as AccountType, BaseContract, bytes, LogicSig, uint64 } from '@algorandfoundation/algorand-typescript'
22
import { DEFAULT_TEMPLATE_VAR_PREFIX } from './constants'
3-
import { ctxMgr } from './context-helpers/context-manager'
3+
import { ContextManager } from './context-helpers/context-manager'
44
import type { DecodedLogs, LogDecoding } from './decode-logs'
55
import { Account, AccountCls } from './impl/reference'
66
import { ContractContext } from './subcontexts/contract-context'
@@ -34,7 +34,7 @@ export class TestExecutionContext {
3434
* @param {bytes} [defaultSenderAddress] - The default sender address.
3535
*/
3636
constructor(defaultSenderAddress?: bytes) {
37-
ctxMgr.instance = this
37+
ContextManager.instance = this
3838
this.#contractContext = new ContractContext()
3939
this.#ledgerContext = new LedgerContext()
4040
this.#txnContext = new TransactionContext()
@@ -215,7 +215,7 @@ export class TestExecutionContext {
215215
this.#activeLogicSigArgs = []
216216
this.#template_vars = {}
217217
this.#compiledApps = []
218-
ctxMgr.reset()
219-
ctxMgr.instance = this
218+
ContextManager.reset()
219+
ContextManager.instance = this
220220
}
221221
}

0 commit comments

Comments
 (0)