npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@sensenet/client-utils

v2.3.0

Published

General sensenet independent client side utilities

Downloads

284

Readme

@sensenet/client-utils

General sensenet independent client side utilities

NPM version NPM downloads License: GPL v2

Install

# Yarn
yarn add @sensenet/client-utils

# NPM
npm install @sensenet/client-utils

Disposable

You can implement disposable resources and use them with a using() or usingAsync() syntax. Example:

class Resource implements Disposable {
  dispose() {
    // cleanup logics
  }
}

using(new Resource(), resource => {
  // do something with the resource
})

usingAsync(new Resource(), async resource => {
  // do something with the resource, allows awaiting promises
})

ObservableValue and ValueObservers

You can track value changes using with this simple Observable implementation.

Example:

const observableValue = new ObservableValue<number>(0)
const observer = observableValue.subscribe(newValue => {
  console.log('Value changed:', newValue)
})

// To update the value
observableValue.setValue(Math.random())
// if you want to dispose a single observer
observer.dispose()
// if you want to dispose the whole observableValue with all of its observers:
observableValue.dispose()

PathHelper

The class contains small helper methods for path transformation and manipulation.

Retrier

Retrier is a utility that can keep trying an operation until it succeeds, times out or reach a specified retry limit.

const funcToRetry: () => Promise<boolean> = async () => {
  const hasSucceeded = false
  // ...
  // custom logic
  // ...
  return hasSucceeded
}
const retrierSuccess = await Retrier.create(funcToRetry)
  .setup({
    Retries: 3,
    RetryIntervalMs: 1,
    timeoutMs: 1000,
  })
  .run()

Trace

Trace is an utility that can be used to track method calls, method returns and errors

const methodTracer: Disposable = Trace.method({
  object: myObjectInstance, // You can define an object constructor for static methods as well
  method: myObjectInstance.method, // The method to be tracked
  methodName: 'method', // Unique identifier for the method
  isAsync: true, // if you set to async, method finished will be *await*-ed
  onCalled: traceData => {
    console.log('Method called:', traceData)
  },
  onFinished: traceData => {
    console.log('Method call finished:', traceData)
  },
  onError: traceData => {
    console.log('Method throwed an error:', traceData)
  },
})

// if you want to stop receiving events
methodTracer.dispose()

Logging package

Initializing with @sensenet-client-utils/inject

You can start using the Logging service with an injector in the following way

import { ConsoleLogger, Injector } from '@sensenet/client-utils'

const myInjector = new Injector().useLogging(ConsoleLogger, Logger1, Logger2 /** ...your Logger implementations */)

You can retrieve the Logger instance with

const myLogger = myInjector.logger

Logging events

You can log a simple event with

myLogger.addEntry({
  level: LogLevel.Verbose,
  message: 'My log message',
  scope: 'logger/test',
  data: {
    foo: 1,
    bar: 42,
  },
})

or

myLogger.verbose({
  message: 'My log message',
  scope: '@sensenet/client-utils/test',
  data: {
    foo: 1,
    bar: 42,
  },
})

The two snippets do the same - they will add a log entry to each registered logger.

Scoped loggers

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

const scopedLogger = myLogger.withScope('logger/test')
scopedLogger.verbose({ message: 'FooBarBaz' })
Implementing your own logger

You can implement your own logging logic in the similar way as this custom log collector

import { AbstractLogger, Injectable, LeveledLogEntry } from '@sensenet/client-utils'

@Injectable({ lifetime: 'singleton' })
export class MyCustomLogCollector extends AbstractLogger {
  private readonly entries: Array<LeveledLogEntry<any>> = []

  public getEntries() {
    return [...this.entries]
  }

  public async addEntry<T>(entry: LeveledLogEntry<T>): Promise<void> {
    this.entries.push(entry)
  }

  constructor() {
    super()
  }
}

Injector

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

const myInjector = new Injector()

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.

const childInjector = myInjector.createChild({ owner: 'myCustomContext' })

Injectable

Creating an Injectable service from a class

You can create an injectable service from a plain class when decorating with the @Injectable() decorator.

@Injectable({
  /** Injectable options */
})
export class MySercive {
  /** ...service implementation... */

  constructor(s1: OtherInjectableService, s2: AnotherInjectableService) {}
}

The constructor parameters (s1: OtherInjectableService and s2: AnotherInjectableService) should be also decorated and will be resolved recursively.

Lifetime

You can define a specific Lifetime for Injectable services on the decorator

@Injectable({
  lifetime: 'transient',
})
export class MySercive {
  /** ...service implementation... */
}

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

const service = myInjector.getInstance(MySercive)
Explicit instance setup

There are cases that you have to set a service instance explicitly. You can do that in the following way

class MyService {
  constructor(public readonly foo: string)
}

myInjector.setExplicitInstance(new MyService('bar'))