Meyda Singleton Demo

node v14.20.1
version: 1.0.0
endpointsharetweet
var meyda1 = require("meyda"); meyda1.chromaBands = 6; meyda1.chromaBands
Above, we require Meyda and set extraction parameters by changing values on the Meyda object.
var meyda2 = require("meyda"); meyda2.chromaBands
For the sake of argument, we need a second Meyda in our project; we do the same as above, but when we inspect the modified value, we find that it has the same value as above, since Meyda is a singleton.
var meyda3 = Object.assign({}, meyda1); meyda3.chromaBands = 12 await [meyda3.chromaBands, meyda1.chromaBands]
In order to actually get a second Meyda, we need to use Object.assign to actually copy it. Not great.
function MeydaConstructor() { var MeydaForConstructor = require("meyda"); Object.assign(this, MeydaForConstructor); return this; } var meydaInstance = new MeydaConstructor(); meydaInstance
A constructor like above technically would do what a constructor usually does, but is still open to the issues shown above - if the singleton has been modified by another part of our program, we can't trust that the what comes out of the constructor will have the right defaults. In fact, some dependency could also use Meyda and affect defaults for the whole program outside of it's own context. Clearly this is not desirable behavior.
Loading…

no comments

    sign in to comment