This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ceb-messaging-builder-inversion): add a builder to inject a bus …
…from a container
- Loading branch information
Showing
18 changed files
with
540 additions
and
23 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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 @@ | ||
# @tmorin/ceb-messaging-builder-inversion | ||
|
||
[![npm version](https://badge.fury.io/js/%40tmorin%2Fceb-messaging-builder-inversion.svg)](https://badge.fury.io/js/%40tmorin%2Fceb-messaging-builder-inversion) | ||
[![api](https://img.shields.io/badge/-api-informational.svg)](https://tmorin.github.io/ceb/api/modules/_tmorin_ceb_messaging_builder_inversion.html) | ||
|
||
> The package is part of the `<ceb/>` library. | ||
> It provides a builder to resolve and inject a bus from a container and then subscribe to event. | ||
## Install | ||
|
||
The NPM package is compliant [CommonJs](https://flaviocopes.com/commonjs) and [ES Module](https://flaviocopes.com/es-modules). | ||
|
||
```bash | ||
npm install @tmorin/ceb-messaging-builder-inversion | ||
``` | ||
|
||
## License | ||
|
||
Released under the [MIT license]. | ||
|
||
[Custom Elements (v1)]: https://html.spec.whatwg.org/multipage/custom-elements.html | ||
[MIT license]: http://opensource.org/licenses/MIT |
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('../../karma.conf') |
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,62 @@ | ||
{ | ||
"name": "@tmorin/ceb-messaging-builder-inversion", | ||
"version": "4.0.3-alpha.13", | ||
"license": "MIT", | ||
"description": "The package is part of the `<ceb/>` library. It provides a builder to resolve and inject a bus from a container and then subscribe to event.", | ||
"keywords": [ | ||
"custom-element-builder", | ||
"custom-elements-v1", | ||
"custom-elements", | ||
"custom-element", | ||
"typescript", | ||
"typescript-library", | ||
"javascript-library", | ||
"messaging", | ||
"cqrs", | ||
"bus", | ||
"event-bus", | ||
"message-bus" | ||
], | ||
"homepage": "https://tmorin.github.io/ceb", | ||
"bugs": { | ||
"url": "https://github.com/tmorin/ceb/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:tmorin/ceb.git" | ||
}, | ||
"author": { | ||
"name": "Thibault Morin", | ||
"url": "https://tmorin.github.io" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"exports": { | ||
".": { | ||
"import": "./dist/mjs/index.js", | ||
"require": "./dist/cjs/index.js" | ||
} | ||
}, | ||
"types": "dist/types/index.d.ts", | ||
"main": "dist/cjs/index.js", | ||
"module": "dist/mjs/index.js", | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "npm run build:lib && npm run build:module && npm run build:fix", | ||
"build:fix": "../../scripts/fix-dist.js", | ||
"build:lib": "tsc -p tsconfig.build.json --module CommonJS --outDir dist/cjs --declaration --declarationDir dist/types", | ||
"build:module": "tsc -p tsconfig.build.json --module ESNext --outDir dist/mjs", | ||
"test": "karma start --single-run --no-auto-watch --browsers FirefoxHeadless,ChromeHeadless", | ||
"test:watch": "karma start --no-single-run --auto-watch --browsers Firefox" | ||
}, | ||
"dependencies": { | ||
"@tmorin/ceb-core": "^4.0.3-alpha.13", | ||
"@tmorin/ceb-inversion": "^4.0.3-alpha.13", | ||
"@tmorin/ceb-messaging-core": "^4.0.3-alpha.13", | ||
"@tmorin/ceb-messaging-core-builder": "^4.0.3-alpha.13", | ||
"@tmorin/ceb-utilities": "^4.0.0" | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
packages/ceb-messaging-builder-inversion/src/__TEST/fixtures.ts
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,68 @@ | ||
import { | ||
Bus, | ||
ExecutionHandler, | ||
Handler, | ||
MessageAction, | ||
MessageConstructor, | ||
MessageEvent, | ||
MessageHeaders, | ||
MessageKind, | ||
MessageResult, | ||
SubscribeOptions, | ||
Subscription, | ||
SubscriptionListener | ||
} from "@tmorin/ceb-messaging-core"; | ||
|
||
export class NotImplementedBus implements Bus { | ||
|
||
public subscribeCalls: Array<[any, any, any]> = [] | ||
|
||
publish<E extends MessageEvent<any, MessageHeaders>>(event: E): Promise<void> { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
subscribe<E extends MessageEvent<any, MessageHeaders>>(EventType: string | MessageConstructor<E>, listener: SubscriptionListener<E>, options?: SubscribeOptions): Subscription { | ||
this.subscribeCalls.push([EventType, listener, options]) | ||
return { | ||
unsubscribe() { | ||
} | ||
} | ||
} | ||
|
||
execute<A extends MessageAction<any, MessageHeaders>, R extends MessageResult<any, MessageHeaders>>(action: any, ResultType?: any, options?: any): Promise<void> | Promise<R> { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
handle<A extends MessageAction<any, MessageHeaders>, R extends MessageResult<any, MessageHeaders>>(ActionType: string | MessageConstructor<A>, ResultType: string | MessageConstructor<R>, handler: ExecutionHandler<A, R>): Handler { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
emit(event: any, err?: any, ...rest: any[]): void { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
on(event: any, listener: any): this { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
off(event?: any, listener?: any): this { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
dispose(): Promise<void> { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
} | ||
|
||
export class EventA implements MessageEvent<string> { | ||
constructor( | ||
readonly body: string, | ||
readonly headers: MessageHeaders = { | ||
messageType: EventA.name, | ||
messageId: `${EventA.name}-${Date.now()}` | ||
}, | ||
readonly kind: MessageKind = MessageKind.event, | ||
) { | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
packages/ceb-messaging-builder-inversion/src/builder.decorate.spec.ts
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,58 @@ | ||
import {assert} from "chai"; | ||
import {ElementBuilder} from "@tmorin/ceb-core"; | ||
import {Bus, BusSymbol} from "@tmorin/ceb-messaging-core"; | ||
import {Container, ContainerBuilder, OnlyConfigureModule} from "@tmorin/ceb-inversion"; | ||
import {BusInversionBuilder} from "./builder"; | ||
import {BusInversionBuilderModule} from "./module"; | ||
import {EventA, NotImplementedBus} from "./__TEST/fixtures"; | ||
|
||
describe("messaging/bus/inversion/builder/decorate", function () { | ||
let sandbox: HTMLDivElement | ||
const tagName = "messaging-bus-inversion-builder-decorate" | ||
let container: Container | ||
let bus: NotImplementedBus | ||
let testElement: TestElement | ||
|
||
@ElementBuilder.get(TestElement).name(tagName).decorate() | ||
class TestElement extends HTMLElement { | ||
@BusInversionBuilder.get().decorate() | ||
bus?: Bus | ||
|
||
@BusInversionBuilder.get().subscribe().decorate() | ||
onEventA(event: EventA) { | ||
} | ||
|
||
@BusInversionBuilder.get().subscribe().type(EventA).decorate() | ||
onEventABis(event: EventA) { | ||
} | ||
} | ||
|
||
before(async function () { | ||
bus = new NotImplementedBus() | ||
container = await ContainerBuilder.get() | ||
.module(OnlyConfigureModule.create(async function () { | ||
this.registry.registerValue(BusSymbol, bus) | ||
})) | ||
.module(new BusInversionBuilderModule()) | ||
.build() | ||
.initialize() | ||
sandbox = document.body.appendChild(document.createElement('div')) | ||
testElement = sandbox.appendChild(document.createElement(tagName) as TestElement) | ||
}) | ||
|
||
after(async function () { | ||
await container.dispose() | ||
}) | ||
|
||
it("should resolve container with default settings", function () { | ||
assert.property(testElement, "bus") | ||
assert.equal<any>(testElement.bus, bus) | ||
}) | ||
|
||
it("should subscribe", function () { | ||
assert.equal(bus.subscribeCalls.length, 2) | ||
assert.equal(bus.subscribeCalls[0][0], "EventA") | ||
assert.equal(bus.subscribeCalls[1][0], EventA) | ||
}) | ||
|
||
}) |
46 changes: 46 additions & 0 deletions
46
packages/ceb-messaging-builder-inversion/src/builder.listener.spec.ts
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,46 @@ | ||
import {assert} from "chai"; | ||
import {ElementBuilder} from "@tmorin/ceb-core"; | ||
import {Bus, BusSymbol} from "@tmorin/ceb-messaging-core"; | ||
import {Container, ContainerBuilder, OnlyConfigureModule} from "@tmorin/ceb-inversion"; | ||
import {BusInversionBuilder} from "./builder"; | ||
import {BusInversionBuilderModule} from "./module"; | ||
import {EventA, NotImplementedBus} from "./__TEST/fixtures"; | ||
|
||
describe("messaging/bus/inversion/builder/listener", function () { | ||
let sandbox: HTMLDivElement | ||
const tagName = "messaging-bus-inversion-builder-listener" | ||
let container: Container | ||
let bus: NotImplementedBus | ||
let testElement: TestElement | ||
|
||
class TestElement extends HTMLElement { | ||
bus?: Bus | ||
} | ||
|
||
before(async function () { | ||
bus = new NotImplementedBus() | ||
container = await ContainerBuilder.get() | ||
.module(OnlyConfigureModule.create(async function () { | ||
this.registry.registerValue(BusSymbol, bus) | ||
})) | ||
.module(new BusInversionBuilderModule()) | ||
.build() | ||
.initialize() | ||
ElementBuilder.get(TestElement).name(tagName).builder( | ||
BusInversionBuilder.get().subscribe(EventA, () => { | ||
}), | ||
).register() | ||
sandbox = document.body.appendChild(document.createElement('div')) | ||
testElement = sandbox.appendChild(document.createElement(tagName)) | ||
}) | ||
|
||
after(async function () { | ||
await container.dispose() | ||
}) | ||
|
||
it("should subscribe", function () { | ||
assert.equal(bus.subscribeCalls.length, 1) | ||
assert.equal(bus.subscribeCalls[0][0], EventA) | ||
}) | ||
|
||
}) |
Oops, something went wrong.