Skip to content

Commit

Permalink
feat(pubsub): add pub/sub package (#1936)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunozoric committed Sep 29, 2021
1 parent 45dd642 commit 3a2facb
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/pubsub/.babelrc.js
@@ -0,0 +1 @@
module.exports = require("../../.babel.node")({ path: __dirname });
21 changes: 21 additions & 0 deletions packages/pubsub/LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Webiny

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
20 changes: 20 additions & 0 deletions packages/pubsub/README.md
@@ -0,0 +1,20 @@
# @webiny/pubsub

[![](https://img.shields.io/npm/dw/@webiny/pubsub.svg)](https://www.npmjs.com/package/@webiny/pubsub)
[![](https://img.shields.io/npm/v/@webiny/pubsub.svg)](https://www.npmjs.com/package/@webiny/pubsub)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)

## Install

```
yarn add @webiny/pubsub
```


## Testing

### Command
````
yarn test packages/pubsub
````
21 changes: 21 additions & 0 deletions packages/pubsub/__tests__/pubsub.test.ts
@@ -0,0 +1,21 @@
import { createTopic } from "../src";

describe("pubsub", () => {
test("create a topic, subscribe to it, and publish an event", async () => {
const topic = createTopic<number>();

const values = [];

topic.subscribe(eventValue => {
values.push(eventValue);
});

topic.subscribe(eventValue => {
values.push(eventValue);
});

await topic.publish(1);

expect(values).toEqual([1, 1]);
});
});
5 changes: 5 additions & 0 deletions packages/pubsub/jest.config.js
@@ -0,0 +1,5 @@
const base = require("../../jest.config.base");

module.exports = {
...base({ path: __dirname })
};
29 changes: 29 additions & 0 deletions packages/pubsub/package.json
@@ -0,0 +1,29 @@
{
"name": "@webiny/pubsub",
"version": "5.14.0",
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/webiny/webiny-js.git",
"directory": "packages/pubsub"
},
"description": "A simple implementation of publish-subscribe pattern based on topic objects instead of strings.",
"license": "MIT",
"publishConfig": {
"access": "public",
"directory": "dist"
},
"devDependencies": {
"@babel/cli": "^7.12.10",
"@babel/core": "^7.5.5",
"@webiny/cli": "^5.13.0",
"@webiny/project-utils": "^5.13.0",
"rimraf": "^3.0.2",
"ttypescript": "^1.5.12",
"typescript": "^4.1.3"
},
"scripts": {
"build": "yarn webiny run build",
"watch": "yarn webiny run watch"
}
}
22 changes: 22 additions & 0 deletions packages/pubsub/src/index.ts
@@ -0,0 +1,22 @@
import { Subscriber, Topic } from "~/types";

export const createTopic = <TEvent = any>(topicName?: string): Topic<TEvent> => {
const subscribers: Subscriber<TEvent>[] = [];

return {
getTopicName() {
return topicName || "unknown";
},
subscribe(cb: Subscriber<TEvent>) {
subscribers.push(cb);
},
getSubscribers() {
return subscribers;
},
async publish(event) {
for (const cb of subscribers) {
await cb(event);
}
}
};
};
15 changes: 15 additions & 0 deletions packages/pubsub/src/types.ts
@@ -0,0 +1,15 @@
export interface Subscriber<TEvent> {
(event: TEvent): void | Promise<void>;
}

export type Event = Record<string, any>;

export interface Topic<TEvent = Event> {
getTopicName(): string;

subscribe(cb: (event: TEvent) => void | Promise<void>): void;

getSubscribers(): Subscriber<TEvent>[];

publish(event?: TEvent): Promise<void>;
}
15 changes: 15 additions & 0 deletions packages/pubsub/tsconfig.build.json
@@ -0,0 +1,15 @@
{
"extends": "../../tsconfig.build.json",
"include": ["./src"],
"exclude": ["node_modules", "./dist"],
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
"declarationDir": "./dist",
"paths": {
"~/*": ["./src/*"]
},
"baseUrl": "."
},
"references": []
}
11 changes: 11 additions & 0 deletions packages/pubsub/tsconfig.json
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig",
"references": [],
"include": ["./src", "./__tests__"],
"compilerOptions": {
"paths": {
"~/*": ["./src/*"]
},
"baseUrl": "."
}
}
8 changes: 8 additions & 0 deletions packages/pubsub/webiny.config.js
@@ -0,0 +1,8 @@
const { watchPackage, buildPackage } = require("@webiny/project-utils");

module.exports = {
commands: {
build: buildPackage,
watch: watchPackage
}
};
18 changes: 16 additions & 2 deletions yarn.lock
Expand Up @@ -10235,7 +10235,7 @@ __metadata:
languageName: unknown
linkType: soft

"@webiny/cli@^5.14.0, @webiny/cli@workspace:packages/cli":
"@webiny/cli@^5.13.0, @webiny/cli@^5.14.0, @webiny/cli@workspace:packages/cli":
version: 0.0.0-use.local
resolution: "@webiny/cli@workspace:packages/cli"
dependencies:
Expand Down Expand Up @@ -10611,7 +10611,7 @@ __metadata:
languageName: unknown
linkType: soft

"@webiny/project-utils@^5.14.0, @webiny/project-utils@workspace:packages/project-utils":
"@webiny/project-utils@^5.13.0, @webiny/project-utils@^5.14.0, @webiny/project-utils@workspace:packages/project-utils":
version: 0.0.0-use.local
resolution: "@webiny/project-utils@workspace:packages/project-utils"
dependencies:
Expand Down Expand Up @@ -10676,6 +10676,20 @@ __metadata:
languageName: unknown
linkType: soft

"@webiny/pubsub@workspace:packages/pubsub":
version: 0.0.0-use.local
resolution: "@webiny/pubsub@workspace:packages/pubsub"
dependencies:
"@babel/cli": ^7.12.10
"@babel/core": ^7.5.5
"@webiny/cli": ^5.13.0
"@webiny/project-utils": ^5.13.0
rimraf: ^3.0.2
ttypescript: ^1.5.12
typescript: ^4.1.3
languageName: unknown
linkType: soft

"@webiny/pulumi-sdk@^5.14.0, @webiny/pulumi-sdk@workspace:packages/pulumi-sdk":
version: 0.0.0-use.local
resolution: "@webiny/pulumi-sdk@workspace:packages/pulumi-sdk"
Expand Down

0 comments on commit 3a2facb

Please sign in to comment.