Home Reference Source
import Session from 'remote-protocol/lib/Session.js'
public class | source

Session

Extends:

events~EventEmitter → Session

Direct Subclass:

RemoteSession

Example:

import { Session } from 'remote-protocol';

const session = new Session(objectStream);

Constructor Summary

Public Constructor
public

constructor(objectStream: Stream, opts: object)

Create a new session with an object-stream to the other peer.

Member Summary

Public Members
public get

True if the session is no longer readable.

public get

True if the session is open.

public get

The number of requests sent and waiting for response.

Method Summary

Public Methods
public

destroy(err: Error): void

Destroy this session and it's stream.

public

dispatch(value: *): *

Convert the given value to a value that can be send ove to the other peer.

public

End this session stream.

public

fetch(value: Action | *): *

Fetch an arbitrary value.

public

request(action: Action, opts: object, onFulfilled: function, onRejected: function): Session

Request from the other peer to fetch the given action and send back it's response value.

public

send(action: Action): Session

Send an action for execution to the other peer.

public

Return session constructor name.

Public Constructors

public constructor(objectStream: Stream, opts: object) source

Create a new session with an object-stream to the other peer.

Params:

NameTypeAttributeDescription
objectStream Stream

An object duplex stream

opts object
  • optional

Options

opts.timeout null | number
  • optional
  • default: 20e3

Default timeout for requests in milliseconds. If null is given, request will not timeout.

opts.endTimeout null | number
  • optional
  • default: 200

Default timeout to end the session in milliseconds after the other ended the stream.

Public Members

public get isEnded: boolean source

True if the session is no longer readable.

public get isOpen: boolean source

True if the session is open.

public get openRequests: number source

The number of requests sent and waiting for response.

Public Methods

public destroy(err: Error): void source

Destroy this session and it's stream.

Params:

NameTypeAttributeDescription
err Error
  • optional

Optional error

Return:

void

Emit:

error

If an error given, emit and "error" event as well.

close

When the session destroyed

public dispatch(value: *): * source

Convert the given value to a value that can be send ove to the other peer. If the value can be send as-is, return the same value (default implementation).

Params:

NameTypeAttributeDescription
value *

The value to dispatch

Return:

*

public end(): Session source

End this session stream.

Return:

Session

public fetch(value: Action | *): * source

Fetch an arbitrary value. If the given value is instance of Action, fetch it. Otherwise return it as is.

Params:

NameTypeAttributeDescription
value Action | *

The given value

Return:

*

public request(action: Action, opts: object, onFulfilled: function, onRejected: function): Session source

Request from the other peer to fetch the given action and send back it's response value.

Params:

NameTypeAttributeDescription
action Action

the given action to fetch by the peer

opts object
  • optional

Options

opts.timeout number | null
  • optional

Override the default timeout for this request (See constructor).

onFulfilled function

a function that will be called with the response value on the first arguments as-is (ie. without Promise.resolve).

onRejected function

a function that will be called with an error at the first argument if the fetching throws an error or the request timeout as reached.

Return:

Session

public send(action: Action): Session source

Send an action for execution to the other peer.

Params:

NameTypeAttributeDescription
action Action

The action to send

Return:

Session

public toString(): string source

Return session constructor name.

Return:

string