-
Notifications
You must be signed in to change notification settings - Fork 426
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
feat: implementation for events in history #7569
Draft
pedrobonamin
wants to merge
9
commits into
next
Choose a base branch
from
events-api-studio
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f0f4317
feat(core): wip implementation for getDocumentEvents
pedrobonamin 1a6fd7f
fix(core): update versionRevisionId when creating unpublish document …
pedrobonamin 938804f
feat(core): add events into timeline
pedrobonamin 94b0fa8
chore(core): move events story to structure
pedrobonamin 70e943c
chore(core): export DocumentVersionEventType type from core
pedrobonamin 02ae874
fix(core): update ts definition for getDocumentEvents
pedrobonamin 16ec450
fix(structure): update DocumentGroup story
pedrobonamin 49d773c
fix(core): update exports definitions
pedrobonamin 9ab3d53
fix(core): update document events tests to vitest
pedrobonamin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -194,7 +194,11 @@ export class Timeline { | |
const nextTransactionToChunk = this._chunks.length > 0 ? this._chunks.last.end : firstIdx | ||
for (let idx = nextTransactionToChunk; idx <= lastIdx; idx++) { | ||
const transaction = this._transactions.get(idx) | ||
this._chunks.mergeAtEnd(chunkFromTransaction(transaction), mergeChunk) | ||
const allTransactions = this._transactions.getAllItems | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To generate the event, we need the previous events, because some of them depend on checks like:
All of this will be more easy to read going to the |
||
this._chunks.mergeAtEnd( | ||
chunkFromTransaction(this.publishedId, transaction, allTransactions), | ||
mergeChunk, | ||
) | ||
} | ||
|
||
// Add transactions at the beginning: | ||
|
@@ -204,7 +208,12 @@ export class Timeline { | |
|
||
for (let idx = firstTransactionChunked - 1; idx >= firstIdx; idx--) { | ||
const transaction = this._transactions.get(idx) | ||
this._chunks.mergeAtBeginning(chunkFromTransaction(transaction), mergeChunk) | ||
const allTransactions = this._transactions.getAllItems | ||
|
||
this._chunks.mergeAtBeginning( | ||
chunkFromTransaction(this.publishedId, transaction, allTransactions), | ||
mergeChunk, | ||
) | ||
} | ||
} | ||
|
||
|
@@ -216,12 +225,14 @@ export class Timeline { | |
|
||
private _createInitialChunk() { | ||
if (this.reachedEarliestEntry) { | ||
if (this._chunks.first?.type === 'initial') return | ||
if (this._chunks.first?.event.type === 'document.createVersion') return | ||
|
||
const firstTx = this._transactions.first | ||
if (!firstTx) return | ||
const initialChunk = chunkFromTransaction(firstTx) | ||
initialChunk.type = 'initial' | ||
const allTransactions = this._transactions.getAllItems | ||
|
||
const initialChunk = chunkFromTransaction(this.publishedId, firstTx, allTransactions) | ||
initialChunk.event.type = 'document.createVersion' | ||
initialChunk.id = '@initial' | ||
initialChunk.end = initialChunk.start | ||
this._chunks.addToBeginning(initialChunk) | ||
|
@@ -275,7 +286,7 @@ export class Timeline { | |
chunkIdx-- | ||
) { | ||
const currentChunk = this._chunks.get(chunkIdx) | ||
if (currentChunk.type === 'publish' || currentChunk.type === 'initial') { | ||
if (currentChunk.event.type === 'document.publishVersion' || currentChunk.id === '@initial') { | ||
return currentChunk | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 33 additions & 125 deletions
158
packages/sanity/src/core/store/_legacy/history/history/chunker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,160 +1,68 @@ | ||
/* eslint-disable no-nested-ternary */ | ||
import {type MendozaEffectPair, type MendozaPatch} from '@sanity/types' | ||
|
||
import {type Chunk, type ChunkType} from '../../../../field' | ||
import {type Chunk} from '../../../../field' | ||
import {getEventFromTransaction} from '../../../events/getDocumentEvents' | ||
import {type EditDocumentVersionEvent} from '../../../events/types' | ||
import {type Transaction} from './types' | ||
|
||
function canMergeEdit(type: ChunkType) { | ||
return type === 'create' || type === 'editDraft' | ||
} | ||
|
||
const CHUNK_WINDOW = 5 * 60 * 1000 // 5 minutes | ||
|
||
function isWithinMergeWindow(a: string, b: string) { | ||
return Date.parse(b) - Date.parse(a) < CHUNK_WINDOW | ||
} | ||
|
||
export function mergeChunk(left: Chunk, right: Chunk): Chunk | [Chunk, Chunk] { | ||
if (left.end !== right.start) throw new Error('chunks are not next to each other') | ||
|
||
// TODO: How to detect first squash/create | ||
|
||
const draftState = combineState(left.draftState, right.draftState) | ||
const publishedState = combineState(left.publishedState, right.publishedState) | ||
|
||
if (left.type === 'delete' && right.type === 'editDraft') { | ||
return [left, {...right, type: 'create', draftState, publishedState}] | ||
} | ||
|
||
// Convert deletes into either discardDraft or unpublish depending on what's been deleted. | ||
if (right.type === 'delete') { | ||
if (draftState === 'missing' && publishedState === 'present') { | ||
return [left, {...right, type: 'discardDraft', draftState, publishedState}] | ||
} | ||
|
||
if (draftState === 'present' && publishedState === 'missing') { | ||
return [left, {...right, type: 'unpublish', draftState, publishedState}] | ||
} | ||
const mergeEvents = ( | ||
leftEvent: EditDocumentVersionEvent, | ||
rightEvent: EditDocumentVersionEvent, | ||
): EditDocumentVersionEvent => { | ||
const mergedEvents = leftEvent.mergedEvents || [] | ||
delete leftEvent.mergedEvents | ||
return { | ||
...rightEvent, | ||
mergedEvents: [...mergedEvents, leftEvent], | ||
} | ||
} | ||
|
||
/** | ||
* @internal | ||
* Decides whether to merge two chunks or not according to their type and timestamp | ||
*/ | ||
export function mergeChunk(left: Chunk, right: Chunk): Chunk | [Chunk, Chunk] { | ||
if (left.end !== right.start) throw new Error('chunks are not next to each other') | ||
if ( | ||
canMergeEdit(left.type) && | ||
right.type === 'editDraft' && | ||
left.event.type === 'document.editVersion' && | ||
right.event.type === 'document.editVersion' && | ||
isWithinMergeWindow(left.endTimestamp, right.startTimestamp) | ||
) { | ||
const authors = new Set<string>() | ||
for (const author of left.authors) authors.add(author) | ||
for (const author of right.authors) authors.add(author) | ||
|
||
return { | ||
index: 0, | ||
id: right.id, | ||
type: left.type, | ||
start: left.start, | ||
end: right.end, | ||
event: mergeEvents(left.event, right.event), | ||
startTimestamp: left.startTimestamp, | ||
endTimestamp: right.endTimestamp, | ||
authors, | ||
draftState, | ||
publishedState, | ||
} | ||
} | ||
|
||
return [left, {...right, draftState, publishedState}] | ||
} | ||
|
||
type ChunkState = 'unedited' | 'deleted' | 'upsert' | ||
function getChunkState(effect?: MendozaEffectPair): ChunkState { | ||
const modified = Boolean(effect) | ||
const deleted = effect && isDeletePatch(effect?.apply) | ||
|
||
if (deleted) { | ||
return 'deleted' | ||
} | ||
|
||
if (modified) { | ||
return 'upsert' | ||
} | ||
|
||
return 'unedited' | ||
return [left, right] | ||
} | ||
|
||
/* | ||
* getChunkType tries to determine what effect the given transaction had on the document | ||
* More information about the logic can be found here https://github.com/sanity-io/sanity/pull/2633#issuecomment-886461812 | ||
* | ||
* | | draft unedited | draft deleted | draft upsert | | ||
* |--------------------|----------------|---------------|--------------| | ||
* | published unedited | X | delete | editDraft | | ||
* | published deleted | delete | delete | delete | | ||
* | published upsert | liveEdit | publish | liveEdit | | ||
/** | ||
* @internal | ||
* Creates a chunk for the timeline from a transaction. | ||
*/ | ||
function getChunkType(transaction: Transaction): ChunkType { | ||
const draftState = getChunkState(transaction.draftEffect) | ||
const publishedState = getChunkState(transaction.publishedEffect) | ||
|
||
if (publishedState === 'unedited') { | ||
if (draftState === 'deleted') { | ||
return 'delete' | ||
} | ||
|
||
if (draftState === 'upsert') { | ||
return 'editDraft' | ||
} | ||
} | ||
|
||
if (publishedState === 'deleted') { | ||
return 'delete' | ||
} | ||
|
||
if (publishedState === 'upsert') { | ||
if (draftState === 'unedited') { | ||
return 'editLive' | ||
} | ||
|
||
if (draftState === 'deleted') { | ||
return 'publish' | ||
} | ||
|
||
if (draftState === 'upsert') { | ||
return 'editLive' | ||
} | ||
} | ||
|
||
return 'editLive' | ||
} | ||
|
||
export function chunkFromTransaction(transaction: Transaction): Chunk { | ||
const modifiedDraft = Boolean(transaction.draftEffect) | ||
const modifiedPublished = Boolean(transaction.publishedEffect) | ||
|
||
const draftDeleted = transaction.draftEffect && isDeletePatch(transaction.draftEffect.apply) | ||
const publishedDeleted = | ||
transaction.publishedEffect && isDeletePatch(transaction.publishedEffect.apply) | ||
|
||
const type = getChunkType(transaction) | ||
|
||
export function chunkFromTransaction( | ||
publishedId: string, | ||
transaction: Transaction, | ||
transactions: Transaction[], | ||
): Chunk { | ||
const previousTransactions = transactions.filter((tx) => tx.index < transaction.index).reverse() | ||
return { | ||
index: 0, | ||
id: transaction.id, | ||
type, | ||
start: transaction.index, | ||
end: transaction.index + 1, | ||
startTimestamp: transaction.timestamp, | ||
endTimestamp: transaction.timestamp, | ||
authors: new Set([transaction.author]), | ||
draftState: modifiedDraft ? (draftDeleted ? 'missing' : 'present') : 'unknown', | ||
publishedState: modifiedPublished ? (publishedDeleted ? 'missing' : 'present') : 'unknown', | ||
event: getEventFromTransaction(publishedId, transaction, previousTransactions), | ||
} | ||
} | ||
|
||
function combineState( | ||
left: 'present' | 'missing' | 'unknown', | ||
right: 'present' | 'missing' | 'unknown', | ||
) { | ||
return right === 'unknown' ? left : right | ||
} | ||
|
||
export function isDeletePatch(patch: MendozaPatch): boolean { | ||
return patch[0] === 0 && patch[1] === null | ||
} |
1 change: 1 addition & 0 deletions
1
packages/sanity/src/core/store/_legacy/history/history/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './chunker' | ||
export * from './Timeline' | ||
export * from './TimelineController' | ||
export * from './types' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is replaced by the event types, that will be part of the
event.chunk.type