-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Embeddables Rebuild] Move titles API (#179202)
Moves the titles API and state comparators away from the embeddable plugin and to the presentation-publishing package.
- Loading branch information
1 parent
f8b3d70
commit 59b1f8a
Showing
22 changed files
with
190 additions
and
207 deletions.
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
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
10 changes: 10 additions & 0 deletions
10
packages/presentation/presentation_publishing/comparators/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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export type { ComparatorFunction, ComparatorDefinition, StateComparators } from './types'; | ||
export { getInitialValuesFromComparators, runComparators } from './state_comparators'; |
44 changes: 44 additions & 0 deletions
44
packages/presentation/presentation_publishing/comparators/state_comparators.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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { StateComparators } from './types'; | ||
|
||
const defaultComparator = <T>(a: T, b: T) => a === b; | ||
|
||
export const getInitialValuesFromComparators = <StateType extends object = object>( | ||
comparators: StateComparators<StateType>, | ||
comparatorKeys: Array<keyof StateType> | ||
) => { | ||
const initialValues: Partial<StateType> = {}; | ||
for (const key of comparatorKeys) { | ||
const comparatorSubject = comparators[key][0]; // 0th element of tuple is the subject | ||
initialValues[key] = comparatorSubject?.value; | ||
} | ||
return initialValues; | ||
}; | ||
|
||
export const runComparators = <StateType extends object = object>( | ||
comparators: StateComparators<StateType>, | ||
comparatorKeys: Array<keyof StateType>, | ||
lastSavedState: StateType | undefined, | ||
latestState: Partial<StateType> | ||
) => { | ||
if (!lastSavedState) { | ||
// if we have no last saved state, everything is considered a change | ||
return latestState; | ||
} | ||
const latestChanges: Partial<StateType> = {}; | ||
for (const key of comparatorKeys) { | ||
const customComparator = comparators[key]?.[2]; // 2nd element of the tuple is the custom comparator | ||
const comparator = customComparator ?? defaultComparator; | ||
if (!comparator(lastSavedState?.[key], latestState[key], lastSavedState, latestState)) { | ||
latestChanges[key] = latestState[key]; | ||
} | ||
} | ||
return Object.keys(latestChanges).length > 0 ? latestChanges : undefined; | ||
}; |
26 changes: 26 additions & 0 deletions
26
packages/presentation/presentation_publishing/comparators/types.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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { PublishingSubject } from '../publishing_subject'; | ||
|
||
export type ComparatorFunction<StateType, KeyType extends keyof StateType> = ( | ||
last: StateType[KeyType] | undefined, | ||
current: StateType[KeyType] | undefined, | ||
lastState?: Partial<StateType>, | ||
currentState?: Partial<StateType> | ||
) => boolean; | ||
|
||
export type ComparatorDefinition<StateType, KeyType extends keyof StateType> = [ | ||
PublishingSubject<StateType[KeyType]>, | ||
(value: StateType[KeyType]) => void, | ||
ComparatorFunction<StateType, KeyType>? | ||
]; | ||
|
||
export type StateComparators<StateType> = { | ||
[KeyType in keyof StateType]: ComparatorDefinition<StateType, KeyType>; | ||
}; |
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
File renamed without changes.
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.