Skip to content

Commit

Permalink
evaluate all code blocks below current block with same scope
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiaskern committed Jul 31, 2018
1 parent 1fccc99 commit e0033cd
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 38 deletions.
10 changes: 9 additions & 1 deletion client/src_analyzer/Worker_Analyze.re
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ module Make = (ESig: Worker_Evaluator.EvaluatorSig) => {
}
)
);
let execute: (. bool, string) => list(Worker_Types.blockData) =
let execute: (. bool, string) => list(blockData) =
(. reset, code) => {
if (reset) {
Evaluator.reset();
Expand All @@ -213,4 +213,12 @@ module Make = (ESig: Worker_Evaluator.EvaluatorSig) => {
});
result;
};

let executeMany:
Belt.Map.String.t(string) => Belt.Map.String.t(list(blockData)) =
codeMap => {
/* Reset before evaluating several blocks */
Evaluator.reset();
codeMap |. Belt.Map.String.map(code => execute(. false, code));
};
};
2 changes: 1 addition & 1 deletion client/src_analyzer/Worker_Index.re
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ Worker.importScripts("/reason.js");

module Analyze = Worker_Analyze.Make(Worker_BrowserEvaluator);

let obj = {"execute": Analyze.execute};
let obj = {"execute": Analyze.execute, "executeMany": Analyze.executeMany};

Comlink.(comlink |. expose(obj, Worker.self));
75 changes: 44 additions & 31 deletions client/src_editor/Editor_Blocks.re
Original file line number Diff line number Diff line change
Expand Up @@ -85,33 +85,46 @@ let make = (~blocks: array(block), _children) => {
};
}),
})
| Block_Execute(blockId) =>
let block = state.blocks |. arrayFind(({b_id}) => b_id == blockId);
switch (block) {
| None => ReasonReact.NoUpdate
| Some(block) =>
switch (block.b_data) {
| B_Text(_) => ReasonReact.NoUpdate
| B_Code({bc_value}) =>
ReasonReact.SideEffects(
(
self =>
Js.Promise.(
Editor_Worker.execute(. true, bc_value)
|> then_(result => {
let widgets =
Editor_Blocks_Utils.executeRessultToWidget(result);
resolve(
self.send(Block_AddWidgets(blockId, widgets)),
);
})
|> catch(error => resolve(Js.log(error)))
|> ignore
)
),
)
}
};
| Block_Execute(_blockId) =>
let allCodeBlocks =
Belt.Map.String.fromArray(
state.blocks
|. Belt.Array.keepU((. block) =>
switch (block.b_data) {
| B_Text(_) => false
| B_Code(_) => true
}
)
|. Belt.Array.mapU((. block) => {
let x =
switch (block.b_data) {
| B_Code({bc_value}) => bc_value
| _ => ""
};

(block.b_id, x);
}),
);
ReasonReact.SideEffects(
(
self =>
Js.Promise.(
Editor_Worker.executeMany(. allCodeBlocks)
|> then_(results => {
results
|. Belt.Map.String.forEachU((. blockId, result) => {
let widgets =
Editor_Blocks_Utils.executeResultToWidget(result);
self.send(Block_AddWidgets(blockId, widgets));
});

resolve();
})
|> catch(error => resolve(Js.log(error)))
|> ignore
)
),
);

| Block_UpdateValue(blockId, newValue, diff) =>
ReasonReact.Update({
Expand Down Expand Up @@ -199,7 +212,7 @@ let make = (~blocks: array(block), _children) => {
|. Editor_Blocks_Utils.syncLineNumber,
})
| Block_FocusUp(blockId) =>
let upperBlockId = {
let upperBlock = {
let rec loop = i =>
if (i >= 0) {
let {b_id} = state.blocks[i];
Expand All @@ -217,7 +230,7 @@ let make = (~blocks: array(block), _children) => {
};
loop((state.blocks |. Belt.Array.length) - 1);
};
switch (upperBlockId) {
switch (upperBlock) {
| None => ReasonReact.NoUpdate
| Some((upperBlockId, blockTyp)) =>
ReasonReact.Update({
Expand All @@ -226,7 +239,7 @@ let make = (~blocks: array(block), _children) => {
})
};
| Block_FocusDown(blockId) =>
let lowerBlockId = {
let lowerBlock = {
let length = state.blocks |. Belt.Array.length;
let rec loop = i =>
if (i < length) {
Expand All @@ -245,7 +258,7 @@ let make = (~blocks: array(block), _children) => {
};
loop(0);
};
switch (lowerBlockId) {
switch (lowerBlock) {
| None => ReasonReact.NoUpdate
| Some((lowerBlockId, blockTyp)) =>
ReasonReact.Update({
Expand Down
2 changes: 1 addition & 1 deletion client/src_editor/Editor_Blocks_Utils.re
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let renderErrorIndicator = (colStart, colEnd, content) =>
++ "\n"
++ content;

let executeRessultToWidget = (result: list(Worker_Types.blockData)) => {
let executeResultToWidget = (result: list(Worker_Types.blockData)) => {
open Worker_Types;
open Editor_CodeBlockTypes;

Expand Down
9 changes: 5 additions & 4 deletions client/src_editor/Editor_Worker.re
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ type js_executeResult = {
[@bs.deriving abstract]
type rtop = {
execute: (. bool, string) => Js.Promise.t(list(Worker_Types.blockData)),
/* reset: (. unit) => Js.Promise.t(unit),
reasonSyntax: (. unit) => Js.Promise.t(unit),
mlSyntax: (. unit) => Js.Promise.t(unit),
parseError: (. string, string) => Js.Promise.t(string), */
executeMany:
(. Belt.Map.String.t(string)) =>
Js.Promise.t(Belt.Map.String.t(list(Worker_Types.blockData))),
};
let worker = RtopWorker.make();

let rtop: rtop = Comlink.comlink |. Comlink.proxy(worker);

let execute = rtop |. executeGet;

let executeMany = rtop |. executeManyGet;

0 comments on commit e0033cd

Please sign in to comment.