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

Implement state variables in rust #86

Merged
merged 33 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
d0a3057
create state variable framework
dqnykamp Dec 30, 2023
623e5c7
text state variables and component profiles
dqnykamp Dec 31, 2023
8ae3ecf
wip: base rendered text component off state variable
dqnykamp Jan 1, 2024
79a7c91
component state variable functions, state var reference
dqnykamp Jan 2, 2024
1c92575
Remove ComponentStateVariables trait
dqnykamp Jan 2, 2024
6a669f5
rename ComponentNodeBase to ComponentNodeStateVariables
dqnykamp Jan 2, 2024
57a7fdc
implement draft algorithm to calculate state variables
dqnykamp Jan 3, 2024
35383bb
comments and small tweaks
dqnykamp Jan 3, 2024
24244e2
handle actions to update state variables and send back flat dast updates
dqnykamp Jan 7, 2024
59df8c5
match public state variables from macros, creating a state variable e…
dqnykamp Jan 8, 2024
66cb099
freshen stale renderer components iteratively to avoid stack overflow
dqnykamp Jan 8, 2024
108fc21
resolve() in finally block
dqnykamp Jan 8, 2024
be34810
Merge branch '0.7' into state-vars
dqnykamp Jan 8, 2024
e6e440e
basic dispatch_action works
dqnykamp Jan 9, 2024
3061f99
small updates
dqnykamp Jan 9, 2024
7775666
break apart files
dqnykamp Jan 9, 2024
7d6b462
move function
dqnykamp Jan 9, 2024
df0b31f
renaming
dqnykamp Jan 9, 2024
6e61687
Dispatch and process updates
siefkenj Jan 9, 2024
38dacea
Set children on update
siefkenj Jan 9, 2024
91798c6
Merge pull request #2 from siefkenj/drust
dqnykamp Jan 9, 2024
78ace38
rework freshen state variable algorithm.
dqnykamp Jan 9, 2024
0f5a2e9
Merge remote-tracking branch 'origin/state-vars' into state-vars
dqnykamp Jan 9, 2024
99d6d60
Bug fixes for new freshen state algorithm
dqnykamp Jan 9, 2024
708dd9a
adding tailcall
dqnykamp Jan 9, 2024
b75eaba
tweak case-insensitive comparison
dqnykamp Jan 9, 2024
702a72c
fix panic! due to component borrow_mut() overlapping with borrow()
dqnykamp Jan 9, 2024
339764b
use enum_dispatch rather than custom proc macros
dqnykamp Jan 10, 2024
9e0e095
tweaks to freshen_state_var
dqnykamp Jan 10, 2024
b7ca01d
add standard component field via attribute macro
dqnykamp Jan 10, 2024
5d49903
minor tweaks
dqnykamp Jan 10, 2024
8d6dcd3
add common component fields via struct rather than macro
dqnykamp Jan 10, 2024
4fabe57
remove unused import
dqnykamp Jan 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions packages/doenetml-prototype/dev/testCode.doenet
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<textInput prefill="hi" />
<textInput prefill="hi" name="ti" />
The value: $ti.value
<!--

<p>Use this to <m>a < b</m>test DoenetML</p>
<p>A second paragraph! <text>with text</text></p>
<choiceInput>
Expand All @@ -9,12 +12,9 @@
<graph showNavigation>
<line through="(-8,8) (9,6)" />
<point>(2,3)</point>
<!--
<line through="(0,4)" slope="1/2" styleNumber="2" />

<line equation="y=2x-8" styleNumber="3" />
<line equation="x=-6" styleNumber="4" />
-->
</graph>
<section boxed>
<p>A lovely section.</p>
Expand All @@ -40,4 +40,6 @@

<section>
<p><lorem generateWords="10" /></p>
</section>
</section>

-->
Original file line number Diff line number Diff line change
@@ -1,18 +1,54 @@
import React from "react";
import { BasicComponent } from "../types";
import { useAppSelector } from "../../state/hooks";
import { useAppDispatch, useAppSelector } from "../../state/hooks";
import { renderingOnServerSelector } from "../../state/redux-slices/global";
import "./text-input.css";
import { coreActions } from "../../state/redux-slices/core";

export const TextInput: BasicComponent = ({ node }) => {
type TextInputData = { state: { immediateValue: string } };

export const TextInput: BasicComponent<TextInputData> = ({ node }) => {
const onServer = useAppSelector(renderingOnServerSelector);
const id = node.data.id;
const value = node.data.state.immediateValue;
const dispatch = useAppDispatch();

const updateValue = React.useCallback(() => {
dispatch(
coreActions.dispatchAction({
actionName: "updateValue",
componentIdx: id,
args: { text: value },
}),
);
}, [dispatch, value]);

if (onServer) {
return <span className="text-input"></span>;
return <span className="text-input">{value}</span>;
}

return (
<span className="text-input">
<label>
<input type="text" />
<input
type="text"
value={value}
onChange={(e) => {
dispatch(
coreActions.dispatchAction({
actionName: "updateImmediateValue",
componentIdx: id,
args: { text: e.target.value },
}),
);
}}
onBlur={updateValue}
onKeyUp={(e) => {
if (e.key === "Enter") {
updateValue();
}
}}
/>
</label>
</span>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Comlink from "comlink";
import { createLoggingAsyncThunk } from "../../hooks";
import { CoreWorker } from "@doenet/doenetml-worker-rust";
import { Action, CoreWorker } from "@doenet/doenetml-worker-rust";
import { doenetGlobalConfig } from "../../../global-config";
import { RootState } from "../../store";
import { _coreReducerActions, selfSelector } from "./slice";
Expand Down Expand Up @@ -86,6 +86,23 @@ export const coreThunks = {
}
},
),
dispatchAction: createLoggingAsyncThunk(
"core/dispatchAction",
async (action: Action, { dispatch, getState }) => {
const worker = getWorker(getState());
if (worker == null) {
throw new Error("No worker loaded");
}

try {
const updates = await worker.dispatchAction(action);
dispatch(_dastReducerActions.processElementUpdates(updates));
} catch (e) {
dispatch(_coreReducerActions._setInErrorState(true));
console.warn(e);
}
},
),
};

export function getWorker(
Expand Down
38 changes: 37 additions & 1 deletion packages/doenetml-prototype/src/state/redux-slices/dast/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { createSlice } from "@reduxjs/toolkit";
import type { PayloadAction } from "@reduxjs/toolkit";
import type { RootState } from "../../store";
import { DastError, DastRoot } from "@doenet/parser";
import type { FlatDastRoot } from "@doenet/doenetml-worker-rust";
import type {
ElementUpdates,
FlatDastRoot,
} from "@doenet/doenetml-worker-rust";

// Define a type for the slice state
export interface DastState {
Expand Down Expand Up @@ -54,6 +57,39 @@ const dastSlice = createSlice({
state.flatDastRoot.elements[index] = element;
}
},
/**
* Process an `elementUpdate` coming from Core.
*/
processElementUpdates: (
state,
action: PayloadAction<ElementUpdates>,
) => {
for (const [id, update] of Object.entries(action.payload)) {
const elm = state.flatDastRoot.elements[Number(id)];
if (elm == null) {
console.error(
"Failed to find element in FlatDast with id =",
id,
"during element update request.",
);
continue;
}
if (elm.type === "error") {
throw new Error("Updating errors is not yet implemented");
}
if (update.changed_state) {
elm.data ??= { id: Number(id), state: {} };
elm.data.state ??= {};
Object.assign(elm.data.state, update.changed_state);
}
if (update.changed_attributes) {
console.warn("Updating attributes is not yet implemented");
}
if (update.new_children) {
elm.children = update.new_children;
}
}
},
},
});

Expand Down
6 changes: 3 additions & 3 deletions packages/doenetml-worker-rust/doenetml-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ strum_macros = "0.25"
[dev-dependencies]
criterion = "0.3.6"

[[bench]]
name = "my_benchmark"
harness = false
# [[bench]]
# name = "my_benchmark"
# harness = false



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ proc-macro = true
quote = "1"
proc-macro2 = "1.0"
syn = "2.0"
# syn = { version = "2.0", features = ["extra-traits"] }
convert_case = "0.5.0"
Loading