-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use reusable state accessor for compiler decorators (#4465)
Idea that could be expanded/exposed to other libraries in the future. Define clearly how to access a certain state key. Doing that now as for paging there is a lot of decorators that are added and that will simplify.
- Loading branch information
1 parent
9a28a9c
commit 3eed298
Showing
11 changed files
with
200 additions
and
148 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
.chronus/changes/refactor-state-accessor-2024-8-17-15-11-51.md
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,7 @@ | ||
--- | ||
changeKind: internal | ||
packages: | ||
- "@typespec/http-server-csharp" | ||
--- | ||
|
||
Fix potential undefined |
8 changes: 8 additions & 0 deletions
8
.chronus/changes/refactor-state-accessor-2024-8-17-21-56-42.md
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,8 @@ | ||
--- | ||
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking | ||
changeKind: internal | ||
packages: | ||
- "@typespec/compiler" | ||
--- | ||
|
||
Use reusable state accessor for compiler decorators |
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 @@ | ||
export { unsafe_useStateMap, unsafe_useStateSet } from "./state-accessor.js"; |
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,30 @@ | ||
import type { Program } from "../core/program.js"; | ||
import type { Type } from "../core/types.js"; | ||
|
||
type StateMapGetter<K extends Type, V> = (program: Program, type: K) => V | undefined; | ||
type StateMapSetter<K extends Type, V> = (program: Program, type: K, value: V) => void; | ||
type StateMapMapGetter<K extends Type, V> = (program: Program) => Map<K, V>; | ||
|
||
/** @experimental */ | ||
export function unsafe_useStateMap<K extends Type, V>( | ||
key: symbol, | ||
): [StateMapGetter<K, V>, StateMapSetter<K, V>, StateMapMapGetter<K, V>] { | ||
const getter = (program: Program, target: K) => program.stateMap(key).get(target); | ||
const setter = (program: Program, target: K, value: V) => | ||
program.stateMap(key).set(target, value); | ||
const mapGetter = (program: Program) => program.stateMap(key); | ||
return [getter, setter, mapGetter as any]; | ||
} | ||
|
||
type StateSetGetter<K extends Type> = (program: Program, type: K) => boolean; | ||
type StateSetSetter<K extends Type> = (program: Program, type: K) => void; | ||
|
||
/** @experimental */ | ||
export function unsafe_useStateSet<K extends Type>( | ||
key: symbol, | ||
): [StateSetGetter<K>, StateSetSetter<K>] { | ||
const getter = (program: Program, target: K) => program.stateSet(key).has(target); | ||
const setter = (program: Program, target: K) => program.stateSet(key).add(target); | ||
|
||
return [getter, setter]; | ||
} |
Oops, something went wrong.