Plugin API - HohensteinGroup/ChangeChecker GitHub Wiki

Plugins

"Value like" plugins

Some types behave like values. For example decimal.js can be used in place of "number" but is still an object. To overwrite the default equality check for objects, you can write a "value like" plugin.

Implement this interface and add an instance of your plugin using the "withPlugin" instance method.

export interface IValueLikePlugin<T> {
    name: string;
    isMatch: (instance: any) => instance is T;
    isValueLikePlugin: true;

    clone: (context: ICloneContext, instance: T) => T;
    equals: (left: T, right: T) => boolean;

What is "ICloneContext"?

At the moment the context provides just a single function "clone(obj: T): T". This function is meant to be used to pass child objects back to the clone pipeline. This is how you can use the inner structural clone algorithm including all installed plugins.

To tell the type system that your "value like" should no longer be a DiffObject in the diff, you have to extend the "IValueLikeRegistry" interface using declaration merging.

declare module "change-checker/types/ValueLikeRegistry" {
    export interface IValueLikeRegistry {
        decimal: Decimal;
    }
}

Reference plugins

Sometimes it is important to keep getter and setter functions alive. To overwrite the default structural cloning, implement this interface

export interface IReferenceLikePlugin<T> {
    name: string;
    isMatch: (instance: any) => instance is T;
    isValueLikePlugin: false;

    clone?: (context: ICloneContext, instance: T) => T;
}