Major improvements made to monetize-npm-cli 🔥
Tags: gftwhackathon, node, webdev, javascript
Published: 2020-06-14 21:23:54

You can read the original post I wrote here

{% link https://dev.to/projectescape/web-monetization-for-npm-packages-3g6o %}

***************
### Note
This post is being written at the time of

1. `v0.0.6` of `monetize-npm-cli`
1. `v0.0.7` of `wrapper-coil-extension`

And includes changes since the original post was written (June 5). 

****************
# Improvements !!
## wrapper-coil-extension

### - Switched to probabilistic revenue sharing

Looping through all the wallets has been replaced by Probabilistic revenue sharing. Payment pointer is now being changed dynamically instead of opening new tab and closing the previous, which also fixed chromium stealing the focus.

### - No Focus stealing

### - Restricted opening more tabs while content is being monetized

User now cannot open more tabs in the puppeteer chromium instance, thus packages will keep getting monetized. 

### - Events are being fired and states are now being changed as expected

Event listeners are now being fired correctly along with the same arguments that you would expect to get in the browser. State is also being updated correctly.

### - Depreciated Timeout

Since probabilistic revenue sharing is being implemented, timeout is no longer needed. Packages are now selected for 65 seconds each randomly.

**************
## monetize-npm-cli

### - `addListener`, `invokeListener` and `removeListener` methods renamed to `addEventListener`, `invokeEventListener` and `removeEventListener` respectively

### - Added `walletHash` and `nameHash` private variables to monetization object
Previously, the operations `getState`,`addEventListener`,  `removeEventListener` and `invokeEventListener` took O(n) time to find the package being referred to, blocking the main thread. This is now being done in constant time, thus improving performance.
`walletHash` and `nameHash` store index of the packages. While `nameHash` contains the index of the package corresponding to the key, `walletHash` has array of such indexes for value, as more than one packages can have the same wallet. 

In cases when more than one packages have the same wallet, the events are fired and states are changed for all these packages when any one of them is being monetized.

`nameHash` has keys of the type `name@version`.

### - `invokeEventListener` is now working as expected and taking and passing one argument
This argument is the same as the response object which is passed by any event being fired in the browser, and is passed by the provider module being used. The correct package is identified by looking into the argument for wallet address, which is matched with `walletHash` to get the correct index.

### - Initial state of all packages is now `stopped`
It was `pending` in previous versions. Fixed now

***************
The main post has also been updated according to these new changes.

The only remaining problem is that monetization does not occur if puppeteer's chromium instance is specifically minimized, otherwise it works fine, even when not in focus. You can leave it running in the background and expect it to be working absolutely fine and as expected.