analytics icon indicating copy to clipboard operation
analytics copied to clipboard

Update method injection with standard API

Open DavidWells opened this issue 2 years ago • 0 comments

Custom methods have the analytics instance injected into them as last argument and into the this context if non-arrow function used. https://getanalytics.io/plugins/writing-plugins/#adding-custom-methods

TODO: Standardize this api to something like:

{
  analytics: instance,
  config, //  > current plugin config,
}

https://github.com/DavidWells/analytics/blob/master/packages/analytics-core/src/index.js#L981

 function appendArguments(fn, extra = {}) {
    return function () {
      /* Get original args */
      const args = Array.prototype.slice.call(arguments)
      /* Create clone of args */
      let newArgs = new Array(fn.length)
      for (let i = 0; i < args.length; i++) {
        newArgs[i] = args[i]
      }
      /* Append new arg to end */
      newArgs[newArgs.length] = instance
      // Set instance on extended methods
      return fn.apply({ instance, ...extra }, newArgs)
    }
  }

DavidWells avatar Jun 22 '22 18:06 DavidWells