invisible-college / statebus

All aboard the STATEBUS!!!
118 stars 5 forks source link

`require('statebus')` should not leak global variables on server #40

Closed karth295 closed 6 years ago

karth295 commented 6 years ago

On the client it's not surprising that a JS library creates global variables, but on a nodejs server I expect to use require. I'm specifically thinking of the state variable -- it should either be used as bus.state on servers or do something like const {bus, state} = require('statebus').

toomim commented 6 years ago

Good catch! The state variable should indeed only be global on the client. I see a bug with how it's being initialized, and am fixing it now.

toomim commented 6 years ago

On the server, we make a few globals: loading, clone, and forget. I'm not sure if these should be.

On the client, we also make global fetch, save, del, and state. On the server, you have to disambiguate which bus you're referring to, so you'll always have to specify the master in master.fetch(), and there's no advantage to making them global.

toomim commented 6 years ago

Here's the bugfix: https://github.com/invisible-college/statebus/commit/d917eb19d8aace94766e08e668a7cf1d60fda0fc