0.3.0 • Published 5 years ago

mharj-dispatcher v0.3.0

Weekly downloads
2
License
LGPL-3.0-or-later
Repository
github
Last release
5 years ago

dispatcher Build Status

Typescript based Dispatcher

Javascript example with two keys

// new dispatcher with specific keys
const disp = new Dispatcher();
// register promise to keys
disp.addAction({_target: 'weather', _act: 'update'}, ({data}) => {
	console.log('weatherData', data);
	return Promise.resolve();
});
// dispatch some message
const msg = {_target: 'weather', _act: 'update', location: 'some city', temp: 10};
Promise.all(disp.dispatch(msg))
	.then( () => {
		console.log('dispatch done');
	})
	.catch( (err) => {
		console.log('Dispatch error', err);
	});

Typescript example with two keys

interface IKeys {
	_act: string;
	_target: string;
}

interface IData {
	location: string;
	temp: number;
}
// new dispatcher with specific keys
const disp = new Dispatcher<IKeys, Promise<void>>();
// register promise to keys
const dispKey: IKeys = {_target: 'weather', _act: 'update'};
disp.addAction<IData>(dispKey, ({keys, params, data}) => {
	console.log('weatherData', data.location, data.temp);
	return Promise.resolve();
});
// dispatch some message
const msg: IData & IKeys = {_target: 'weather', _act: 'update', location: 'some city', temp: 10};
Promise.all(disp.dispatch(msg, undefined))
	.then( () => {
		console.log('dispatch done');
	})
	.catch( (err) => {
		console.log('Dispatch error', err);
	});

Typescript example with Keys/Action/Promise functions and two keys

interface IKeys {
	_act: string;
	_target: string;
}

interface IData {
	location: string;
	temp: number;
}

// construct key for dispatch
const weatherMessageKeys = ():IKeys => {
	return {_target: 'weather', _act: 'update'};
}
// construct action with message data
const weatherMessageAction = (location: string, temp: number): IData & IKeys => {
	return {_target: 'weather', _act: 'update', location, temp}
}
// construct dispatch Promise to handle payload (and resolve Promise)
const weatherMessageDispatchPromise = ({data}:{data: IData}) => {
	console.log('weatherData', data.location, data.temp);
	return Promise.resolve();
}

// new dispatcher with specific keys
const disp = new Dispatcher<IKeys, Promise<void>>();
// register promise to keys
disp.addAction<IData>( weatherMessageKeys(), weatherMessageDispatchPromise);
// dispatch some promise messages
Promise.all(disp.dispatch( weatherMessageAction('some city', 10) ))
	.then( () => {
		console.log('dispatch done');
	})
	.catch( (err) => {
		console.log('Dispatch error', err);
	});
0.3.0

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago