General sensenet independent client side utilities
# Yarnyarn add @sensenet/client-utils# NPMnpm install @sensenet/client-utils
You can implement disposable resources and use them with a using() or usingAsync() syntax. Example:
usingnew Resource,usingAsyncnew Resource,
ObservableValue and ValueObservers
You can track value changes using with this simple Observable implementation.
// To update the valueobservableValue.setValueMath.random// if you want to dispose a single observerobserver.dispose// if you want to dispose the whole observableValue with all of its observers:observableValue.dispose
The class contains small helper methods for path transformation and manipulation.
Retrier is a utility that can keep trying an operation until it succeeds, times out or reach a specified retry limit.
Trace is an utility that can be used to track method calls, method returns and errors
// if you want to stop receiving eventsmethodTracer.dispose
Initializing with @sensenet-client-utils/inject
You can start using the Logging service with an injector in the following way
You can retrieve the Logger instance with
You can log a simple event with
The two snippets do the same - they will add a log entry to each registered logger.
At the most of the cases, you use a logger in a service with a specific scope. You can create and use a scoped logger in the following way
Implementing your own logger
You can implement your own logging logic in the similar way as this custom log collector
Injectors act as containers, they are responsible for creating / retrieving service instances based on the provided Injectable metadata. You can create an injector with simply instantiating the class
You can organize your injector(s) in trees by creating child injectors. You can use the children and services with scoped lifetime for contextual services.
Creating an Injectable service from a class
You can create an injectable service from a plain class when decorating with the
The constructor parameters (
s1: OtherInjectableService and
s2: AnotherInjectableService) should be also decorated and will be resolved recursively.
You can define a specific Lifetime for Injectable services on the decorator
The lifetime can be
- transient - A new instance will be created each time when you get an instance
- scoped - A new instance will be created if it doesn't exist on the current scope. Can be useful for injectable services that can be used for contextual data.
- singleton - A new instance will be created only if it doesn't exists on the root injector. It will act as a singleton in other cases.
Injectables can only depend on services with longer lifetime, e.g. a transient can depend on a singleton, but inversing it will throw an error
Retrieving your service from the injector
You can retrieve a service by calling
Explicit instance setup
There are cases that you have to set a service instance explicitly. You can do that in the following way