Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pubsub): add pub/sub package (#1936)
- Loading branch information
1 parent
45dd642
commit 3a2facb
Showing
12 changed files
with
184 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require("../../.babel.node")({ path: __dirname }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
```` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const base = require("../../jest.config.base"); | ||
|
||
module.exports = { | ||
...base({ path: __dirname }) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"extends": "../../tsconfig", | ||
"references": [], | ||
"include": ["./src", "./__tests__"], | ||
"compilerOptions": { | ||
"paths": { | ||
"~/*": ["./src/*"] | ||
}, | ||
"baseUrl": "." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const { watchPackage, buildPackage } = require("@webiny/project-utils"); | ||
|
||
module.exports = { | ||
commands: { | ||
build: buildPackage, | ||
watch: watchPackage | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters