DEV Community

Boris Siscanu
Boris Siscanu

Posted on • Edited on • Originally published at Medium

Introducing darkwasp library

Update: Darkwasp joined Domy project (web components).

Most of the websites right now use one processor core out of many, by doing so they reduce the user experience considerably, freezing the entire web in its actual state of progress. In the era of Deep Learning and real-time experience, it seems that the web hit the wall.

Why is this all happening? The reason is dead simple — thread safety. Web Workers were made isolated by design and the most inconvenient thing out of this is the constraint to run the Web Worker from a separate file.

Consequently, to facilitate the work with parallel processes in web environment, we came out with new library called The Dark Wasp. In a nutshell, the idea is to save the biggest part of the web app code in the storage and load it on request in a separate thread, no need to load the entire codebase all at once. The same functions could be used in frontend and backend (web browser or Node.js).

GitHub logo teamdomy / wasp

Web Workers Superset

The Distributed Storage for Functions

The package makes simple the work with parallel processes (websites on multi-core processor). It supports web browsers and backend clients.

Apache 2.0 License

Starting the first app

To start the work call the connect function, it returns the Promise and listens for an object with 2 properties:

  1. app - the name of your app/storage and
  2. peer - secret and unique UID of the user (think about it as the username and the password in one string).

The package provides 3 core types named agents: Peer, Wasp and Swarm. Allagent methods return the Promise.

For the purpose of the example, lets create and save 2 files with functions, we will store them lately:

random.js

module.exports = function() {
  return (Math.random() + 10).toString(7)
}
Enter fullscreen mode Exit fullscreen mode

fibonacci.js

module.
Enter fullscreen mode Exit fullscreen mode

To access the storage, we’ll need the name of the storage itself - “app” property and the unique identifier of the current user - “peer” property. If the storage with the given name doesn’t exist, it’ll be created and the user will get owners rights. Latest joined users will have limited rights, being simple peers.

Now, for the sake of example, let’s create a random function:

In the example above, app.wasp.random.set() means create the wasp (lambda function) with the name “random” and the code which follows in set() method. In the same time, storage doesn’t exist, so the “peer” will get owner rights.

Next, deliver the function to the storage by executing the file:

node store.js

Done! Further use of stored functions is a piece of cake, in the browser it looks as follows:

… and the Node.js environment (the “peer” is not the first, so no special treatment in this case)

That’s it!

Top comments (2)

Collapse
 
johncarroll profile image
John Carroll

So are the stored functions stateless? Or could you use darkwasp to store/access a service like ApolloClient? Definitely looks like a very useful library in either case.

Collapse
 
bsiscanu profile image
Boris Siscanu

Hi John, thank you for your interest, I started to write the response and decided to write another article about it. Keep in touch ;)