Options
All
  • Public
  • Public/Protected
  • All
Menu

Hierarchy

  • CriticalSection

Implements

Index

Constructors

Properties

Methods

Constructors

constructor

Properties

Private lock

lock: ISemaphore = ...

Static Private objectAccessor

objectAccessor: symbol = ...

Methods

do

  • do<T>(func: () => T | Promise<T>): Promise<T>
  • Wait for the critical section to become available and execute a function.

    Type parameters

    • T

    Parameters

    • func: () => T | Promise<T>
        • (): T | Promise<T>
        • Returns T | Promise<T>

    Returns Promise<T>

    A promise which is resolved with the (eventually) returned value by func. Effectively, once the critical section has been entered, Promise.resolve(func()) is returned. Especially if func returns a Thenable, by the semantics of Promise.resolve, this Thenable is adopted as the new Promise.
    In other words, it is impossible to make criticalSection.do(...) resolve to a Promise, i.e. await criticalSection.do(...) be a Promise.

Static for

  • Create or get the object-bound critical section.

    If no critical section is yet bound to the object, a new one is created and bound to that specific object. Critical sections bound to objects higher up in the prototype chain do not get inherited.

    example

    Multiple calls on the same object return the same CriticalSection:

    // The object can also be a function
    const myObj = function () {
      // ...
    };
    const firstSection = CriticalSection.for(myObj);
    const secondSection = CriticalSection.for(myObj);
    
    // true
    console.log(firstSection === secondSection);
    
    example

    No inheritance with respect to the prototype chain:

    const myPrototype = {};
    const firstSection = CriticalSection.for(myPrototype);
    
    const object = Object.create(myPrototype);
    
    // true
    console.log(CriticalSection.for(object) !== firstSection);
    
    example

    Beware of iframes and inter-website communicating code, e.g. the Array constructors differ and are therefore considered distinct objects by this method.

    const iframe = document.createElement('iframe');
    document.body.appendChild(iframe);
    const xArray = window.frames[window.frames.length-1].Array;
    
    const iframeArr = new xArray(1,2,3);
    const thisSiteArr = [4, 5, 6];
    
    // true in both cases
    console.log(iframeArr.constructor !== thisSiteArr.constructor);
    console.log(CriticalSection.for(iframeArr.constructor) !==
                CriticalSection.for(thisSiteArr.constructor));
    
    throws

    A {@link TypeError} is raised when object is a primitive value.

    Parameters

    • object: Object

      The object with which a new critical section bond should be created if it does not exists yet. object must be a real object and not a primitive as per the ECMAScript standard specification. Especially, the following values are primitives (see also [MDN][1]):

      • numbers (e.g. 1, 2, 3)
      • strings (e.g. 'Hello World')
      • booleans (true, false)
      • null
      • undefined
      • Symbols

      A {@link TypeError} will be thrown if such a value is passed. (Note that there is no method to check for primitive values in ECMAScript, therefore, this method might not throw a {@link TypeError} or other errors for new primitive values introduced in yet-to-come ECMAScript versions.)

    Returns ICriticalSection

Generated using TypeDoc