UNPKG

3.51 kBPlain TextView Raw
1import {Injector, Type} from '@angular/core';
2
3// There are three essential concepts that are defined in this file: variants, bootstrappers,
4// and application state readers. Each of these can be one of two things: either a class
5// decorated with @Injectable() that accepts some application services in its constructor, or
6// a function that accepts an Injector and queries the dependency injector itself. If it is
7// a class definition, that class will be instantiated in the context of a running application.
8// Likewise if it is a function, it will be executed and given the root dependency Injector
9// which you can use to query the application for the services you need.
10
11export interface Variant<T> {
12 // A class or a function which can transition the application into the state described by
13 // this variant and a value that is provided at runtime.
14 transition?: Type<StateTransition<T>> | StateTransitionFunction<T>;
15
16 // An optional set describing all the possible values of this variant. For example if this
17 // is a locale variant, this set will contain all languages that the application has
18 // translations for. The only time this set is used is when we are prerendering all variant
19 // permutations of an application. If you are doing demand rendering, you do not need to
20 // set this because we will get the value from the object supplied to Application::renderUri
21 values?: Array<T> | Set<T>;
22}
23
24export type VariantsMap = {[variant: string]: Variant<any>};
25
26// A StateReader<T> is used to extract state from the application, and that state is then
27// injected into the rendered document so that the client application has access to it.
28export interface StateReader<State> {
29 getState(): State | Promise<State>;
30}
31
32export type ApplicationStateReaderFunction<R> = (injector: Injector) => Promise<any>;
33
34export type ApplicationStateReader<R> = Type<StateReader<R>> | ApplicationStateReaderFunction<R>;
35
36// A bootstrap function is executed after the root module is instantiated and before rendering.
37export interface Bootstrap {
38 bootstrap(): Promise<void> | void;
39}
40
41export type ApplicationBootstrapperFunction = (injector: Injector) => Promise<void> | void;
42
43export type ApplicationBootstrapper = Type<Bootstrap> | ApplicationBootstrapperFunction;
44
45// A StateTransition is a class that uses the dependency injector to request application services
46// and then places the application in the state described by the value provided to transition().
47// These are used to compose application variants. So for example several state transitions may
48// be composed together into a ComposedTransition and then that transition is run at application
49// bootstrap, as part of the rendering process.
50export interface StateTransition<T> {
51 transition(value: T): Promise<void> | void;
52}
53
54export type StateTransitionFunction<T> = (injector: Injector, value: T) => Promise<void> | void;
55
56export type ComposedTransition = (injector: Injector) => Promise<void>;
57
58// A postprocessor is used to manipulate the DOM prior to completion of serialization. It accepts
59// the document object for the application being rendered, and a string representing the rendered
60// document. You can either manipulate document using DOM APIs or you can return a string that
61// will represent the new version of the rendered document. If you return void, it is assumed that
62// you did your processing by manipulating the document object.
63export type Postprocessor = (document: Document, html?: string) => void | string;