@threadplane/angularFunctionv0.3.7

withAgentOverrides

Derives an agent that serves overrides in place of the named members and forwards every other read to agent, at the time of the read.

Use it instead of an object spread. An agent serves a snapshot field that is not set yet, such as error, only when it is asked for by name, so a spread copies the members that exist at that moment and the copy never gains the rest. The derived agent has no such moment: a field published later, a thread switch and a replaced session all show through it, and the members it does not override are the same references as the original's.

overrides may be a function. It is called once, with the original agent, so a replacement can call the member it wraps:

const guarded = withAgentOverrides(injectAgent(), (agent) => ({
  submit: (input, options) =>
    locked() ? Promise.resolve('aborted') : agent.submit(input, options),
}));

The original agent is not changed, and code holding it does not see the overrides. The overrides are read once: changing the object afterwards changes nothing. The derived agent resolves to the original's configuration and development-install policy, and it can be derived from again.

import { withAgentOverrides } from '@threadplane/angular';

Declaration · overload 1

export declare function withAgentOverrides<TAgent extends object>(agent: TAgent, overrides: AgentOverrides<TAgent> | ((agent: TAgent) => AgentOverrides<TAgent>)): TAgent;

Type parameters

TAgent
object

Parameters

agent · required
TAgent
overrides · required
AgentOverrides<TAgent> | ((agent: TAgent) => AgentOverrides<TAgent>)

Returns

TAgent