GitHub package.json versionTypeScriptNPM
GitHub package.json versionTypeScriptNPM

Prehooks

As of v7.0.0

What are Prehooks?

Prehooks are user functions which are invoked by the React Observable Context prior to executing context state operations.

Why Prehooks?

Prehooks provide a central place for sanitizing, modifying, transforming, validating etc. all related incoming state updates. The context store obtains its prehooks via its context's optional prehooks property.
The context store administers 2 update operations, each adhering to its own user-defined prehook when present. Otherwise, the update operation proceeds normally to completion. Thus, there are 2 prehooks named resetState and setState - after the store update methods they support.
Each prehook returns a boolean value ( true to continue AND false to abort the update operation). The prehook may modify (i.e. sanitize, transform, transpose) the argument to accurately reflect the intended update value. This is done by mutating part of the argument which holds the next nextUpdate values.

What do Prehooks look like?


interface Prehook<T>{
    resetState?: (
        resetData: Partial<T>, // resetData holds nextUpdate data.
        state: {
            current: T,
            original: T
        }
    ) => boolean;
    setState?: (
        newChanges: Partial<T> // newChanges holds nextUpdate data.
    ) => boolean;
}

How are Prehooks wired up to the context store?

Method 1: At context creation

const context = createEagleEye( T|AutoImmutable<T>?, Prehooks<T>?, IStorage<T>? )

Method 2: Context.prehooks property

context.prehooks = Prehooks<T>;