-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Task Namespace in realtime-api (#471)
* new playground for task * add support for contexts * prettier * add task namespace * e2e tests for task * changesets * cleanup * add task types * type task worker * remove Proxy usage in createTaskObject * rm debug in e2e tasks * solved fixme * rename to task.received * drop Job and expose send * fixes * update changeset from patch to minor
- Loading branch information
Showing
21 changed files
with
366 additions
and
8 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,7 @@ | ||
--- | ||
'@sw-internal/e2e-realtime-api': minor | ||
'@sw-internal/playground-realtime-api': minor | ||
'@signalwire/core': minor | ||
--- | ||
|
||
[internal] Add playground and e2e tests for Task namespace |
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 @@ | ||
--- | ||
'@signalwire/realtime-api': minor | ||
--- | ||
|
||
Add `Task` namespace |
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 { Task } from '@signalwire/realtime-api' | ||
import { createTestRunner } from './utils' | ||
|
||
const handler = () => { | ||
return new Promise<number>(async (resolve, reject) => { | ||
const context = 'task-e2e' | ||
const jobPayload = { | ||
id: Date.now(), | ||
item: 'foo', | ||
} | ||
|
||
const client = new Task.Client({ | ||
host: process.env.RELAY_HOST as string, | ||
project: process.env.RELAY_PROJECT as string, | ||
token: process.env.RELAY_TOKEN as string, | ||
contexts: [context], | ||
}) | ||
|
||
client.on('task.received', (payload) => { | ||
if (payload.id === jobPayload.id && payload.item === 'foo') { | ||
return resolve(0) | ||
} | ||
console.error('Invalid payload on `task.received`', payload) | ||
return reject(4) | ||
}) | ||
|
||
await Task.send({ | ||
host: process.env.RELAY_HOST as string, | ||
project: process.env.RELAY_PROJECT as string, | ||
token: process.env.RELAY_TOKEN as string, | ||
context, | ||
message: jobPayload, | ||
}) | ||
}) | ||
} | ||
|
||
async function main() { | ||
const runner = createTestRunner({ | ||
name: 'Task E2E', | ||
testHandler: handler, | ||
}) | ||
|
||
await runner.run() | ||
} | ||
|
||
main() |
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,28 @@ | ||
import { Task } from '@signalwire/realtime-api' | ||
|
||
const client = new Task.Client({ | ||
host: process.env.HOST || 'relay.swire.io', | ||
project: process.env.PROJECT as string, | ||
token: process.env.TOKEN as string, | ||
|
||
contexts: ['office'], | ||
|
||
debug: { | ||
logWsTraffic: true, | ||
}, | ||
}) | ||
|
||
client.on('task.received', (payload) => { | ||
console.log('Task Received', payload) | ||
}) | ||
|
||
setTimeout(async () => { | ||
console.log('Sending to the client..') | ||
await Task.send({ | ||
host: process.env.HOST || 'relay.swire.io', | ||
project: process.env.PROJECT as string, | ||
token: process.env.TOKEN as string, | ||
context: 'office', | ||
message: { yo: ['bro', 1, true] }, | ||
}) | ||
}, 2000) |
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
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,45 @@ | ||
import type { OnlyStateProperties, OnlyFunctionProperties } from '..' | ||
|
||
export type TaskReceivedEventName = 'task.received' | ||
|
||
export type TaskEventNames = TaskReceivedEventName | ||
|
||
export interface TaskContract {} | ||
|
||
export type TaskEntity = OnlyStateProperties<TaskContract> | ||
export type TaskMethods = Omit< | ||
OnlyFunctionProperties<TaskContract>, | ||
'subscribe' | 'unsubscribe' | 'updateToken' | ||
> | ||
|
||
/** | ||
* ========== | ||
* ========== | ||
* Server-Side Events | ||
* ========== | ||
* ========== | ||
*/ | ||
|
||
/** | ||
* 'queuing.relay.tasks' | ||
*/ | ||
export interface TaskInboundEvent { | ||
event_type: 'queuing.relay.tasks' | ||
context: string | ||
message: Record<string, unknown> | ||
timestamp: number | ||
space_id: string | ||
project_id: string | ||
} | ||
|
||
export type TaskEvent = TaskInboundEvent | ||
|
||
/** | ||
* TODO: update MapToPubSubShape in another PR | ||
* not used MapToPubSubShape because queuing.relay.tasks | ||
* has a different shape | ||
*/ | ||
export type TaskAction = { | ||
type: TaskReceivedEventName | ||
payload: TaskInboundEvent | ||
} |
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,42 @@ | ||
import type { ConsumerContract, BaseComponentOptions } from '@signalwire/core' | ||
import { connect, BaseConsumer } from '@signalwire/core' | ||
import type { RealTimeTaskApiEvents } from '../types' | ||
import { RealtimeClient } from '../client/index' | ||
import { taskWorker } from './workers' | ||
|
||
export interface Task extends ConsumerContract<RealTimeTaskApiEvents> { | ||
/** @internal */ | ||
_session: RealtimeClient | ||
} | ||
|
||
/** @internal */ | ||
class TaskAPI extends BaseConsumer<RealTimeTaskApiEvents> { | ||
constructor(options: BaseComponentOptions<RealTimeTaskApiEvents>) { | ||
super(options) | ||
|
||
this.setWorker('taskWorker', { | ||
worker: taskWorker, | ||
}) | ||
this.attachWorkers() | ||
this._attachListeners('') | ||
} | ||
} | ||
|
||
/** @internal */ | ||
export const createTaskObject = ( | ||
params: BaseComponentOptions<RealTimeTaskApiEvents> | ||
): Task => { | ||
const task = connect<RealTimeTaskApiEvents, TaskAPI, Task>({ | ||
store: params.store, | ||
Component: TaskAPI, | ||
componentListeners: { | ||
errors: 'onError', | ||
responses: 'onSuccess', | ||
}, | ||
})(params) | ||
|
||
return task | ||
} | ||
|
||
export * from './TaskClient' | ||
export * from './send' |
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,12 @@ | ||
import type { Task } from './Task' | ||
|
||
export interface TaskClientDocs extends Task { | ||
new (opts: { | ||
/** SignalWire project id, e.g. `a10d8a9f-2166-4e82-56ff-118bc3a4840f` */ | ||
project: string | ||
/** SignalWire project token, e.g. `PT9e5660c101cd140a1c93a0197640a369cf5f16975a0079c9` */ | ||
token: string | ||
/** SignalWire contexts, e.g. 'home', 'office'... */ | ||
contexts: string[] | ||
}): this | ||
} |
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 type { AssertSameType, UserOptions } from '@signalwire/core' | ||
import { setupClient, clientConnect } from '../client/index' | ||
import { TaskClientDocs } from './TaskClient.docs' | ||
import type { Task } from './Task' | ||
import { createTaskObject } from './Task' | ||
|
||
interface TaskClientMain extends Task { | ||
new (opts: TaskClientOptions): this | ||
} | ||
|
||
interface TaskClient extends AssertSameType<TaskClientMain, TaskClientDocs> {} | ||
|
||
/** @ignore */ | ||
export interface TaskClientOptions | ||
extends Omit<UserOptions, '_onRefreshToken'> { | ||
contexts: string[] | ||
} | ||
|
||
/** @ignore */ | ||
const TaskClient = function (options?: TaskClientOptions) { | ||
const { client, store, emitter } = setupClient(options) | ||
|
||
const task = createTaskObject({ | ||
store, | ||
emitter, | ||
}) | ||
|
||
const taskOn: TaskClient['on'] = (...args) => { | ||
clientConnect(client) | ||
|
||
return task.on(...args) | ||
} | ||
const taskOnce: TaskClient['once'] = (...args) => { | ||
clientConnect(client) | ||
|
||
return task.once(...args) | ||
} | ||
|
||
const interceptors = { | ||
on: taskOn, | ||
once: taskOnce, | ||
_session: client, | ||
} as const | ||
|
||
return new Proxy<Omit<TaskClient, 'new'>>(task, { | ||
get(target, prop, receiver) { | ||
if (prop in interceptors) { | ||
// @ts-expect-error | ||
return interceptors[prop] | ||
} | ||
|
||
return Reflect.get(target, prop, receiver) | ||
}, | ||
}) | ||
// For consistency with other constructors we'll make TS force the use of `new` | ||
} as unknown as { new (options?: TaskClientOptions): TaskClient } | ||
|
||
export { TaskClient as Client } |
Oops, something went wrong.