Skip to content

Commit e66d3f1

Browse files
author
hyperandroid
committed
dispatchMessage now notifies on onError callback if the session is finished. Previously it threw an error, making mandatory to wrap dispatchMessage methods with try/catch.
Error callbacks have been refactored to return an Error instead of string.
1 parent c985d3b commit e66d3f1

File tree

9 files changed

+44
-23
lines changed

9 files changed

+44
-23
lines changed

build/src/automata.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ export interface FSMJson {
2626
transition: TransitionJson[];
2727
}
2828
export declare type SessionMessageCallback = <T>(session: Session<T>, message?: Message) => void;
29-
export declare type SessionMessageCallbackError = <T>(session: Session<T>, message?: string) => void;
29+
export declare type SessionMessageCallbackError = <T>(session: Session<T>, message?: Error) => void;
3030
export declare class SessionConsumeMessagePromise<T> {
3131
_success: SessionMessageCallback;
3232
_error: SessionMessageCallbackError;
3333
constructor();
3434
then(ok: SessionMessageCallback, error?: SessionMessageCallbackError): this;
3535
__success(s: Session<T>, m: Message): void;
36-
__error(s: Session<T>, message?: string): void;
36+
__error(s: Session<T>, message?: Error): void;
3737
}
3838
export declare class FSMRegistry {
3939
static _fsm: GenericMap<FSM>;

build/src/automata.js

Lines changed: 10 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/src/automata.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/test/test1.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/test/test1.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

changelog

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
06-08-2016 *3.0.2*
2+
------------------
3+
4+
`dispatchMessage` now notifies on `onError` callback if the session is finished. Previously it threw an error, making mandatory to wrap dispatchMessage methods with try/catch.
5+
Error callbacks have been refactored to return an Error instead of string.
6+
7+
8+
04-15/2016 *3.0.1*
9+
------------------
10+
11+
* Changed error callback function to accept an Error instead of a String.
12+
113
04-15/2016 *3.0.0*
214
------------------
315

@@ -81,4 +93,4 @@ Major changes.
8193
------------------
8294

8395
* published module 1.0.4
84-
* added onEnter/onExit on subState.
96+
* added onEnter/onExit on subState.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "automata",
3-
"version": "3.0.0",
3+
"version": "3.0.2",
44
"main": "./build/src/automata.js",
55
"keywords": [
66
"DFA",

src/automata.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export interface FSMJson {
3030
}
3131

3232
export type SessionMessageCallback = <T>(session : Session<T>, message? : Message) => void;
33-
export type SessionMessageCallbackError = <T>(session : Session<T>, message? : string) => void;
33+
export type SessionMessageCallbackError = <T>(session : Session<T>, message? : Error) => void;
3434

3535
export class SessionConsumeMessagePromise<T> {
3636

@@ -50,7 +50,7 @@ export class SessionConsumeMessagePromise<T> {
5050
this._success && this._success( s, m );
5151
}
5252

53-
__error( s : Session<T>, message? : string ) {
53+
__error( s : Session<T>, message? : Error ) {
5454
this._error && this._error( s, message );
5555
}
5656
}
@@ -87,13 +87,13 @@ export class FSMRegistry {
8787
(session : Session<T>, m: Message) : void => {
8888
promise.__success( session, m );
8989
},
90-
(session : Session<T>, m : string) : void => {
90+
(session : Session<T>, m : Error) : void => {
9191
promise.__error( session, m );
9292
}
9393
);
9494
} else {
9595
setImmediate( function() {
96-
promise.__error( null, "Unkonwn automata: '"+fsm_id+"'");
96+
promise.__error( null, new Error("Unkonwn automata: '"+fsm_id+"'") );
9797
} );
9898
}
9999

@@ -498,12 +498,16 @@ export class Session<T> {
498498
* User side message.
499499
*/
500500
dispatchMessage<U extends Message>( m : U ) : SessionConsumeMessagePromise<T> {
501+
const c : SessionConsumeMessagePromise<T> = new SessionConsumeMessagePromise();
502+
501503
if ( this._ended ) {
502-
throw "Session is ended.";
504+
setTimeout( ()=> {
505+
c._error(this, new Error('Session ended'));
506+
}, 0 );
507+
} else {
508+
this._messages_controller.dispatchMessage(m, c);
503509
}
504-
505-
const c : SessionConsumeMessagePromise<T> = new SessionConsumeMessagePromise();
506-
this._messages_controller.dispatchMessage( m, c );
510+
507511
return c;
508512
}
509513

@@ -766,7 +770,7 @@ export class SessionMessageControllerMessageQueue<T> {
766770
this._session.__messageImpl(m);
767771
ret = false;
768772
} catch (e) {
769-
console.error(`consume for message '${m.msgId}' got exception: `, e);
773+
// console.error(`consume for message '${m.msgId}' got exception: `, e);
770774
this._messages_queue= [];
771775
this._callback.__error( this._session, e );
772776
ret = true;

test/test1.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ Automata.CreateSession(
8686
}
8787
);
8888
},
89-
function error( s : Session<Controller>, m : string ) {
89+
function error( s : Session<Controller>, m : Error ) {
9090

91-
console.log("Error creating Session of type Test1, reason: '"+m+"'");
91+
console.log("Error creating Session of type Test1, reason: '"+m.message+"'");
9292
}
9393
);
9494

0 commit comments

Comments
 (0)