import { EventEmitter } from 'eventemitter3'; import { SocketEvent, SocketOwner, SocketEventHandler, SocketType } from './socket'; import { SignalReceiver, SignalHandler, Signal, SignalReceiverCastResult } from './signal'; import { PayloadDescriptor } from './payload-description'; import { Packet } from './packet'; import { SocketTap } from './socket-tap'; import { Nullable } from '../common-types'; import { SocketDescriptor } from './socket-descriptor'; export declare abstract class Socket extends EventEmitter implements SignalReceiver { private type_; private state_; private descriptor_; private signalHandler_; private tap_; private isReady_; private isReadyArmed_; private resolveDisposed_; protected owner: SocketOwner; constructor(type: SocketType, descriptor: SocketDescriptor); close(): void; type(): SocketType; descriptor(): SocketDescriptor; payload(index?: number): PayloadDescriptor; payloads(): PayloadDescriptor[]; /** * Read transferring flag of socket state. */ isTransferring(): boolean; /** * A utility method to manage the readyness state synchroneously. * For every socket-instance we are then retrieving the whenReady promise * *once* and tracking the ready-state through it. */ isReady(): boolean; /** * MAY be implemented by subclasses to indicate async I/O resource initialization readyness * when applicable (for example for file-system or MediaSource socket). * * The default implementation works for any socket impl that is ready "in-sync" with the constructor * as it returns an already resolve promise. * * Further specification constraints: * * 1) When the readyness-promise hasn't been resolved yet, the socket MAY crash * when it gets transferred data or signals cast. * * 2) The socket MUST be connectable upon creation however. * Ideally, a pipeline (or the respective branch) should first make sure that all it's I/O sockets are * "ready" before enabling any data-flow. * * 3) Socket implemention SHOULD never buffer/queue-up any data in the integration layer, * as it is a task that processors are more suited to do and that is independent of socket. * * 4) Queuing data in proc while downstream sockets become ready MAY be the best solution in certain * cases of pipeline restructering. * * 5) I/O "reading" output-sockets MAY become ready async and not cause further harm by not implementing `whenReady` method. * * 6) A socket MUST never go back to something "not ready" when it was ready once. * * FIXME: implement "dispose" method * */ whenReady(): Promise; protected handleWithTap_(tap: SocketTap, p: Packet): Nullable; /** * For subclasses only. Set the transfering flag of the socket state. */ protected setTransferring_(b: boolean): void; private transferAsync_; /** * Wraps transferSync in an async call and returns a promise * @param p */ transfer(p: Packet): Promise; /** * @abstract * Transfer the partial ownership of a Packet to this Socket. * * Implemented by the subclass in a "sync" manner. * * Return `false` value may indicate that the socket is not peered * and thus no effective transfer took place, or that the data processing handler * is not set in some other way, or an error was thrown when processing. */ abstract transferSync(p: Packet): boolean; /** * Passes the signal to the handler. * The default implementation is a synchroneous call, * but this may be anything asynchrneous and/or RPC'd in sub-classes, * since the SignalHandler is designed to return a Promise. * * The idea by default is that sockets pass signal on to their internal handler, * which can be set by anyone to allow handling signals on this socket. * * Typically the signal-handler of an input-socket will have to be set by it's peer output socket, * to allow for signals to travel through in up direction. If one would override that, one is also in charge * of signal communication up from input to ouput between these two sockets. Since an output-socket can also * peer an output socket, this is also the case for their communication of up signals. * * Note that the base class of Socket does not cast to the socket owner per-se. * * This is a detail that is implemeneted by the i/o sockets (and can be opted-in when implementing * from abstract socket). * */ cast(signal: Signal): SignalReceiverCastResult; setSignalHandler(signalHandler: SignalHandler): void; setAndGetTap(socketTap: T): T; setTap(tap: SocketTap): Socket; getTap(): SocketTap; setOwner(owner: SocketOwner): Socket; getOwner(): SocketOwner; emit(e: SocketEvent): boolean; on(event: SocketEvent, handler: SocketEventHandler): this; once(event: SocketEvent, handler: SocketEventHandler): this; off(event: SocketEvent, handler: SocketEventHandler): this; protected _emit(event: SocketEvent): void; }