Environment API

#Environment Class

The base class for all environment types

The environment object is available via the globally declared coenv variable.

  • In the browser, there is a single environment object that represents the browser.
  • When rendering, there are multiple environment objects, one per render request.

Never modify, nor cache the environment object as it can (and will) change from request to request in a server environment.

class Environment extends EventTarget {
    browser: boolean;
    ssr: boolean;
    enterLoading(): void;
    leaveLoading(): void;
    get loading(): boolean;
    load(callback: () => Promise<any>): Promise<any>;
    untilLoaded(): Promise<void>;
}

#browser

True when running in browser environment.

browser: boolean;

#enterLoading()

Notifies the environment that an async load operation is starting.

Environment level loading notifications are used when rendering to determine when the initial page load has completed and rendering can commence.

enterLoading(): void;

#leaveLoading()

Notifies the environment that an async load operation has finished.

leaveLoading(): void;

#load()

Runs an async data load operation.

load(callback: () => Promise<any>): Promise<any>;
  • callback A callback that performs the data load

#loading

Returns true if there are in progress async load operations.

get loading(): boolean;

#ssr

True when running in a rendering environment.

ssr: boolean;

#untilLoaded()

Returns a promise that resolves when any pending load operations have finished.

untilLoaded(): Promise<void>;

#setEnvProvider()

Sets an environment provider.

function setEnvProvider(value: () => Environment): void;
  • value A callback to provide the current environment object