How to use bakeryjs - 10 common examples

To help you get started, we’ve selected a few bakeryjs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Socialbakers / BakeryJS / example / components / generators / helloworld.js View on Github external
const {boxFactory} = require('bakeryjs');
const {promisify} = require('util');

const timeout = promisify(setTimeout);

// boxFactory creates a component,
// it must be the default export of the module
module.exports = boxFactory(
	// Component's metadata
	{
		provides: ['msg'], // What kind of data this component provides?
		requires: [], // What kind of data this component requires to work?
		emits: ['msg'], // This component can emit multiple messages at once, which must be reflected in this property
		aggregates: false, // Whether the component can aggregate multiple messages
	},
	// Logic of the component, can be async function which emits data over time
	async function(serviceProvider, value, emit) {
		// Each message is an object with key corresponding to 'emits' property in metadata
		emit([{msg: 'Hello world!'}]);
		await timeout(230);
		// Component can emit multiple messages at once
		emit([{msg: '¡Hola mundo!'}, {msg: 'Ahoj světe!'}]);
		await timeout(150);
		emit([{msg: 'Hallo Welt!'}, {msg: 'Bonjour monde!'}]);
github Socialbakers / BakeryJS / example / components / processors / wordcount.js View on Github external
const {boxFactory} = require('bakeryjs');

// Processor component which calculates number of words in a message
module.exports = boxFactory(
	{
		provides: ['words'],
		requires: ['msg'],
		emits: [],
		aggregates: false,
	},
	// serviceParamsProvider may contain some shared modules, like logger
	// second parameter is an object with properties corresponding to 'requires'
	function(serviceProvider, {msg}) {
		return {words: msg.split(/\W+/).length};
	}
);
github Socialbakers / BakeryJS / test-data / processors / wordcount.ts View on Github external
import {boxFactory, ServiceProvider, MessageData} from 'bakeryjs';

module.exports = boxFactory(
	{
		provides: ['words'],
		requires: ['msg'],
		emits: [],
		aggregates: false,
	},
	function(serviceProvider: ServiceProvider, value: MessageData) {
		return {words: (value.msg as string).split(/\W+/).length};
	}
);
github Socialbakers / BakeryJS / test-data / processors / wordbatchcount.ts View on Github external
import {boxFactory, ServiceProvider, MessageData} from 'bakeryjs';

module.exports = boxFactory(
	{
		provides: ['words'],
		requires: ['msg'],
		aggregates: false,
		batch: {
			maxSize: 3,
			timeoutSeconds: 0.3,
		},
	},
	async function(serviceProvider: ServiceProvider, values: MessageData[]) {
		return values.map((value) =>
			Object.create(null, {
				words: {value: (value.msg as string).split(/\W+/).length},
			})
		);
	}
github Socialbakers / BakeryJS / test-data / generators / hellobatchworld.ts View on Github external
import {boxFactory, ServiceProvider, MessageData} from 'bakeryjs';

module.exports = boxFactory(
	{
		provides: ['msg'],
		requires: [],
		emits: ['msg'],
		aggregates: false,
	},
	async function(
		serviceProvider: ServiceProvider,
		value: MessageData,
		emit: (chunk: MessageData[], priority?: number) => void
	) {
		return new Promise((resolve) => {
			emit([{msg: 'Hello World!'}, {msg: 'Yellow World!'}]);
			setTimeout(
				() => emit([{msg: 'Hallo Welt!'}, {msg: 'Salut Monde!'}]),
				150
github Socialbakers / BakeryJS / test-data / processors / wordbatchcountslow.ts View on Github external
import {boxFactory, ServiceProvider, MessageData} from 'bakeryjs';

module.exports = boxFactory(
	{
		provides: ['words'],
		requires: ['msg'],
		aggregates: false,
		batch: {
			maxSize: 3,
			timeoutSeconds: 0.1,
		},
	},
	async function(serviceProvider: ServiceProvider, values: MessageData[]) {
		const vals = values.map((value) =>
			Object.create(null, {
				words: {value: (value.msg as string).split(/\W+/).length},
			})
		);
		await new Promise((resolve) => setTimeout(resolve, 1200));
github Socialbakers / BakeryJS / test-data / generators / helloworld.ts View on Github external
import {boxFactory, ServiceProvider, MessageData} from 'bakeryjs';

module.exports = boxFactory(
	{
		provides: ['msg'],
		requires: [],
		emits: ['msg'],
		aggregates: false,
	},
	async function(
		serviceProvider: ServiceProvider,
		value: MessageData,
		emit: (chunk: MessageData[], priority?: number) => void
	) {
		emit([{msg: 'Hello World!'}]);
		return;
	}
);
github Socialbakers / BakeryJS / test-data / processors / checksum.ts View on Github external
import {boxFactory, ServiceProvider, MessageData} from 'bakeryjs';

module.exports = boxFactory(
	{
		provides: ['checksum'],
		requires: ['words', 'punct'],
		emits: [],
		aggregates: false,
		parameters: {
			title: 'Parameter of the box -- positive number',
			type: 'number',
			minimum: 0,
		},
	},
	function(serviceProvider: ServiceProvider, value: MessageData) {
		return {
			checksum:
				Math.sqrt(serviceProvider.parameters || 2) * value.words +
				value.punct,
github Socialbakers / BakeryJS / tests / components / _ / processors / print.ts View on Github external
import {boxFactory, ServiceProvider, MessageData} from 'bakeryjs';

const Print = boxFactory(
	{
		requires: ['jobId', 'raw'],
		provides: [],
		emits: [],
		aggregates: false,
	},
	function processValue(
		services: ServiceProvider,
		input: MessageData,
		neverEmit: (chunk: MessageData[], priority?: number) => void
	): MessageData {
		services.get('logger').log({printBox: JSON.stringify(input)});
		return {};
	}
);
export default Print;
github Socialbakers / BakeryJS / tests / components / _ / generators / tick.ts View on Github external
import {boxFactory, ServiceProvider, MessageData} from 'bakeryjs';

const Tick = boxFactory(
	{
		requires: ['jobId'],
		provides: ['raw'],
		emits: ['tick'],
		aggregates: false,
	},
	function processValue(
		serviceProvider: ServiceProvider,
		value: MessageData,
		emitCallback: (chunk: MessageData[], priority?: number) => void
	): Promise {
		let i = 0;
		return new Promise((resolve: (result?: any) => void): void => {
			const id = setInterval((): void => {
				if (i >= 3) {
					clearInterval(id);

bakeryjs

FBP-inspired library

MIT
Latest version published 3 years ago

Package Health Score

54 / 100
Full package analysis

Popular bakeryjs functions