0.0.12 • Published 5 years ago

topicsend v0.0.12

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

Gitpod Ready-to-Code

topicsend

npm install topicsend

Sample tests;

import { Chat } from "./";
import { mediate } from "./Chat"
import { Message, Mediator } from "./Mediator";

class Users {
	users(): PromiseLike<string[]> {
		throw new Error("Not implmented");
	}
}

test('full static', async () => {
	const chat = new Chat();

	const topic = chat.topic(Users);

	chat.subscribe(Users, {
		users() {
			return ["john", "bob"]
		}
	})

	let users = await topic.users();

	expect(users).toHaveLength(2);
	expect(users[0]).toBe("john");

	users = await chat.send("Users:users");

	expect(users).toHaveLength(2);
	expect(users[0]).toBe("john");
})

test('static topic - dynamic handler', async () => {
	const chat = new Chat();

	const topic = chat.topic(Users);

	chat.handle("Users", "users", () => ["john", "bob"]);

	const users = await topic.users();

	expect(users).toHaveLength(2);
	expect(users[0]).toBe("john");
})

test('dynamic', async () => {
	const chat = new Chat();

	const topic = chat.topic("Users");

	chat.handle("Users:users", () => ["john", "bob"]);

	const users = await topic.users();

	expect(users).toHaveLength(2);
	expect(users[0]).toBe("john");
})


test('global', async () => {
	const chat = mediate();

	const topic = chat.topic("Users");

	chat.handle("Users:users", () => ["john", "bob"]);

	const users = await topic.users();

	expect(users).toHaveLength(2);
	expect(users[0]).toBe("john");
})



test('Mediator', async () => {
	class LoginResponse {
        success = true;
    }

    class LoginRequest extends Message<LoginResponse> {

    }

    const mediator = Mediator.instance();

    mediator.handle(LoginRequest, (m) => new LoginResponse());
    mediator.before(LoginRequest, (m) => console.log("Before"));
    mediator.after(LoginRequest, (m) => console.log("After"));

    const result = await mediator.send(new LoginRequest());

    expect(result.success).toBeTruthy();

})
0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.3

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago