Skip to content

Commit

Permalink
Update globals state from set global .../reset global ... commands
Browse files Browse the repository at this point in the history
  • Loading branch information
jaclarke committed Jun 14, 2023
1 parent 134b6f6 commit 4e015b5
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
9 changes: 9 additions & 0 deletions shared/studio/state/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ export class Connection extends Model({
);
}

(this.conn as any).lastStateUpdate = null;

const startTime = performance.now();

let inCodec, outCodec, outCodecBuf, capabilities, _;
Expand Down Expand Up @@ -260,6 +262,13 @@ export class Connection extends Model({
execute: Math.round(executeEndTime - parseEndTime),
};

sessionStateCtx
.get(this)!
.updateGlobalsFromCommand(
statements,
(this.conn as any).lastStateUpdate?.globals
);

return {
result: decode(outCodecBuf, resultBuf, opts.newCodec),
duration,
Expand Down
65 changes: 65 additions & 0 deletions shared/studio/state/sessionState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,71 @@ export class SessionState extends Model({
this.draftSnapshot = clone(this.draftState!);
}

@modelAction
updateGlobalsFromCommand(statements: string[], stateUpdate: any) {
const globalNames = new Set(
statements
.map(
(statement) =>
statement.match(/^\s*(?:re)?set\s+global\s+(.+?)(?:\s|:=|$)/i)?.[1]
)
.filter((name) => name) as string[]
);

if (!globalNames.size) {
return;
}

console.log(globalNames, stateUpdate);

const dbState = dbCtx.get(this)!;

const schemaDataGlobals = new Map(
[...dbState.schemaData!.globals.values()].map((global) => [
global.name,
global,
])
);

for (const globalName of globalNames) {
const type =
schemaDataGlobals.get(globalName) ??
schemaDataGlobals.get(`default::${globalName}`);
console.log(type);
if (!type) {
continue;
}

const val = stateUpdate?.[type.name];
if (val === undefined || (val === null && !type.default)) {
const existingGlobalState = this.draftState!.globals[type.name];
if (existingGlobalState) {
existingGlobalState.active = false;
const [newVal, err] = newPrimitiveValue(
type.target as PrimitiveType
);
existingGlobalState.value = frozen(newVal);
existingGlobalState.error = err;
}
} else {
const editorVal =
val != null
? valueToEditorValue(val, type.target as PrimitiveType)
: null;

this.draftState!.globals[type.name] = {
active: true,
type: frozen(type.target, FrozenCheckMode.Off),
value: frozen(editorVal),
error: false,
};
}
}

this.updateActiveState();
this.storeSessionData();
}

storeSessionData() {
const instanceState = instanceCtx.get(this)!;
const dbState = dbCtx.get(this)!;
Expand Down

0 comments on commit 4e015b5

Please sign in to comment.