https://github.com/nfour/yaef
Yet Another Event Framework
https://github.com/nfour/yaef
Last synced: 6 months ago
JSON representation
Yet Another Event Framework
- Host: GitHub
- URL: https://github.com/nfour/yaef
- Owner: nfour
- Created: 2019-03-22T05:36:14.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-03T18:28:29.000Z (over 3 years ago)
- Last Synced: 2024-10-11T23:16:09.866Z (almost 2 years ago)
- Language: TypeScript
- Homepage:
- Size: 1.02 MB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# YAEF: Yet Another Event Framework
**Features:**
- Useful type validation for evented architectures
- IPC communication, including a **Registry**
- Clearly defined event model
+ [Usage](#usage)
+ [Example](#example)
+ [Documented example](#documented-example)
## Usage
- If you are utilizing `RemoteModuleComponent` then:
- You must set an environment variable:
- `NODE_OPTIONS=--experimental-worker`
- Requires NodeJS `>= 10.5.0`
## Example
It looks something like this:
```ts
import { Component, ComponentMediator, ComponentSignature, EventSignature } from '@yaef/core';
type IFruitTypes = 'Apple' | 'Banana';
// Create some event signatures
const ItIsANewDay = EventSignature('ItIsANewDay')
const FruitIsRipe = EventSignature('IsRipe', { fruit: '' });
// Or:
const HarvestedFruit = { name: 'HarvestedFruit', fruit: '' };
// Create some component signatures
const Apple = ComponentSignature('Apple', {
observations: [ItIsANewDay],
publications: [FruitIsRipe],
});
// Or:
const Harvester = {
name: 'Harvester',
observations: [FruitIsRipe],
publications: [HarvestedFruit],
};
// Create some components
const harvester = Component(Harvester, (m) => {
m.observe(FruitIsRipe, ({ fruit }) => {
fruit; // IFruitTypes
m.publish(HarvestedFruit, { fruit });
});
});
const apple = Component(Apple, (m) => {
m.observe(ItIsANewDay, () => {
if (true) { /** pretend this is doing something... */
m.publish(FruitIsRipe, { fruit: 'Apple' });
}
});
});
const { disconnect, connect, mediator } = ComponentMediator({
components: [apple, harvester]
});
// The components are initialized with the mediator
await connect();
// Emit some events.
function theEarthRotates () {
mediator.publish(ItIsANewDay);
}
async function theEarthExplodes () {
clearTimeout(dayInterval);
await disconnect(); // If components need to gracefully die, they can here
}
const dayInterval = setInterval(theEarthRotates, 1000); // That is a bit fast
setTimeout(theEarthExplodes, 20000);
```
## Documented example
- (WIP) [./docs/overview.ts](./docs/overview.ts)