Skip to content
This repository was archived by the owner on Apr 13, 2023. It is now read-only.

Commit e5ed08c

Browse files
committed
feat(api): add generic for action input and output
1 parent 0f26398 commit e5ed08c

File tree

6 files changed

+30
-30
lines changed

6 files changed

+30
-30
lines changed

src/driver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export * from './interfaces';
1212
*/
1313
export function makeActionsDriver(handlers: ActionHandlers = {}) {
1414

15-
async function executeAction(request: Action): Promise<ActionResult> {
15+
async function executeAction(request: Action<any>): Promise<ActionResult<any, any>> {
1616
try {
1717
// when the handler is not found the
1818
if (!handlers[request.type]) {

src/interfaces.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,35 @@ import {Stream} from 'xstream';
33
/**
44
* An action handler executes an action.
55
*/
6-
export interface ActionHandler {
6+
export interface ActionHandler<I, O> {
77
/**
88
* The handler.
99
* @param action the action
1010
* @return an optional output which can be embedded within a Promise
1111
*/
12-
(action: Action): any | void | Promise<any | void>
12+
(action: Action<I>): O | Promise<O>
1313
}
1414

1515
/**
1616
* The map of action handlers.
1717
* To be handled, the {@link Action.type} of the {@link Action} must match the key of the {@link ActionHandlers} object.
1818
*/
1919
export type ActionHandlers = {
20-
[k: string]: ActionHandler
20+
[k: string]: ActionHandler<any, any>
2121
}
2222

2323
/**
2424
* An action is a message which will be handled by an {@link ActionHandler}.
2525
*/
26-
export interface Action {
26+
export interface Action<P> {
2727
/**
2828
* The type of the message.
2929
*/
3030
type: string
3131
/**
3232
* The optional payload.
3333
*/
34-
payload?: any
34+
payload?: P
3535
/**
3636
* The category is used to select action's results via {@link ActionsSource.select}.
3737
*/
@@ -46,15 +46,15 @@ export interface Action {
4646
/**
4747
* An action result is used internally to build the source part.
4848
*/
49-
export interface ActionResult {
49+
export interface ActionResult<I, O> {
5050
/**
5151
* The request.
5252
*/
53-
request: Action
53+
request: Action<I>
5454
/**
5555
* The response related to the handled action.
5656
*/
57-
response?: any
57+
response?: O
5858
/**
5959
* The error raised during the handling of the action.
6060
*/
@@ -64,12 +64,12 @@ export interface ActionResult {
6464
/**
6565
* A stream of actions.
6666
*/
67-
export type ActionStream = Stream<Action>
67+
export type ActionStream = Stream<Action<any>>
6868

6969
/**
7070
* A stream of action result.
7171
*/
72-
export interface ActionResultStream extends Stream<ActionResult> {
72+
export interface ActionResultStream extends Stream<ActionResult<any, any>> {
7373
}
7474

7575
/**

src/source.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class ActionsSource {
6666
return action$;
6767
}
6868
return adapt(
69-
xs.fromObservable<Action>(action$).map(action => {
69+
xs.fromObservable<Action<any>>(action$).map(action => {
7070
action.namespace = action.namespace || [];
7171
action.namespace.unshift(scope);
7272
return action;
@@ -81,7 +81,7 @@ export class ActionsSource {
8181
* @return a new {@link ActionsSource}
8282
*/
8383
public filter(
84-
predicate: (action: Action) => boolean,
84+
predicate: (action: Action<any>) => boolean,
8585
scope?: string
8686
): ActionsSource {
8787

test/driver.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import run from '@cycle/run';
22
import xs, {Stream} from 'xstream';
33
import {Action, ActionResult, ActionsSource, makeActionsDriver, SelectedResults} from '../src/driver';
44

5-
function syncAction(action: Action): any {
5+
function syncAction(action: Action<string>): any {
66
return action.payload
77
}
88

9-
async function asyncAction(action: Action): Promise<any> {
9+
async function asyncAction(action: Action<string>): Promise<any> {
1010
return syncAction(action);
1111
}
1212

@@ -15,11 +15,11 @@ function failedAction(): any {
1515
}
1616

1717
describe('driver', () => {
18-
let actions: Array<Action>;
19-
let actions$: Stream<Action>;
18+
let actions: Array<Action<any>>;
19+
let actions$: Stream<Action<any>>;
2020
let readResponses: Array<any>;
2121
let readErrors: Array<any>;
22-
let readResults: Array<ActionResult>;
22+
let readResults: Array<ActionResult<any, any>>;
2323

2424
function read({result$, response$, error$}: SelectedResults) {
2525
response$.subscribe({
@@ -33,7 +33,7 @@ describe('driver', () => {
3333
}
3434
});
3535
result$.subscribe({
36-
next(result: ActionResult) {
36+
next(result: ActionResult<any, any>) {
3737
readResults.push(result)
3838
}
3939
})
@@ -173,7 +173,7 @@ describe('driver', () => {
173173
});
174174

175175
return {
176-
ACTIONS: xs.of<Action>({
176+
ACTIONS: xs.of<Action<any>>({
177177
type: 'unknown'
178178
})
179179
};

test/source.isolateSink.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {Action, ActionsSource, ActionStream} from '../src/driver';
22
import xs from 'xstream';
33

44
describe('ActionsSource.isolateSink', () => {
5-
let actions: Array<Action>;
5+
let actions: Array<Action<any>>;
66
let streamOfActions: ActionStream;
77

88
beforeEach(() => {
@@ -15,7 +15,7 @@ describe('ActionsSource.isolateSink', () => {
1515

1616
it('should isolate source', (done) => {
1717
const isolatedStream = ActionsSource.isolateSink(streamOfActions, 'ns1');
18-
const readActions: Array<Action> = [];
18+
const readActions: Array<Action<any>> = [];
1919
isolatedStream.subscribe({
2020
next(action) {
2121
readActions.push(action);
@@ -33,7 +33,7 @@ describe('ActionsSource.isolateSink', () => {
3333

3434
it('should not isolate source when no scope', (done) => {
3535
const isolatedStream = ActionsSource.isolateSink(streamOfActions, null);
36-
const readActions: Array<Action> = [];
36+
const readActions: Array<Action<any>> = [];
3737
isolatedStream.subscribe({
3838
next(action) {
3939
readActions.push(action);

test/source.isolateSource.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import {Action, ActionResult, ActionResultStream, ActionsSource} from '../src/dr
22
import xs from 'xstream';
33

44
interface Transaction {
5-
request: Action,
6-
result: ActionResult
5+
request: Action<any>,
6+
result: ActionResult<any, any>
77
}
88

99
describe('ActionsSource.isolateSource', () => {
10-
let actions: Array<Action>;
10+
let actions: Array<Action<any>>;
1111
let transactions: Array<Transaction>;
1212
let streamOfActionResults: ActionResultStream;
1313

@@ -32,10 +32,10 @@ describe('ActionsSource.isolateSource', () => {
3232
it('should isolate source', (done) => {
3333
const main = new ActionsSource(streamOfActionResults);
3434

35-
const readActionResults: Array<ActionResult> = [];
35+
const readActionResults: Array<ActionResult<any, any>> = [];
3636

3737
ActionsSource.isolateSource(main, 'ns1').select().result$.subscribe({
38-
next(result: ActionResult) {
38+
next(result: ActionResult<any, any>) {
3939
readActionResults.push(result);
4040
}
4141
});
@@ -51,10 +51,10 @@ describe('ActionsSource.isolateSource', () => {
5151
it('should not isolate source when no scope', (done) => {
5252
const main = new ActionsSource(streamOfActionResults);
5353

54-
const readActionResults: Array<ActionResult> = [];
54+
const readActionResults: Array<ActionResult<any, any>> = [];
5555

5656
ActionsSource.isolateSource(main, null).select().result$.subscribe({
57-
next(result: ActionResult) {
57+
next(result: ActionResult<any, any>) {
5858
readActionResults.push(result);
5959
}
6060
});

0 commit comments

Comments
 (0)