Jump to content

Help WIth Obscure Javascript (Event Emitters & Computed Variables)

TheNuzziNuzz
Go to solution Solved by TheNuzziNuzz,

The solution was to use Proxies on all the objects call by my "computed" functions. Initially set the computed function to a global variable, execute the function, and within the "get" proxy save a copy of function via the global variable so whenever "set" is called so is the original computed function. This way any variables used in the computed function will cause the computed function to re-run.

Would anyone have any idea how I could re-create the functionality of Vue.JS's computed variables. Basically create an event emitter function that fires whenever one of the variables used in that function updates?

Computers r fun

Link to comment
Share on other sites

Link to post
Share on other sites

if you store your variables in an object you can use a proxy to intercept the change before it happens.
 

const _config = {
  somevar: 1
}

const config = new Proxy(
  _config,
  {
    set: function(obj, prop, value) {
      console.log(prop, 'was updated')
    }
  });

config.somevar = 2; // prints "somevar was updated"

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

The solution was to use Proxies on all the objects call by my "computed" functions. Initially set the computed function to a global variable, execute the function, and within the "get" proxy save a copy of function via the global variable so whenever "set" is called so is the original computed function. This way any variables used in the computed function will cause the computed function to re-run.

Computers r fun

Link to comment
Share on other sites

Link to post
Share on other sites

You also get the play with proxies :P mmy new favourite toy at work other than neural networks

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×