Coder Social home page Coder Social logo

cq-ch's Introduction

Command query channels

codecov

Motivation

In some cases, JS applications in the browser need to exchange information or perform some actions in response to events in another application that runs with it in the same environment.

This is especially actual for frontend microservice architecture as known as micro frontends.

Of course, you can always use events, but this can easily lead to code pollution, increased interlocking between applications, and vague interfaces.

Getting started with base cq-ch

Send command

At first, we need to declare an interface of commands, that our application will send. In order for the library to identify the application or interface, a unique identifier must be passed as the second argument.

As result, we receive a function to send commands of types, defined in our interface. Other types of commands will throw exceptions if we try to send them.

import { command } from '@olime/cq-ch';
// Create interface.
const send = command(['FIRST_COMMAD_TYPE', 'SECOND_COMMAND_TYPE'], 'unique-key-of-application');
// Sending command. Command is a simple JS Object with one required field - type.
send({
  type: 'FIRST_COMMAND_TYPE',
  data: 'Hello world.'
});

Receive commands

To receive commands, we need to declare an interface of that commands types.

As a result, function execute will return a function. If we call the resulting function, with the command type (or array of types), we get an asynchronous function, each call of which will return us the next received command object of the passed type(s).

import { execute } from '@olime/cq-ch';
// Create executed commands interface of application.
const takeCommand = execute(['THIRD_COMMAND_TYPE', 'FOUTH_COMMAND_TYPE'], 'unique-key-of-application');
// Run checking of new commands in filter channel.
setInterval(async () => {
  const command = await takeCommand();
  if (command) {
    console.log(command);
  }
}, 3);

Send queries

To send queries, we need to declare an interface of that queries types.

As a result, function request will return us a function to send queries of types, defined in our interface. Queries of other types will throw exceptions if we try to send them.

import { request } from '@olime/cq-ch';
// Create interface of sending queries in your application.
const sendQuery = request(['FIRST_QUERY_TYPE'], 'unique-key-of-application');
// Somewhere in your application, request data in async function.
const result = await sendQuery({type: 'FIRST_QUERY_TYPE', data: 'Hello world'});

Receiving queries

To receive queries, we need to declare an interface of that query types.

As a result, function respond will return a function. If we call the resulting function, with the request type (or array of types), we get an asynchronous function, each call of which will return us the next received request object of the passed type(s).

import { respond } from '@olime/cq-ch';
// Create interface of receiving queries in your application.
const takeQuery = respond(['THIRD_QUERY_TYPE', 'FOUTH_QUERY_TYPE'], 'unique-key-of-application');
// Run checking of new received queries in filter channel.
setInterval(async () => {
  const query = await takeQuery();
  if (query) {
    // Respond with data on received query.
    query.resolve({ data: 'Hello world!'});
  }
}, 2);

Receiving messages without separation on commands and queries

To receive messages without separation, we can use take function and declare interface with it.

import { take } from '@olime/cq-ch';
// Create interface of receiving queries in your application.
const takeMessage = take(['THIRD_QUERY_TYPE', 'FOUTH_QUERY_TYPE', 'FIRST_COMMAND_TYPE'], 'unique-key-of-application');
// Run checking of new received queries in filter channel.
setInterval(async () => {
  const message = await takeMessage();
  if (message) {
    // Respond with data on received query.
    message.resolve?.({ data: 'Hello world!'});
    // Do something else with message.
    console.log(message);
  }
}, 2);

Getting started with toolkit

If you want to decrease amount of boilerplate code, you can use @olime/cq-ch/es/toolkit. It's wrapper under raw base library with some useful functions for creating messages and process them.

At first, we need to create channel for sending and receiving messages. Then create message creator function and send message with send method of channel.

import {createChannel, createCommand, createQuery} from '@olime/cq-ch/es/toolkit';
// Create channel for sending and receiving messages.
const channel = createChannel('unique-key-of-application');
// Create command creator with payload type definition.
const testCommand = createCommand<string>('FIRST_COMMAND_TYPE');
// Create query creator with payload and response type definitions.
const pingQuery = createQuery<'ping', 'pong'>('PING');
// Create and send command with payload.
channel.send(testCommand('Hello'));
// Create and send query with payload, and receive response.
const pong = await channel.send(pingQuery('ping'));
// pong variable value is 'pong'.

You can separate the message creator functions into a separate package.

// Inside a message contract package, for example @acme/messages
import {createCommand} from '@olime/cq-ch/es/toolkit';
import {createQuery} from "./index";

export const firstCommand = createCommand<string>('FIRST_COMMAND_TYPE');
export const secondCommand = createCommand<{ testProperty: string }>('SECOND_COMMAND_TYPE');
export const pingQuery = createQuery<'ping', 'pong'>('PING');

And use them in specific applications as a contract.

// Inside a specific application
import { firstCommand, secondCommand, pingQuery } from '@acme/messages';
import { createChannel } from "@olime/cq-ch/es/toolkit";
// Create channel for sending and receiving messages.
const channel = createChannel('unique-key-of-application');
// Create and send first command with payload.
channel.send(firstCommand('Hello'));
// Create and send second command with payload.
channel.send(secondCommand({ testProperty: 'hello' }));
// Create and send ping query.
const pong = channel.send(pingQuery('ping'));
// Process input messages.
setInterval(async () => {
    // Receive messages.
    const message = await channel.take(pingQuery, firstCommand);
    if (message) {
        if (firstCommand.match(message)) {
            // Make some good stuff with received command.
        }
        if (pingQuery.match(message)) {
            // Respond on query.
            channel.respond(message, 'pong');
        }
    }
}, 3);

cq-ch's People

Contributors

olime avatar dependabot[bot] avatar

Stargazers

Pavel Rotanov avatar Pastuhov Kirill avatar Ivan Burkov avatar Dmitry Shishkin avatar

Watchers

 avatar Dmitry Shishkin avatar Pavel Rotanov avatar

cq-ch's Issues

Can't pass an array of incoming messages to takeChannel

I have a problem passing an array of incoming messages to takeChannel (return function from takeChannelCreator). But this feature is described in the documentation and jsdoc.

const getCommandsOfThirdType = execute(['THIRD_COMMAND_TYPE']);

I found a problem with double nesting in an array in the castType function if the argument inputType is an array:
let resultType = [inputType];

This can be fixed as follows:

let resultType = Array.isArray(inputType) ? inputType : [inputType];

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.