Skip to content

Commit

Permalink
Merge pull request #967 from y-lohse/expose-better-getset-types
Browse files Browse the repository at this point in the history
Add typing information to get/set proxy typing
  • Loading branch information
smwhr authored Oct 3, 2022
2 parents 2ad2a13 + 7386907 commit 6a590f2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 45 deletions.
88 changes: 48 additions & 40 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"rollup-plugin-terser": "7.0.2",
"rollup-plugin-typescript2": "0.31.2",
"ts-jest": "26.5.6",
"ts-node": "10.5.0",
"ts-node": "^10.9.1",
"tslint": "6.1.3",
"typescript": "4.3.5"
}
Expand Down
23 changes: 19 additions & 4 deletions src/engine/VariablesState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,20 @@ import { throwNullException } from "./NullException";
import { CallStack } from "./CallStack";
import { StatePatch } from "./StatePatch";
import { SimpleJson } from "./SimpleJson";
import { InkList } from "./Story";
import { Path } from "./Path";

export class VariablesState {
// Fake class wrapper around VariableState to have correct typing
// when using the Proxy syntax in typescript
function VariablesStateAccessor<T>(): new () => Pick<T, keyof T> {
return class {} as any;
}

type VariableStateValue = boolean | string | number | InkList | Path | null;

export class VariablesState extends VariablesStateAccessor<
Record<string, any>
>() {
// The way variableChangedEvent is a bit different than the reference implementation.
// Originally it uses the C# += operator to add delegates, but in js we need to maintain
// an actual collection of delegates (ie. callbacks) to register a new one, there is a
Expand Down Expand Up @@ -70,7 +82,9 @@ export class VariablesState {
// the original code uses a magic getter and setter for global variables,
// allowing things like variableState['varname]. This is not quite possible
// in js without a Proxy, so it is replaced with this $ function.
public $(variableName: string, value: any) {
public $(variableName: string): VariableStateValue;
public $(variableName: string, value: VariableStateValue): void;
public $(variableName: string, value?: any) {
if (typeof value === "undefined") {
let varContents = null;

Expand Down Expand Up @@ -116,6 +130,7 @@ export class VariablesState {
callStack: CallStack,
listDefsOrigin: ListDefinitionsOrigin | null
) {
super();
this._globalVariables = new Map();
this._callStack = callStack;
this._listDefsOrigin = listDefsOrigin;
Expand All @@ -138,8 +153,8 @@ export class VariablesState {

return p;
} catch (e) {
// thr proxy object is not available in this context. we should warn the
// dev but writting to the console feels a bit intrusive.
// the proxy object is not available in this context. we should warn the
// dev but writing to the console feels a bit intrusive.
// console.log("ES6 Proxy not available - direct manipulation of global variables can't work, use $() instead.");
}
}
Expand Down

0 comments on commit 6a590f2

Please sign in to comment.