Module: docloop

TODO: Description for tis module

License:
  • GPL-3.0

Classes

DocloopAdapter
DocloopCore
DocloopEndpoint
DocloopError
DocloopLink
EventQueue

Methods


<static> cacheCalls(obj, method_name, ttl)

Replaces a method of provided object with a proxy, that will cache the return value of the original method for the givin period of time. If the proxy is called the original method will be called only if there is no cached value.

The proxy method will allways wrap the original method in a Promise.

This may help to prevent redundant API calls (e.g. if you know that the result of an API call will not change in the next 10 seconds)

Two calls with different arguments will be cached separately. Two sets of arguments are considered equal if their JSON representaions are equal.

The orginial method can be restored with obj[method_name].restore().

Parameters:
Name Type Default Description
obj Object

Target object

method_name String

Name of the method whose calls should be cached.

ttl Number 1000

Time to live for the cache values in milliseconds.

Throws:

If method_name does not point to a function.

Type
TypeError
Returns:

undefined


<static> catchAsyncErrors(fn)

Wrapper for express request handlers to catch rejected promises or async methods.

Parameters:
Name Type Description
fn function

Express request handler to wrap

Returns:

Wrapped request handler

Type
function

<static> collateCalls(obj, method_name [, force_call_after])

Replaces a method of provided object with a proxy, that will prevent multiple calls to the original method with the same arguments while waiting for a response.

The proxy method will allways wrap the original method in a Promise.

If a previous call with the same arguments is still pending the proxy will return the corresponding promise.

This may help to prevent multiple redudant API calls. (e.g. when you dont expect the result of an API call to change while it's pending)

Two sets of arguments are considered equal if their JSON representaions are equal.

The orginial method can be restored with obj[method_name].restore().

Parameters:
Name Type Argument Default Description
obj Object

Target object

method_name string

Name of the method whose calls should be callated

force_call_after Number <optional>
1000

After this time (in milliseconds) the proxy will call the original method, regardless of previous calls still pending.

Throws:

If method_name does not point to a function.

Type
TypeError
Returns:

undefined


<static> errorHandler(err, req, res, next)

Custom Express error handler, tailored to work with DocloopError. Will terminate a request with a proposed error code if error is instance of DocloopError, will use 500 if not.

Parameters:
Name Type Description
err Object

Express error

req Object

Express request

res Object

Express result

next function

Express next

Returns:
Type
undefined

<static> serializeCalls(obj, method_names)

Replaces a method of provided object with a proxy, that will chain consecutive calls: So the orginal method will be called right after all previous calls have been resolved or rejected.

The proxy method will allways wrap the original method in a Promise.

You can provide an array of method names to serialize multiple methods at once.

This may help to prevent timing conflicts of multiple calls.

The orginial mthod can be restored with obj.[method_name].restore().

Parameters:
Name Type Description
obj Object

Target object

method_names string | Array.<string>

Name(s) of the method(s) whose calls should be serialized.

Throws:

If one of the method names does not point to a function.

Type
TypeError
Returns:

undefined