Skip to content

Switch to a plugin style approach to exposing models in CO2.js #206

@fershad

Description

@fershad

Is your feature request related to a problem? Please describe.

The aim of the suggested changes described below is to enable the addition of more carbon estimation models to CO2.js in a way that is flexible for models that are not based on data transfer (bytes) as a key input

Currently, we have a number of modules as named exports which allow access to different functions and variables within CO2.js. For example, if you wanted to perform a carbon estimate you'd import the co2 module as follows:

import { co2 } from '@tgwf/co2'

This is, however, somewhat limiting in that any new models that are introduced CO2.js would need to conform to the existing functions that are in those models. Sticking with the example of estimating emissions, a new model would need to work within the bounds of the perByte or perVisit functions. These functions take inputs of bytes and greenHosting. However, not all models use these inputs.

Describe the solution you'd like
To allow for greater flexibility as other models are added to CO2.js, a change should be made towards a plugin style system. CO2.js could set some expected defaults, as in a function or output that it expects all models to exports, and then models can be free to use whatever inputs or data they need to behind the scenes.

For example, each model plugin could be expected to export a function called estimate. This would be the default function called when that model is used. Inputs can then be passed into the function via an options objects. The model can also then expose additional functions for specific cases if need be.

Let's take the example of the Sustainable Web Design Model which is already implemented in CO2.js. It might look something like this:

// THE BELOW IS JUST AN EXAMPLE

import { SustainableWebDesign } from '@tgwf/co2/models`;

// The model can specify however many optional inputs it wants. The model should also have defaults set for all optional inputs.
const options = {
  greenHosting: true
};

// This estimates the emissions for 1000 bytes
const websiteCarbon = SustainableWebDesign.estimate(1000, options);

const optionsPerVisit = {
  bytes: 1000,
  greenHosting: true,
  firstVisitRatio: 0.6,
  returnVisitRatio: 0.4,
  dataReloadRatio: 0.3
};

// This estimates the emissions of 10 visits to a website of 1000 bytes.
const websiteCarbonPerVisit = SustainableWebDesign.perVisit(10, optionsPerVisit);

If another model were to be added (let's say for example onlineEventsModel) then it would be expected to expose a default estimate function, but then could also expose additional functions for specific cases (e.g. perVideoCall, perConference, perPodcast etc.). These additional functions might build upon the functionality of the estimate function or could be standalone.

Metadata

Metadata

Assignees

Labels

designingSpecific outcomes to address, but that we’re not committing tofunding-requiredroadmap

Type

No type

Projects

Status

Now

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions