Skip to content

Commit 084bb27

Browse files
author
hyperandroid
committed
* Session serialize/deserialize has an optional parameter 'from_registry'. It set, the FSM serialization will serialize
FSM's name instead of the automata definition, and deserialization, will get the automata fro the registry instead the embedded definition. * Ended session's don't have their stack emptied. It is necessary to get end state's name when `current_state_name` is called. * Finished session don't remove `state` info. Necessary to make `currentStateName` and `currentState` to give information about the last state before getting session finished.
1 parent e66d3f1 commit 084bb27

File tree

9 files changed

+76
-29
lines changed

9 files changed

+76
-29
lines changed

build/src/automata.d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export declare class SessionConsumeMessagePromise<T> {
3838
export declare class FSMRegistry {
3939
static _fsm: GenericMap<FSM>;
4040
static FSMFromId(id: string): FSM;
41-
static register(fsm_json: FSMJson): void;
41+
static register(fsm_json: FSMJson): FSM;
4242
static createSession<T>(session_controller: T, fsm_id: string, o?: SessionObserver<T>): SessionConsumeMessagePromise<T>;
4343
}
4444
export interface StateAutoTransitionElement {
@@ -124,7 +124,7 @@ export interface SerializedSession {
124124
ended: boolean;
125125
controller: any;
126126
states: SerializedSessionContext[];
127-
fsm: FSMJson;
127+
fsm: FSMJson | string;
128128
}
129129
export declare class Session<T> {
130130
_fsm: FSM;
@@ -137,9 +137,9 @@ export declare class Session<T> {
137137
constructor(session_controller: T);
138138
__initialize(fsm: FSM): SessionConsumeMessagePromise<T>;
139139
__serializeController(): any;
140-
serialize(): SerializedSession;
141-
static deserialize<T, U>(s: SerializedSession, deserializer: (sg: U) => T): Session<T>;
142-
__deserialize(s: SerializedSession): void;
140+
serialize(from_registry?: boolean): SerializedSession;
141+
static deserialize<T, U>(s: SerializedSession, deserializer: (sg: U) => T, from_registry?: boolean): Session<T>;
142+
__deserialize(s: SerializedSession, from_registry?: boolean): void;
143143
addObserver(o: SessionObserver<T>): void;
144144
/**
145145
* User side message.

build/src/automata.js

Lines changed: 23 additions & 9 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/test3.js

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

build/test/test3.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: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1+
06-16-2016 *3.0.4*
2+
------------------
3+
* Session serialize/deserialize has an optional parameter 'from_registry'. It set, the FSM serialization will serialize
4+
FSM's name instead of the automata definition, and deserialization, will get the automata fro the registry instead the
5+
embedded definition.
6+
* Ended session's don't have their stack emptied. It is necessary to get end state's name when `current_state_name`
7+
is called.
8+
9+
06-08-2016 *3.0.3*
10+
------------------
11+
* Finished session don't remove `state` info. Necessary to make `currentStateName` and `currentState` to give information
12+
about the last state before getting session finished.
13+
114
06-08-2016 *3.0.2*
215
------------------
316

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.
17+
* `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.
518
Error callbacks have been refactored to return an Error instead of string.
619

720

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.2",
3+
"version": "3.0.4",
44
"main": "./build/src/automata.js",
55
"keywords": [
66
"DFA",

src/automata.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,19 @@ export class FSMRegistry {
6363
return FSMRegistry._fsm[id];
6464
}
6565

66-
static register( fsm_json : FSMJson ) {
66+
static register( fsm_json : FSMJson ) : FSM {
67+
68+
let ret : FSM = null;
6769
try {
6870
let fsm = new FSM(fsm_json);
6971
FSMRegistry._fsm[fsm.name] = fsm;
7072
console.log("Registered Automata '"+fsm.name+"'");
73+
ret=fsm;
7174
} catch( e ) {
7275
console.error(e);
7376
}
77+
78+
return ret;
7479
}
7580

7681
static createSession<T>( session_controller : T, fsm_id : string, o? : SessionObserver<T> ) : SessionConsumeMessagePromise<T> {
@@ -408,7 +413,7 @@ export interface SerializedSession {
408413
ended : boolean,
409414
controller : any,
410415
states : SerializedSessionContext[],
411-
fsm : FSMJson
416+
fsm : FSMJson | string
412417
}
413418

414419
export class Session<T> {
@@ -452,34 +457,44 @@ export class Session<T> {
452457
return {};
453458
}
454459

455-
serialize() : SerializedSession {
460+
serialize( from_registry?:boolean ) : SerializedSession {
456461

457462
const serializedController = this.__serializeController();
458463

459464
return {
460465
ended : this._ended,
461-
fsm : this._fsm.serialize(),
466+
fsm : from_registry ? this._fsm.name : this._fsm.serialize(),
462467
states : this._states.map( st => st.serialize() ),
463468
controller: serializedController
464469
};
465470
}
466471

467-
static deserialize<T,U>( s : SerializedSession, deserializer : (sg : U) => T ) : Session<T> {
472+
static deserialize<T,U>( s : SerializedSession, deserializer : (sg : U) => T, from_registry?:boolean ) : Session<T> {
468473

469474
const controller : T = deserializer( s.controller );
470475
const session : Session<T> = new Session( controller );
471-
session.__deserialize( s );
476+
session.__deserialize( s, from_registry );
472477

473478
return session;
474479
}
475480

476-
__deserialize( s : SerializedSession ) {
481+
__deserialize( s : SerializedSession, from_registry?:boolean ) {
482+
483+
if ( !from_registry ) {
484+
this._fsm = FSMRegistry.register(s.fsm as FSMJson);
485+
} else {
486+
if ( typeof s.fsm==='string' ) {
487+
// try automata saved as string.
488+
this._fsm = FSMRegistry.FSMFromId(s.fsm as string);
489+
} else {
490+
// if not, assume it was saved as fully serialized automata, but loading it was saved as automata name.
491+
this._fsm = FSMRegistry.FSMFromId((s.fsm as FSMJson).name);
492+
}
493+
}
477494

478-
FSMRegistry.register( s.fsm );
479-
this._fsm= FSMRegistry.FSMFromId( s.fsm.name );
480495
this._ended = s.ended;
481496
this._states = s.states.map( e => {
482-
const c : State = e.current_state === s.fsm.name ?
497+
const c : State = e.current_state === this._fsm.name ?
483498
this._fsm :
484499
this._fsm._states.filter( s => s._name===e.current_state )[0];
485500
const p : State = e.prev_state === "" ?
@@ -680,7 +695,7 @@ export class Session<T> {
680695
this.__setCurrentState(next, m);
681696

682697
if (next.isFinal()) {
683-
this.__popAllStates(m);
698+
// this.__popAllStates(m);
684699
this.__endSession(m);
685700
}
686701
}

test/test3.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ Automata.CreateSession(
168168
console.log("");
169169
console.log("Sent 'bc'");
170170
session.dispatchMessage( { msgId: "bc" } );
171+
session.dispatchMessage( { msgId: "bc" } ).then(
172+
() => {},
173+
(session:Session<Controller>, err:Error) => {console.error( err.message )}
174+
);
171175
}
172176
);
173177

0 commit comments

Comments
 (0)