Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/redis/redis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {connect, RedisConnectOptions} from "https://deno.land/x/redis/mod.ts";

let redis: any;
let pub: any;
let sub: any

export const initRedis = async (options: RedisConnectOptions) => {
redis = await connect(options);
pub = await connect(options);
sub = await connect(options);
}

export const subscribe = async (channel: string) => {
const subscribe = await sub.subscribe(channel);
for await (const {channel, message} of subscribe.receive()) {
console.log({channel, message: message})
}
}

export const publish = async (channel: string, data: any) => {
console.log("publish ", data)
return pub.publish(channel, JSON.stringify(data));
}
17 changes: 17 additions & 0 deletions lib/redis/redis_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {
beforeAll,
} from "https://x.nest.land/hooked@0.1.0/mod.ts";
import {assertEquals} from "https://deno.land/std@0.74.0/testing/asserts.ts";

import {initRedis} from "./redis.ts";

beforeAll(async () => {
await initRedis({
hostname: 'localhost',
port: '6379',
})
});

Deno.test("should pub/sub works", async () => {
//TODO I couldn't write test case for pub-sub
});
3 changes: 1 addition & 2 deletions lib/tasks/task.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { v4 } from "https://deno.land/std/uuid/mod.ts";

type Task = {
export type Task = {
id: string;
name: string;
data: string;
Expand All @@ -14,7 +14,6 @@ type Handler = (payload: Data) => void;

const tasks: Map<string, Task[]> = new Map<string, Task[]>();
const handlers: Map<string, Handler> = new Map<string, Handler>();
const results: Map<string, any> = new Map<string, any>();

export function cancel(taskId: string) {
tasks.delete(taskId);
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/task_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { assert, assertEquals } from "https://deno.land/std@0.74.0/testing/asser
import { create, handle } from "./task.ts";

function delay(ms: number): Promise<void> {
return new Promise((res) => setTimeout(res, ms));
return new Promise((res:()=>any) => setTimeout(res, ms));
}

Deno.test("It should add tasks", async () => {
Expand Down