Skip to content

Commit

Permalink
Merge pull request #256 from briefercloud/run-inputs-before-blocks
Browse files Browse the repository at this point in the history
Run inputs before blocks
  • Loading branch information
vieiralucas authored Nov 20, 2024
2 parents 2f78b9c + 2376824 commit a830a43
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 17 deletions.
2 changes: 2 additions & 0 deletions apps/api/src/yjs/v2/observers/blocks/date-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export class DateInputObserver implements IDateInputObserver {
if (status !== 'idle') {
block.setAttribute('status', 'idle')
}

this.executor.save(block)
}

public async handleBlockEvent(
Expand Down
2 changes: 2 additions & 0 deletions apps/api/src/yjs/v2/observers/blocks/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ export class InputObserver implements IInputObserver {
status: 'idle',
})
}

this.executor.saveValue(block)
}

public async handleBlockEvent(
Expand Down
4 changes: 3 additions & 1 deletion apps/api/src/yjs/v2/persistors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
isDateInputBlock,
getDateInputAttributes,
PivotTableBlock,
isTextInputBlock,
} from '@briefer/editor'
import { equals, omit } from 'ramda'
import { uuidv4 } from 'lib0/random.js'
Expand Down Expand Up @@ -526,7 +527,8 @@ export class AppPersistor implements Persistor {
if (!publishedBlock) {
throw new Error(`Block(${blockId}) not found in published state`)
}
if (!isInputBlock(publishedBlock)) {

if (!isTextInputBlock(publishedBlock)) {
return true
}

Expand Down
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
58 changes: 48 additions & 10 deletions packages/editor/src/blocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
getInputBlockExecutedAt,
inputRequestSaveValue,
} from './input.js'
import { YBlockGroup, switchBlockType } from '../index.js'
import { YBlockGroup, isTextInputBlock, switchBlockType } from '../index.js'
import { FileUploadBlock, duplicateFileUploadBlock } from './fileUpload.js'
import {
DropdownInputBlock,
Expand Down 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
6 changes: 3 additions & 3 deletions packages/editor/src/blocks/input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
BlockType,
YBlock,
getInputAttributes,
isInputBlock,
isTextInputBlock,
getInputValueExecStatus,
getInputVariableExecStatus,
makeInputBlock,
Expand All @@ -20,7 +20,7 @@ describe('isYInputBlock', () => {
const inputBlock = makeInputBlock('blockId', blocks)
blocks.set('blockId', inputBlock)

expect(isInputBlock(inputBlock)).toBe(true)
expect(isTextInputBlock(inputBlock)).toBe(true)
})

it('should return false for non-Input blocks', () => {
Expand All @@ -29,7 +29,7 @@ describe('isYInputBlock', () => {
const sqlBlock = makeSQLBlock('blockId', blocks)
blocks.set('blockId', sqlBlock)

expect(isInputBlock(sqlBlock)).toBe(false)
expect(isTextInputBlock(sqlBlock)).toBe(false)
})
})

Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/blocks/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export type InputBlock = BaseBlock<BlockType.Input> & {
executedAt: string | null
}

export const isInputBlock = (
export const isTextInputBlock = (
block: YBlock
): block is Y.XmlElement<InputBlock> =>
block.getAttribute('type') === BlockType.Input
Expand Down Expand Up @@ -234,7 +234,7 @@ export function getInputBlockExecStatus(
function getAvailableInputVariable(
blocks: Y.Map<YBlock>
): InputBlock['variable'] {
const inputBlocks = Array.from(blocks.values()).filter(isInputBlock)
const inputBlocks = Array.from(blocks.values()).filter(isTextInputBlock)
const vars = new Set(
inputBlocks.map((block) => block.getAttribute('variable')?.value)
)
Expand Down

0 comments on commit a830a43

Please sign in to comment.