Skip to content

Commit

Permalink
refactor: move context and stack to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
exuanbo committed Oct 13, 2024
1 parent cac7fc4 commit 50c433f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 30 deletions.
3 changes: 2 additions & 1 deletion src/container.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {type InjectionConfig, isConfigLike} from './config'
import {assert, ErrorMessage, expectNever, invariant} from './errors'
import type {Injections} from './injection'
import {Stack, useInjectionContext, withInjectionContext} from './injection-context'
import {useInjectionContext, withInjectionContext} from './injection-context'
import {getMetadata} from './metadata'
import {type InjectionProvider, isClassProvider, isFactoryProvider, isProvider, isValueProvider} from './provider'
import {InjectionScope} from './scope'
import {type Constructor, type InjectionToken, isConstructor, Type} from './token'
import {Stack} from './utils/stack'

const ProviderRegistry: typeof Map<InjectionToken, InjectionProvider> = Map

Expand Down
31 changes: 2 additions & 29 deletions src/injection-context.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type {Container} from './container'
import {createContext} from './create-context'
import type {InjectionScope} from './scope'
import type {InjectionToken} from './token'
import {createContext} from './utils/context'
import type {Stack} from './utils/stack'

export interface InjectionContext {
container: Container
Expand All @@ -14,34 +15,6 @@ export interface Resolution {
dependents: Map<InjectionToken, any>
}

// @internal
export class Stack<K, V> {
#entries = new Array<{key: K, value: V}>()
#keys = new Set<K>()

push(key: K, value: V) {
this.#entries.push({key, value})
this.#keys.add(key)
}

pop() {
const entry = this.#entries.pop()
if (entry) {
this.#keys.delete(entry.key)
return entry.value
}
}

peek() {
const entry = this.#entries.at(-1)
return entry?.value
}

has(key: K) {
return this.#keys.has(key)
}
}

export interface ResolutionFrame {
scope: ResolvedScope
token: InjectionToken
Expand Down
File renamed without changes.
27 changes: 27 additions & 0 deletions src/utils/stack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// @internal
export class Stack<K, V> {
#entries = new Array<{key: K, value: V}>()
#keys = new Set<K>()

push(key: K, value: V) {
this.#entries.push({key, value})
this.#keys.add(key)
}

pop() {
const entry = this.#entries.pop()
if (entry) {
this.#keys.delete(entry.key)
return entry.value
}
}

peek() {
const entry = this.#entries.at(-1)
return entry?.value
}

has(key: K) {
return this.#keys.has(key)
}
}

0 comments on commit 50c433f

Please sign in to comment.