Skip to content

Commit

Permalink
Use reusable state accessor for compiler decorators (#4465)
Browse files Browse the repository at this point in the history
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
timotheeguerin authored Sep 17, 2024
1 parent 9a28a9c commit 3eed298
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 148 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: internal
packages:
- "@typespec/http-server-csharp"
---

Fix potential undefined
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
4 changes: 4 additions & 0 deletions packages/compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
"./emitter-framework": {
"types": "./dist/src/emitter-framework/index.d.ts",
"default": "./dist/src/emitter-framework/index.js"
},
"./experimental": {
"types": "./dist/src/experimental/index.d.ts",
"default": "./dist/src/experimental/index.js"
}
},
"browser": {
Expand Down
1 change: 1 addition & 0 deletions packages/compiler/src/experimental/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { unsafe_useStateMap, unsafe_useStateSet } from "./state-accessor.js";
30 changes: 30 additions & 0 deletions packages/compiler/src/experimental/state-accessor.ts
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];
}
Loading

0 comments on commit 3eed298

Please sign in to comment.