Introduction

Dojo’s middleware system provides a way to manage asynchronous or imperative APIs in a reactive way, as well as influence behavior and the property API of any composing function-based widgets or other middleware.

Several core and optional middleware are provided by the framework, however, application developers can easily write their own.

FeatureDescription
Reactive DOM accessVia middleware, function-based widgets can work with concrete information and APIs from the sections of the DOM corresponding to their output nodes.
Widget render lifecycle controlMiddleware can control the portions of Dojo’s rendering pipeline for any composing widgets, such as invalidating widgets when they require a rendering update. Widget rendering can also be paused and resumed, allowing for short-circuiting of the render output while waiting for critical information to become available.
A variety of framework-provided middlewareDojo provides optional middleware that allow widgets to implement a range of functionality such as responding to and controlling focus, simple value caching, responding to element intersection and resize events, CSS theming, internationalization and build-time rendering amongst others.
Easy composition and re-useThe middleware design is closely aligned with function-based widgets, allowing consistent composition within a widget hierarchy as well as re-use when developing custom middleware.

Basic usage

Creating middleware

  • Defining middleware via the same create() primitive used for defining function-based widgets
  • Optionally defining a properties interface that augments the properties interface of widgets that compose the middleware. Property values are given when creating instances of the composing widgets.
  • Returning a simple function reference that defines the middleware’s API that other composing widgets or middleware can interact with

src/middleware/myMiddleware.ts

  1. import { create } from '@dojo/framework/core/vdom';
  2. const factory = create().properties<{ middlewareProp?: boolean }>();
  3. export const myMiddleware = factory(({ properties }) => {
  4. return () => {
  5. return properties().middlewareProp ? 'Conditional is true' : 'Conditional is false';
  6. };
  7. });
  8. export default myMiddleware;

Composing middleware

  • Combining middleware and returning an object to expose a more complex API
  • Using the core invalidator middleware to flag the composing widget as requiring re-rendering

src/middleware/myComposingMiddleware.ts

  1. import { create, invalidator } from '@dojo/framework/core/vdom';
  2. import myMiddleware from './myMiddleware';
  3. const factory = create({ myMiddleware, invalidator });
  4. export const myComposingMiddleware = factory(({ middleware: { myMiddleware, invalidator } }) => {
  5. return {
  6. get() {
  7. return myMiddleware();
  8. },
  9. set() {
  10. invalidator();
  11. }
  12. };
  13. });
  14. export default myComposingMiddleware;

Using middleware within a widget

  • Augmenting the widget’s properties interface with additional properties from any middleware used
  • Passing in middleware properties when using widgets that compose them

src/widgets/MyWidget.tsx

  1. import { create, tsx } from '@dojo/framework/core/vdom';
  2. import myComposingMiddleware from '../middleware/myComposingMiddleware';
  3. const factory = create({ myComposingMiddleware });
  4. export default factory(function MyWidget({ properties, middleware: { myComposingMiddleware } }) {
  5. return (
  6. <virtual>
  7. <div>{`Middleware property value: ${properties.middlewareProp}`}</div>
  8. <div>{`Middleware usage: ${myComposingMiddleware.get()}`}</div>
  9. </virtual>
  10. );
  11. });

src/main.tsx

  1. import renderer, { tsx } from '@dojo/framework/core/vdom';
  2. import MyWidget from './widgets/MyWidget';
  3. const r = renderer(() => <MyWidget middlewareProp={true} />);
  4. r.mount();