Skip to content

Commit

Permalink
always run inputs that havent run yet
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfcosta committed Nov 20, 2024
1 parent 83d28d6 commit 7d5e326
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
3 changes: 2 additions & 1 deletion packages/editor/src/blocks/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ describe('computeDepencyQueue', () => {
clickedBlock,
layout,
blocks,
envStartedAt
envStartedAt,
false
).map((b) => b.getAttribute('id'))

const expected = [secondBlock].map((b) => b.getAttribute('id'))
Expand Down
56 changes: 47 additions & 9 deletions packages/editor/src/blocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,13 @@ export const requestRun = <B extends YBlock>(
skipDependencyCheck: boolean,
customOnRequestRun?: (block: B) => void
) => {
const dependencies = skipDependencyCheck
? []
: computeDepencyQueue(block, layout, blocks, environmentStartedAt)
const dependencies = computeDepencyQueue(
block,
layout,
blocks,
environmentStartedAt,
skipDependencyCheck
)

const queue = dependencies
if (!customOnRequestRun) {
Expand Down Expand Up @@ -158,9 +162,13 @@ export const requestTrySuggestion = (
environmentStartedAt: Date | null,
skipDependencyCheck = false
) => {
const dependencies = skipDependencyCheck
? []
: computeDepencyQueue(block, layout, blocks, environmentStartedAt)
const dependencies = computeDepencyQueue(
block,
layout,
blocks,
environmentStartedAt,
skipDependencyCheck
)
const queue = dependencies.concat(block)

for (const block of queue) {
Expand Down Expand Up @@ -334,6 +342,22 @@ export function isExecutableBlock(block: YBlock): boolean {
})
}

export function isInputBlock(block: YBlock): boolean {
return switchBlockType(block, {
onPython: () => false,
onSQL: () => false,
onVisualization: () => false,
onInput: () => true,
onDropdownInput: () => true,
onWriteback: () => false,
onDateInput: () => true,
onRichText: () => false,
onFileUpload: () => false,
onDashboardHeader: () => false,
onPivotTable: () => false,
})
}

export function duplicateYText(text: Y.Text): Y.Text {
const newText = new Y.Text()
newText.insert(0, text.toString())
Expand Down Expand Up @@ -393,12 +417,25 @@ function getExecutedAt(block: YBlock, blocks: Y.Map<YBlock>): Date | null {
function mustExecute(
block: YBlock,
blocks: Y.Map<YBlock>,
environmentStartedAt: Date | null
environmentStartedAt: Date | null,
skipDependencyCheck: boolean
): boolean {
if (!isExecutableBlock(block)) {
return false
}

// We should always run input blocks if they have not been run yet
// even if skipDependencyCheck is true
if (skipDependencyCheck) {
const lastExecutedAt = getExecutedAt(block, blocks)
const lastExecutedAtIsAfterEnvironmentStartedAt =
lastExecutedAt === null ||
environmentStartedAt === null ||
dfns.isAfter(environmentStartedAt, lastExecutedAt)

return isInputBlock(block) && lastExecutedAtIsAfterEnvironmentStartedAt
}

if (environmentStartedAt === null) {
return true
}
Expand All @@ -415,7 +452,8 @@ export function computeDepencyQueue(
block: YBlock,
layout: Y.Array<YBlockGroup>,
blocks: Y.Map<YBlock>,
environmentStartedAt: Date | null
environmentStartedAt: Date | null,
skipDependencyCheck: boolean
): YBlock[] {
const flatLayout = layout
.toArray()
Expand All @@ -438,7 +476,7 @@ export function computeDepencyQueue(

const blocksBefore = flatLayout.slice(0, blockIndex)
const blocksBeforeToRun = blocksBefore.filter((block) =>
mustExecute(block, blocks, environmentStartedAt)
mustExecute(block, blocks, environmentStartedAt, skipDependencyCheck)
)

return blocksBeforeToRun
Expand Down

0 comments on commit 7d5e326

Please sign in to comment.