Skip to content

Commit

Permalink
Re-add unsafe_ exports as deprecated
Browse files Browse the repository at this point in the history
To smooth the transition for any packages relying on these exports.
  • Loading branch information
steverice committed Jan 22, 2025
1 parent 99a7948 commit baa8542
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/compiler/src/experimental/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export {
mutateSubgraphWithNamespace as unsafe_mutateSubgraphWithNamespace,
} from "./mutators.js";
export { Realm as unsafe_Realm } from "./realm.js";
export { unsafe_useStateMap, unsafe_useStateSet } from "./state-accessor.js";
export { $ as unsafe_$ } from "./typekit/index.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>;

/** @deprecated use `useStateMap` from `@typespec/compiler/utils` instead */
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;

/** @deprecated use `useStateSet` from `@typespec/compiler/utils` instead */
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];
}

0 comments on commit baa8542

Please sign in to comment.