Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update session state from set global/alias/module ... and configure session ... commands #201

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion shared/studio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"react-hook-form": "^7.32.2"
},
"peerDependencies": {
"edgedb": "^1.2.0",
"edgedb": "^1.3.1",
"react": "^17.0.0",
"react-dom": "^17.0.0"
},
Expand Down
27 changes: 26 additions & 1 deletion shared/studio/state/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,13 @@ export class Connection extends Model({
});
private _queryQueue: PendingQuery[] = [];

private _baseSessionConfig = Session.defaults();

@computed
get _state() {
const sessionState = sessionStateCtx.get(this);

let state = Session.defaults();
let state = this._baseSessionConfig;

if (sessionState?.activeState.globals.length) {
state = state.withGlobals(
Expand Down Expand Up @@ -216,6 +218,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 +264,27 @@ export class Connection extends Model({
execute: Math.round(executeEndTime - parseEndTime),
};

const stateUpdate = (this.conn as any).lastStateUpdate;
if (!opts.ignoreSessionConfig && stateUpdate) {
let newState = Session.defaults();
if (stateUpdate.module) {
newState = newState.withModuleAliases({module: stateUpdate.module});
}
if (stateUpdate.aliases) {
newState = newState.withModuleAliases(
(stateUpdate.aliases as [string, string][]).reduce(
(aliases, [key, val]) => {
aliases[key] = val;
return aliases;
},
{} as {[key: string]: string}
)
);
}
this._baseSessionConfig = newState;
sessionStateCtx.get(this)!.updateStateFromCommand(stateUpdate);
}

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

@modelAction
updateStateFromCommand(stateUpdate: {
globals?: {[key: string]: any};
config?: {[key: string]: any};
}) {
const dbState = dbCtx.get(this)!;

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

const unsetGlobals = new Set<string>();
for (const [globalName, val] of Object.entries(
stateUpdate.globals ?? {}
)) {
const type = schemaDataGlobals.get(globalName);
if (!type) {
continue;
}

if (val === null && !type.default) {
unsetGlobals.add(globalName);
} 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,
};
}
}
for (const [globalName, globalState] of Object.entries(
this.draftState!.globals
)) {
if (
globalState.active &&
(unsetGlobals.has(globalName) || !stateUpdate.globals?.[globalName])
) {
globalState.active = false;
const [newVal, err] = newPrimitiveValue(
globalState.type.data as PrimitiveType
);
globalState.value = frozen(newVal);
globalState.error = err;
}
}

for (const [configName, configState] of Object.entries(
this.draftState!.config
)) {
const val = stateUpdate.config?.[configName];
if (val === undefined) {
if (configState.active) {
configState.active = false;
const [newVal, err] = newPrimitiveValue(
configState.type.data as PrimitiveType
);
configState.value = frozen(newVal);
configState.error = err;
}
} else {
configState.active = true;
configState.value = frozen(
val != null
? valueToEditorValue(val, configState.type.data as PrimitiveType)
: null
);
configState.error = false;
}
}

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

storeSessionData() {
const instanceState = instanceCtx.get(this)!;
const dbState = dbCtx.get(this)!;
Expand Down
7 changes: 7 additions & 0 deletions shared/studio/tabs/repl/commands.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ export function renderCommandResult(result: CommandResult) {
<div className={styles.command}>\c, \connect DBNAME</div>
<div className={styles.info}>Switch to database DBNAME</div>

<div className={styles.heading}>Settings</div>

<div className={styles.command}>\set limit LIMIT</div>
<div className={styles.info}>
Set implicit limit to LIMIT. Set to 0 to disable
</div>

<div className={styles.heading}>Help</div>

<div className={styles.command}>\?, \h, \help</div>
Expand Down
45 changes: 45 additions & 0 deletions shared/studio/tabs/repl/state/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {SchemaObjectType, SchemaScalarType} from "@edgedb/common/schemaData";
import {Repl, ReplHistoryItem} from ".";
import {dbCtx} from "../../../state";
import {instanceCtx} from "../../../state/instance";
import {sessionStateCtx} from "../../../state/sessionState";

export enum CommandOutputKind {
error,
Expand Down Expand Up @@ -92,6 +93,50 @@ export async function handleSlashCommand(
repl.updateSetting("retroMode", !repl.settings.retroMode);
break;
}
case "set": {
switch (args[0]) {
case "limit":
const limit = parseInt(args[1], 10);
if (Number.isNaN(limit) || limit < 0) {
item.setCommandResult({
kind: CommandOutputKind.error,
msg: `invalid limit: '${args[1]}'`,
});
} else {
item.setCommandResult({kind: CommandOutputKind.none});
const sessionState = sessionStateCtx.get(repl)!;
const limitState =
sessionState.draftState?.options["Implicit Limit"];
console.log(sessionState, limitState);
if (limitState) {
if (limit === 0) {
if (limitState.active) {
sessionState.toggleOptionActive("Implicit Limit");
}
} else {
sessionState.updateItemValue(
limitState,
limit.toString(),
false
);
if (!limitState.active) {
sessionState.toggleOptionActive("Implicit Limit");
}
}
sessionState.updateActiveState();
sessionState.storeSessionData();
}
}
break;
default:
item.setCommandResult({
kind: CommandOutputKind.error,
msg: `unknown set command: '${args[0]}'`,
});
break;
}
break;
}
default:
item.setCommandResult({
kind: CommandOutputKind.error,
Expand Down
2 changes: 1 addition & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@types/react": "^17.0.20",
"@types/react-dom": "^17.0.9",
"buffer": "^6.0.3",
"edgedb": "^1.3.0",
"edgedb": "^1.3.1",
"hash.js": "^1.1.7",
"mobx": "^6.5.0",
"mobx-keystone": "^0.67.2",
Expand Down
12 changes: 6 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2071,7 +2071,7 @@ __metadata:
react-error-boundary: ^3.1.4
react-hook-form: ^7.32.2
peerDependencies:
edgedb: ^1.2.0
edgedb: ^1.3.1
react: ^17.0.0
react-dom: ^17.0.0
languageName: unknown
Expand Down Expand Up @@ -6478,12 +6478,12 @@ __metadata:
languageName: node
linkType: hard

"edgedb@npm:^1.3.0":
version: 1.3.0
resolution: "edgedb@npm:1.3.0"
"edgedb@npm:^1.3.1":
version: 1.3.1
resolution: "edgedb@npm:1.3.1"
bin:
edgeql-js: dist/cli.js
checksum: 59460dfcc6edcbcf2b762bc685f152f65e39e89e83532cfec5ed91d3cb1c61157a517a4ac5255a5319c084ecc4a4d8f8d6e69ce40206bfbdb6458fda590e8427
checksum: 35fcec5deffd5ed151cf3a9f2f517a83a4164415b8c6cf8be9155b4b7429a93079566c68b74128dbb14a668a723ab2e21cd8ecb40f80808743878c3114d65c00
languageName: node
linkType: hard

Expand Down Expand Up @@ -14434,7 +14434,7 @@ __metadata:
"@types/react-dom": ^17.0.9
"@types/selenium-webdriver": ^4.1.10
buffer: ^6.0.3
edgedb: ^1.3.0
edgedb: ^1.3.1
hash.js: ^1.1.7
jest: ^29.4.0
mobx: ^6.5.0
Expand Down