Options
All
  • Public
  • Public/Protected
  • All
Menu

External module @corpuscule/utils/lib/tokenRegistry

This module provides tools to connect separate decorators together.

Usage

import createTokenRegistry from '@corpuscule/utils/lib/tokenRegistry'

Index

Type aliases

Functions

Type aliases

Token

Token: object

A key to access specific data store in the registry.

The initial goal of the token design is to create an artificial closure for the decorators which are, according to the proposal, not JavaScript values and cannot be returned from a function or assigned to a variable.

It is often necessary to bind together several decorators to build a complex system on top of their interaction. The token access approach allows creating a shared store which can be obtained using the token from inside the decorator. The only thing user should do is to provide the same token for all decorators that have to be linked with each other.

TokenCreator

TokenCreator: function

A function that creates tokens.

Type declaration

Functions

createTokenRegistry

  • Creates:

    • a key-value registry where the key is a unique token and the value is a data store;
    • a function to generate a token.

    Example

    
    const [createToken, registry] = createTokenRegistry<string[]>(() => []);
    
    const token = createToken();
    
    const arr = registry.get(token);
    
    arr.push('test'); // ['test']

    Type parameters

    • T

      type of the data store.

    Parameters

    • createDataStore: function

      a function to create a new data store for the new token. The function will be called during the createToken function call.

        • (): T
        • Returns T

    • Optional createRawToken: TokenCreator

      a function that will be used to generate a new token during the createToken call. It is optional, but you can use it to inherit an already existing token system. To do it, send the createToken you want to inherit as this argument.

    Returns [TokenCreator, WeakMap<Token, T>]

    a tuple which contains the new createToken function and the registry instance.

Generated using TypeDoc