Introduction to Dojo Middleware

Keywords: Javascript Attribute

Dojo's middleware system can manage asynchronous or imperative APIs responsively, and affect the behavior and attribute APIs of function-based composite components or other middleware.

The framework has provided several core middleware and optional middleware, and application developers can easily write their own middleware.

function describe
Responsive DOM access With the help of middleware, function-based components can process and use specific information and API s of DOM parts corresponding to output nodes.
Life Cycle of Control Component Rendering For any composite component, the middleware can control all parts of the Dojo rendering pipeline, such as rendering components invalidated when updates are needed. You can also pause and continue the rendering of components, while waiting for key information to be available, short-circuit rendering results.
The framework provides some Middleware Dojo provides middleware that enables components to perform many functions, such as response and control focus, simple cache values, cross-event and size-change events of response elements, CSS themes, internationalization and rendering at build time.
Easy to combine and reuse The design of middleware is closely related to function-based components. Middleware can be combined into component hierarchy and reused when developing custom middleware.

Basic Usage

Creating Middleware

  • Defining create() based on function components can also be used to define Middleware
  • Define an attribute interface (optional) to extend the attribute interface of components that use this middleware. These attribute values are passed in when creating composite component instances
  • Returns a simple function reference that defines the middleware API for use by other composite components or middleware

src/middleware/myMiddleware.ts

import { create } from '@dojo/framework/core/vdom';

const factory = create().properties<{ middlewareProp?: boolean }>();

export const myMiddleware = factory(({ properties }) => {
    return () => {
        return properties().middlewareProp ? 'Conditional is true' : 'Conditional is false';
    };
});

export default myMiddleware;

Composite Middleware

  • Combining middleware and returning an object to expose more complex API s
  • Using Core invalidator Middleware Marks Components as Need to Render

src/middleware/myComposingMiddleware.ts

import { create, invalidator } from '@dojo/framework/core/vdom';
import myMiddleware from './myMiddleware';

const factory = create({ myMiddleware, invalidator });

export const myComposingMiddleware = factory(({ middleware: { myMiddleware, invalidator } }) => {
    return {
        get() {
            return myMiddleware();
        },
        set() {
            invalidator();
        }
    };
});

export default myComposingMiddleware;

Use Middleware in components

  • Extending component's attribute interface with additional attributes used by middleware
  • When using a component composed of middleware, the attributes of the middleware are passed in

src/widgets/MyWidget.tsx

import { create, tsx } from '@dojo/framework/core/vdom';
import myComposingMiddleware from '../middleware/myComposingMiddleware';

const factory = create({ myComposingMiddleware });

export default factory(function MyWidget({ properties, middleware: { myComposingMiddleware } }) {
    return (
        <virtual>
            <div>{`Middleware property value: ${properties.middlewareProp}`}</div>
            <div>{`Middleware usage: ${myComposingMiddleware.get()}`}</div>
        </virtual>
    );
});

src/main.tsx

import renderer, { tsx } from '@dojo/framework/core/vdom';
import MyWidget from './widgets/MyWidget';

const r = renderer(() => <MyWidget middlewareProp={true} />);
r.mount();

Posted by jesseledwards on Thu, 22 Aug 2019 21:11:23 -0700