All Downloads are FREE. Search and download functionalities are using the official Maven repository.

reference.language.category_singletons.html Maven / Gradle / Ivy




    
    Methods
    
    

    
    

    
    

    
    
    
    

    


Singletons

There are situations which require having one and only one instance of a given category.

If, for example, you were to define a World category, chances are you would only need one instance.

Prompto directly supports this concept through Singletons, a specialized category which has one and only one instance.

Being a specialized category, a singleton supports most category features i.e. attributes and methods except the following:

  • a singleton cannot derive from another category or singleton
  • a singleton cannot contain abstract methods
  • a singleton cannot be instantiated
  • a singleton cannot be made mutable

Accessing singleton attributes and methods

Singleton attributes and methods are accessible using the singleton name, which serves as both a type and the only instance.

Populating singleton attributes

An immutable singleton would not be very useful, since it would only hold constant values known at coding time. On the other hand, it would be very difficult to ensure consistency of a mutable singleton in the context of multiple Workers. Prompto solves this problem using with blocks.

In the below example, we:

  • define a 'Configuration' singleton
  • populate its attributes inside a with block
  • read an attribute directly from the singleton

In the above example, it is safe to read login outside a with block, because it only involves reading one value.

Calling singleton methods

Singleton methods are likely to:

  • access more than one attribute
  • change the singleton's attribute values
For those reasons, singleton methods can only be called within with blocks:

How the with block works

The with block ensures that only the main program or one Worker at a time can execute the statements contained in with block body. This protection spans across all with blocks for a given singleton. Internally, the main program or Worker takes ownership of the singleton until all statements in the block body are executed, thus blocking any other worker from simultaneously entering any with block for the singleton.

JavaScript Workers do not share data. Although Prompto supports singletons on all platforms, developers are advised to avoid them in a browser if they use Workers, because using them can significantly slow down execution.





© 2015 - 2025 Weber Informatics LLC | Privacy Policy