9.0.10076 • Published 4 years ago

@acoustic-content-sdk/redux-utils v9.0.10076

Weekly downloads
1
License
MIT
Repository
-
Last release
4 years ago

npm

Implementation of utility functions that help working with redux state.

Immutability

One of the core principals of the redux pattern is to keep all state immutable. This implies that when updating a value we need to make a copy of the affected objects. The following functions help to limit the size of these copies.

Updater

The createUpdater creates an wrapper around an object. This wrapper allows to set or remove (deep) properties of the wrapped object without mutating it. The updater makes sure to only create the minimum set of shallow copies.

Example:

const myObject={...};
const updater = createUpdater(myObject);

updater.set('elements.text.value', 'newValue');

const newObject = updater.get();

The updater works similar to immer but without the overhead.

Invariance Checks

During development it is important to verify that objects are not getting mutated. Use the invariance checker to test this, but make sure to not include these checks in production builds for performance reasons.

Example:

const inv = invarianceChecker(myObject);
// do some operations

// validate that myObject has not been mutated
assert(inv());

API Documentation

API Documentation

Home > @acoustic-content-sdk/redux-utils

redux-utils package

Implementation of utility functions that help working with redux state.

Functions

FunctionDescription
createBreadcrumb(aId, aSelParent, aSelId)Constructs a breadcrumb trail starting at a particular ID
createUpdater(aValue, aLogSvc)Constructs a function that updates one or more values in a json object.
equalsByRevision(aLeft, aRight)
getNavSelectors(aJson$, aLogSvc)Returns selectors that access parent and child nodes from a navigation structure
invarianceChecker(aValue)Creates a function that validates that an object has not changed. Note that this should only be used for debugging purposes, since it makes a deep copy of the value to test for the invariance
isDraftId(aAuthoringId)Tests if the authoring ID has a draft suffix
isNotFromDataBase(aValue)Tests if a value is not from a database
isNotRoot(aId)
markAsFromDataBase(aValue)Adds a key to the item marking it as coming from the local data base
removeArrayItemByAccessor(aAccessor, aItem, aUser)
removeDataBaseMarker(aValue)Removes the database marker from the object
removeRecord()Creates a reducer that removes a key from a record
rxElementType(aTypeResolver, aLogSvc, aScheduler)Resolves the element type, either directly from the element or from the authoring type
rxLogDiff(aLogger)
rxResolveAuthoringItems(idOrItem, resolveItem, resolveAsset, aLogSvc, scheduler)Resolves the item and all referenced items and assets
selectByDeliveryId(aId)Constructs a selector that validates that the ID is a delivery ID (not a draft ID) and selects based on that ID from the state
serializeDiff(aOld, aNew, aSameStyle, aNewStyle, aDeletedStyle)Serializes the differences between two JSON objects
sortAuthoringItems(aResult)Performs a topological sort on a set of resolved items
updateGenericProperties(aItem, aUser)Updates properties of the item that depend on the environment
updateImageElement(aAccessor, aItem, aAsset)
updateRecord(aKey, aPredicate)Creates a reducer that adds an item to a record
updateSingleItem(aPredicate)
updateValueByAccessor(aAccessor, aValue, aItem, aUser)Updates a single property based on the accessor expression.

Interfaces

InterfaceDescription
ItemWithId
ItemWithLinkedDocId
ItemWithRevision
NavigationJson
NavSelectorsInterface that exposes selectors to the navigation structure
ResolvedNode
UpdaterFacade that offers the modification functions for a json object. The original JSON object will not be modified and the modified object will only contain the minimum number of modifications.

Variables

VariableDescription
addToSetEpicConstructs an epic that convers an ADD action to a SET action
cloneAuthoringItemsBasically a re-export of the clone functionality to have it all clean inside this one function
DB_KEY
DB_VALUE
ensureDraftIdMakes sure to end the ID with a draft suffix
getDeliveryIdReturns the delivery ID from an authoring ID, i.e. strips off the ':draft' suffix from the ID
getDeliveryIdFromAuthoringItemReturns the delivery ID from an authoring item.
getValueByAccessorRetrieves a property value by accessor
keyByIdExtract the delivery ID of the draft
ROOT_ID
selectClassificationExtracts the classification property
updateItemsWithRevision

Type Aliases

Type AliasDescription
AddRecordReducerReducer function that adds an item to a record
AuthoringItem
ItemReducerReducer function that updates an item
RemoveRecordReducerReducer function that adds an item to a record
ResolutionResult
ResolveAuthoringAsset
ResolveAuthoringContentItem

Home > @acoustic-content-sdk/redux-utils > createBreadcrumb

createBreadcrumb() function

Constructs a breadcrumb trail starting at a particular ID

Signature:

export declare function createBreadcrumb<T>(aId: string, aSelParent: UnaryFunction<string, Observable<T>>, aSelId: UnaryFunction<T, string>): Observable<T[]>;

Parameters

ParameterTypeDescription
aIdstringthe start ID
aSelParentUnaryFunction<string, Observable<T>>selector function for the parent node
aSelIdUnaryFunction<T, string>selector function for the ID from a parent node

Returns:

Observable<T[]>

the breadcrumb trail

Home > @acoustic-content-sdk/redux-utils > createUpdater

createUpdater() function

Constructs a function that updates one or more values in a json object.

Signature:

export declare function createUpdater<T>(aValue: T, aLogSvc?: LoggerService): Updater<T>;

Parameters

ParameterTypeDescription
aValueTthe JSON structure to update
aLogSvcLoggerService

Returns:

Updater<T>

updater functions

Home > @acoustic-content-sdk/redux-utils > equalsByRevision

equalsByRevision() function

Signature:

export declare function equalsByRevision(aLeft: ItemWithRevision, aRight: ItemWithRevision): boolean;

Parameters

ParameterTypeDescription
aLeftItemWithRevision
aRightItemWithRevision

Returns:

boolean

Home > @acoustic-content-sdk/redux-utils > getNavSelectors

getNavSelectors() function

Returns selectors that access parent and child nodes from a navigation structure

Signature:

export declare function getNavSelectors(aJson$: Observable<NavigationJson>, aLogSvc?: LoggerService): NavSelectors;

Parameters

ParameterTypeDescription
aJson$Observable<NavigationJson>the JSON that represents the navigation
aLogSvcLoggerServiceoptional logger service

Returns:

NavSelectors

the selectors

Home > @acoustic-content-sdk/redux-utils > invarianceChecker

invarianceChecker() function

Creates a function that validates that an object has not changed. Note that this should only be used for debugging purposes, since it makes a deep copy of the value to test for the invariance

Signature:

export declare function invarianceChecker(aValue: any): Generator<boolean>;

Parameters

ParameterTypeDescription
aValueanythe value to test

Returns:

Generator<boolean>

the validator

Home > @acoustic-content-sdk/redux-utils > isDraftId

isDraftId() function

Tests if the authoring ID has a draft suffix

Signature:

export declare function isDraftId(aAuthoringId: string): boolean;

Parameters

ParameterTypeDescription
aAuthoringIdstringthe ID

Returns:

boolean

true if we have a draft suffix, else false

Home > @acoustic-content-sdk/redux-utils > isNotFromDataBase

isNotFromDataBase() function

Tests if a value is not from a database

Signature:

export declare function isNotFromDataBase(aValue: any): boolean;

Parameters

ParameterTypeDescription
aValueanythe value

Returns:

boolean

true if the value exists and if it is not from the database, else false

Home > @acoustic-content-sdk/redux-utils > isNotRoot

isNotRoot() function

Signature:

export declare function isNotRoot(aId: string): boolean;

Parameters

ParameterTypeDescription
aIdstring

Returns:

boolean

Home > @acoustic-content-sdk/redux-utils > markAsFromDataBase

markAsFromDataBase() function

Adds a key to the item marking it as coming from the local data base

Signature:

export declare function markAsFromDataBase<T>(aValue: T): T;

Parameters

ParameterTypeDescription
aValueTthe value to work with

Returns:

T

a value with the marker

Home > @acoustic-content-sdk/redux-utils > removeArrayItemByAccessor

removeArrayItemByAccessor() function

Signature:

export declare function removeArrayItemByAccessor<T extends BaseAuthoringItem>(aAccessor: AccessorType, aItem: T, aUser?: User): Updater<T>;

Parameters

ParameterTypeDescription
aAccessorAccessorType
aItemT
aUserUser

Returns:

Updater<T>

Home > @acoustic-content-sdk/redux-utils > removeDataBaseMarker

removeDataBaseMarker() function

Removes the database marker from the object

Signature:

export declare function removeDataBaseMarker<T>(aValue: T): T;

Parameters

ParameterTypeDescription
aValueTthe value to work with

Returns:

T

a value without the marker

Home > @acoustic-content-sdk/redux-utils > removeRecord

removeRecord() function

Creates a reducer that removes a key from a record

Signature:

export declare function removeRecord<T>(): RemoveRecordReducer<T>;

Returns:

RemoveRecordReducer<T>

the reducer

Home > @acoustic-content-sdk/redux-utils > rxElementType

rxElementType() function

Resolves the element type, either directly from the element or from the authoring type

Signature:

export declare function rxElementType(aTypeResolver: UnaryFunction<string, Observable<AuthoringType>>, aLogSvc?: LoggerService, aScheduler?: SchedulerLike): UnaryFunction<AccessorType, OperatorFunction<RenderingContext, ELEMENT_TYPE>>;

Parameters

ParameterTypeDescription
aTypeResolverUnaryFunction<string, Observable<AuthoringType>>
aLogSvcLoggerServicelogger service
aSchedulerSchedulerLikeoptional scheduler

Returns:

UnaryFunction<AccessorType, OperatorFunction<RenderingContext, ELEMENT_TYPE>>

the resolved type or undefined if the type could not be determined

Home > @acoustic-content-sdk/redux-utils > rxLogDiff

rxLogDiff() function

Signature:

export declare function rxLogDiff<T>(aLogger: Logger): MonoTypeOperatorFunction<T>;

Parameters

ParameterTypeDescription
aLoggerLogger

Returns:

MonoTypeOperatorFunction<T>

Home > @acoustic-content-sdk/redux-utils > rxResolveAuthoringItems

rxResolveAuthoringItems() function

Resolves the item and all referenced items and assets

Signature:

export declare function rxResolveAuthoringItems(idOrItem: IdOrItem, resolveItem: ResolveAuthoringContentItem, resolveAsset: ResolveAuthoringAsset, aLogSvc?: LoggerService, scheduler?: SchedulerLike): Observable<ResolutionResult>;

Parameters

ParameterTypeDescription
idOrItemIdOrItem
resolveItemResolveAuthoringContentItemcallback to resolve a content item
resolveAssetResolveAuthoringAssetcallback to resolve an asset
aLogSvcLoggerService
schedulerSchedulerLikescheduler for recursive calls

Returns:

Observable<ResolutionResult>

the observable containing the resolution result

Home > @acoustic-content-sdk/redux-utils > selectByDeliveryId

selectByDeliveryId() function

Constructs a selector that validates that the ID is a delivery ID (not a draft ID) and selects based on that ID from the state

Signature:

export declare function selectByDeliveryId<T>(aId?: string): UnaryFunction<Record<string, T>, T>;

Parameters

ParameterTypeDescription
aIdstringthe ID

Returns:

UnaryFunction<Record<string, T>, T>

a selector for that ID based on some state

Home > @acoustic-content-sdk/redux-utils > serializeDiff

serializeDiff() function

Serializes the differences between two JSON objects

Signature:

export declare function serializeDiff(aOld: any, aNew: any, aSameStyle?: UnaryFunction<string, string>, aNewStyle?: UnaryFunction<string, string>, aDeletedStyle?: UnaryFunction<string, string>): string;

Parameters

ParameterTypeDescription
aOldanyold object
aNewanynew objects
aSameStyleUnaryFunction<string, string>styling callback for identical style
aNewStyleUnaryFunction<string, string>styling callback for new style
aDeletedStyleUnaryFunction<string, string>styling callback for deleted style

Returns:

string

the serialized string

Home > @acoustic-content-sdk/redux-utils > sortAuthoringItems

sortAuthoringItems() function

Performs a topological sort on a set of resolved items

Signature:

export declare function sortAuthoringItems(aResult: ResolutionResult): AuthoringItem[];

Parameters

ParameterTypeDescription
aResultResolutionResultthe resolution result, basically a graph of nodes

Returns:

AuthoringItem[]

the items in toplogical order, i.e. parents before children

Home > @acoustic-content-sdk/redux-utils > updateGenericProperties

updateGenericProperties() function

Updates properties of the item that depend on the environment

Signature:

export declare function updateGenericProperties<T extends BaseAuthoringItem>(aItem: Updater<T>, aUser?: User): Updater<T>;

Parameters

ParameterTypeDescription
aItemUpdater<T>updater for the item
aUserUseroptionally the current user

Returns:

Updater<T>

the updater after the item has been modified

Home > @acoustic-content-sdk/redux-utils > updateImageElement

updateImageElement() function

Signature:

export declare function updateImageElement(aAccessor: AccessorType, aItem: Updater<AuthoringContentItem>, aAsset: AuthoringAsset): Updater<AuthoringContentItem>;

Parameters

ParameterTypeDescription
aAccessorAccessorType
aItemUpdater<AuthoringContentItem>
aAssetAuthoringAsset

Returns:

Updater<AuthoringContentItem>

Home > @acoustic-content-sdk/redux-utils > updateRecord

updateRecord() function

Creates a reducer that adds an item to a record

Signature:

export declare function updateRecord<T>(aKey: UnaryFunction<T, string>, aPredicate?: EqualsPredicate<T>): AddRecordReducer<T>;

Parameters

ParameterTypeDescription
aKeyUnaryFunction<T, string>function to extract the key from the item
aPredicateEqualsPredicate<T>predicate to check if two items are equal

Returns:

AddRecordReducer<T>

the reducer

Home > @acoustic-content-sdk/redux-utils > updateSingleItem

updateSingleItem() function

Signature:

export declare function updateSingleItem<T>(aPredicate?: EqualsPredicate<T>): ItemReducer<T>;

Parameters

ParameterTypeDescription
aPredicateEqualsPredicate<T>

Returns:

ItemReducer<T>

Home > @acoustic-content-sdk/redux-utils > updateValueByAccessor

updateValueByAccessor() function

Updates a single property based on the accessor expression.

Signature:

export declare function updateValueByAccessor<T extends BaseAuthoringItem>(aAccessor: AccessorType, aValue: any, aItem: T, aUser?: User): Updater<T>;

Parameters

ParameterTypeDescription
aAccessorAccessorTypethe accessor expression that points
aValueanythe new value
aItemTthe item to update
aUserUser

Returns:

Updater<T>

a copy of the item with the modified value

Home > @acoustic-content-sdk/redux-utils > ItemWithId

ItemWithId interface

Signature:

export interface ItemWithId 

Properties

PropertyTypeDescription
idstring

Home > @acoustic-content-sdk/redux-utils > ItemWithLinkedDocId

ItemWithLinkedDocId interface

Signature:

export interface ItemWithLinkedDocId 

Properties

PropertyTypeDescription
idstring
linkedDocIdstring

Home > @acoustic-content-sdk/redux-utils > ItemWithRevision

ItemWithRevision interface

Signature:

export interface ItemWithRevision extends ItemWithId 

Properties

PropertyTypeDescription
revstring

Home > @acoustic-content-sdk/redux-utils > NavigationJson

NavigationJson interface

Signature:

export interface NavigationJson 

Properties

PropertyTypeDescription
childrenNavigationJson[]
idstring

Home > @acoustic-content-sdk/redux-utils > NavSelectors

NavSelectors interface

Interface that exposes selectors to the navigation structure

Signature:

export interface NavSelectors 

Properties

PropertyTypeDescription
rootstringID of the (virtual) root item of the navigation
selectChildrenUnaryFunction<string, Observable<string[]>>Selects the children of a particular item
selectParentUnaryFunction<string, Observable<string>>Selects the parent of a particular item

Home > @acoustic-content-sdk/redux-utils > ResolvedNode

ResolvedNode interface

Signature:

export interface ResolvedNode<T> 

Properties

PropertyTypeDescription
idstring
itemT
parentIdstring

Home > @acoustic-content-sdk/redux-utils > Updater

Updater interface

Facade that offers the modification functions for a json object. The original JSON object will not be modified and the modified object will only contain the minimum number of modifications.

Signature:

export interface Updater<T> 

Properties

PropertyTypeDescription
addBiFunction<string, any, T>Inserts a new value into an array pointed to by the accessor. All values across the parent path will be cloned (shallow) if they do not have a clone, yet.Pass undefined as the new value to delete the value.Returns the modified version of the top level object.
delUnaryFunction<string, T>Removes the value pointed to by the accessor. All values across the parent path will be cloned (shallow) if they do not have a clone, yet.Returns the modified version of the top level object.
getGenerator<T>Returns the top level, modified object
setBiFunction<string, any, T>Replaces the value pointed to by the accessor with a new value. All values across the parent path will be cloned (shallow) if they do not have a clone, yet.Pass undefined as the new value to delete the value.Returns the modified version of the top level object.

Home > @acoustic-content-sdk/redux-utils > addToSetEpic

addToSetEpic variable

Constructs an epic that convers an ADD action to a SET action

Signature:

addToSetEpic: <T>(aAddAction: string, aSetAction: string) => Epic<any, any, any, any>

Home > @acoustic-content-sdk/redux-utils > cloneAuthoringItems

cloneAuthoringItems variable

Basically a re-export of the clone functionality to have it all clean inside this one function

Signature:

cloneAuthoringItems: UnaryFunction<AuthoringItem[], AuthoringItem[]>

Home > @acoustic-content-sdk/redux-utils > DB_KEY

DB_KEY variable

Signature:

DB_KEY = "ecec4405-7fb1-4b86-8d27-7338eea45683"

Home > @acoustic-content-sdk/redux-utils > DB_VALUE

DB_VALUE variable

Signature:

DB_VALUE = "e3257dbc-1ca8-4d57-94a2-37934960f39f"

Home > @acoustic-content-sdk/redux-utils > ensureDraftId

ensureDraftId variable

Makes sure to end the ID with a draft suffix

Signature:

ensureDraftId: (aAuthoringId: string) => string

Home > @acoustic-content-sdk/redux-utils > getDeliveryId

getDeliveryId variable

Returns the delivery ID from an authoring ID, i.e. strips off the ':draft' suffix from the ID

Signature:

getDeliveryId: UnaryFunction<string, string>

Home > @acoustic-content-sdk/redux-utils > getDeliveryIdFromAuthoringItem

getDeliveryIdFromAuthoringItem variable

Returns the delivery ID from an authoring item.

Signature:

getDeliveryIdFromAuthoringItem: UnaryFunction<ItemWithLinkedDocId | BaseAuthoringItem, string>

Home > @acoustic-content-sdk/redux-utils > getValueByAccessor

getValueByAccessor variable

Retrieves a property value by accessor

Signature:

getValueByAccessor: <T>(aItem: BaseAuthoringItem, aAccessor: string) => T

Home > @acoustic-content-sdk/redux-utils > keyById

keyById variable

Extract the delivery ID of the draft

Signature:

keyById: UnaryFunction<ItemWithLinkedDocId, string>

Home > @acoustic-content-sdk/redux-utils > ROOT_ID

ROOT_ID variable

Signature:

ROOT_ID: string

Home > @acoustic-content-sdk/redux-utils > selectClassification

selectClassification variable

Extracts the classification property

Signature:

selectClassification: UnaryFunction<BaseAuthoringItem | ContentItem, string>

Home > @acoustic-content-sdk/redux-utils > updateItemsWithRevision

updateItemsWithRevision variable

Signature:

updateItemsWithRevision: <T extends ItemWithRevision>(aState: Record<string, T>, aItem: T) => Record<string, T>

Home > @acoustic-content-sdk/redux-utils > AddRecordReducer

AddRecordReducer type

Reducer function that adds an item to a record

Signature:

export declare type AddRecordReducer<T> = BiFunction<Record<string, T>, T, Record<string, T>>;

Home > @acoustic-content-sdk/redux-utils > AuthoringItem

AuthoringItem type

Signature:

export declare type AuthoringItem = AuthoringContentItem | AuthoringAsset;

Home > @acoustic-content-sdk/redux-utils > ItemReducer

ItemReducer type

Reducer function that updates an item

Signature:

export declare type ItemReducer<T> = BiFunction<T, T, T>;

Home > @acoustic-content-sdk/redux-utils > RemoveRecordReducer

RemoveRecordReducer type

Reducer function that adds an item to a record

Signature:

export declare type RemoveRecordReducer<T> = BiFunction<Record<string, T>, string, Record<string, T>>;

Home > @acoustic-content-sdk/redux-utils > ResolutionResult

ResolutionResult type

Signature:

export declare type ResolutionResult = Record<string, ResolvedNode<AuthoringItem>>;

Home > @acoustic-content-sdk/redux-utils > ResolveAuthoringAsset

ResolveAuthoringAsset type

Signature:

export declare type ResolveAuthoringAsset = UnaryFunction<string, Observable<AuthoringAsset>>;

Home > @acoustic-content-sdk/redux-utils > ResolveAuthoringContentItem

ResolveAuthoringContentItem type

Signature:

export declare type ResolveAuthoringContentItem = UnaryFunction<string, Observable<AuthoringContentItem>>;

Home > @acoustic-content-sdk/redux-utils > ItemWithId > id

ItemWithId.id property

Signature:

id?: string;

Home > @acoustic-content-sdk/redux-utils > NavigationJson > children

NavigationJson.children property

Signature:

children?: NavigationJson[];

Home > @acoustic-content-sdk/redux-utils > NavigationJson > id

NavigationJson.id property

Signature:

id: string;

Home > @acoustic-content-sdk/redux-utils > ItemWithRevision > rev

ItemWithRevision.rev property

Signature:

rev?: string;

Home > @acoustic-content-sdk/redux-utils > ItemWithLinkedDocId > id

ItemWithLinkedDocId.id property

Signature:

readonly id?: string;

Home > @acoustic-content-sdk/redux-utils > ItemWithLinkedDocId > linkedDocId

ItemWithLinkedDocId.linkedDocId property

Signature:

readonly linkedDocId?: string;

Home > @acoustic-content-sdk/redux-utils > ResolvedNode > id

ResolvedNode.id property

Signature:

id: string;

Home > @acoustic-content-sdk/redux-utils > ResolvedNode > item

ResolvedNode.item property

Signature:

item: T;

Home > @acoustic-content-sdk/redux-utils > ResolvedNode > parentId

ResolvedNode.parentId property

Signature:

parentId?: string;

Home > @acoustic-content-sdk/redux-utils > NavSelectors > root

NavSelectors.root property

ID of the (virtual) root item of the navigation

Signature:

root: string;

Home > @acoustic-content-sdk/redux-utils > NavSelectors > selectChildren

NavSelectors.selectChildren property

Selects the children of a particular item

Signature:

selectChildren: UnaryFunction<string, Observable<string[]>>;

Home > @acoustic-content-sdk/redux-utils > NavSelectors > selectParent

NavSelectors.selectParent property

Selects the parent of a particular item

Signature:

selectParent: UnaryFunction<string, Observable<string>>;

Home > @acoustic-content-sdk/redux-utils > Updater > add

Updater.add property

Inserts a new value into an array pointed to by the accessor. All values across the parent path will be cloned (shallow) if they do not have a clone, yet.

Pass undefined as the new value to delete the value.

Returns the modified version of the top level object.

Signature:

add: BiFunction<string, any, T>;

Home > @acoustic-content-sdk/redux-utils > Updater > del

Updater.del property

Removes the value pointed to by the accessor. All values across the parent path will be cloned (shallow) if they do not have a clone, yet.

Returns the modified version of the top level object.

Signature:

del: UnaryFunction<string, T>;

Home > @acoustic-content-sdk/redux-utils > Updater > get

Updater.get property

Returns the top level, modified object

Signature:

get: Generator<T>;

Home > @acoustic-content-sdk/redux-utils > Updater > set

Updater.set property

Replaces the value pointed to by the accessor with a new value. All values across the parent path will be cloned (shallow) if they do not have a clone, yet.

Pass undefined as the new value to delete the value.

Returns the modified version of the top level object.

Signature:

set: BiFunction<string, any, T>;