Skip to content

Instantly share code, notes, and snippets.

@enriched
Created May 23, 2016 18:07
Show Gist options
  • Save enriched/189539b38eab840a05b4b74690b7fc8a to your computer and use it in GitHub Desktop.
Save enriched/189539b38eab840a05b4b74690b7fc8a to your computer and use it in GitHub Desktop.
Typescript definitions for durable
var durable: DurableStatic = require('durable');
export {durable};
export interface DurableStatic {
state: <M extends EventBase, S extends StateBase>(name: string) => State<M, S>;
statechart: <M extends EventBase, S extends StateBase>(name: string) => Statechart<M, S>;
stage: <M extends EventBase, S extends StateBase>(name: string) => Stage<M, S>;
flowchart: <M extends EventBase, S extends StateBase>(name: string) => Flowchart<M, S>;
ruleset: <M extends EventBase, S extends StateBase>(name: string) => Ruleset<M, S>;
runAll: (databases?: DBConnection[], port?: number, basePath?: string, stateCacheSize?: number) => void;
run: (rulesetDefinitions, start, databases?: DBConnection[], port?: number, basePath?: string, stateCacheSize?: number) => void;
}
///////////////////////////////////////////////////////////////////////////////
// Typescript Specific
///////////////////////////////////////////////////////////////////////////////
/**
* Extend this interface when creating new Message (M) and State(S) interfaces
* when sending data to the durable_rules inference cache.
*
* The interface that extends this should have all of its keys have a Term type
* with the intended primitive type as the type argument, eg.
*
* interface ExampleMessage extends DurableEventBase {
* amount: Term<number>;
* }
*/
export interface EventBase {
id: Term<string>;
sid: Term<string>;
}
export interface StateBase {
sid: Term<string>;
}
///////////////////////////////////////////////////////////////////////////////
// Flow Structures
///////////////////////////////////////////////////////////////////////////////
// Shared Functionality
//-----------------------------------------------------------------------------
export interface RuleCreator<M, S> extends Definable {
add(...args: number[]): ArithmeticExpression;
sub(...args: number[]): ArithmeticExpression;
mul(...args: number[]): ArithmeticExpression;
div(...args: number[]): ArithmeticExpression;
and(...args: Rule[]): LogicExpresion;
or(...args: Rule[]): LogicExpresion;
/**
* This can alias things into the context
* (eg. c.first = m.amount.gt(100) with have the event with amount > 100 in c.first)
* */
c: any;
/** Event and Fact changes */
m: M;
/** Context changes */
s: S;
all(...args: Rule[]): Rule;
any(...args: Rule[]): Rule;
none(exp): Rule;
/** The priority of the rule currently being defined */
pri(pri): Rule;
count(count: number): Rule;
span(span: number): Rule;
cap(cap): Rule;
timeout(name: string): Rule;
}
export interface Named {
getName(): string;
}
export interface Definable {
define(): any;
}
export type RuleOrAction<M, S> = Rule | RuleAction<M, S>;
// Ruleset
//-----------------------------------------------------------------------------
export interface Ruleset<M, S> extends RuleCreator<M, S>, Definable, Named {
getStart();
/** Rules followed by a callback to be executed when all Rules are true */
whenAll(...rules: RuleOrAction<M,S>[]): Parallel<M, S>;
/** Rules followed by a callback to be executed when one Rule is true */
whenAny(...rules: RuleOrAction<M,S>[]): Parallel<M, S>;
timeout(name: string);
whenStart(func: HostCB<M, S>);
}
export interface Parallel<M,S> {
ruleset(name: string): Ruleset<M,S>;
statechart(name: string): Statechart<M,S>;
flowchart(name: string): Flowchart<M,S>
}
// Statechart
//-----------------------------------------------------------------------------
export interface Statechart<M, S> extends RuleCreator<M, S>, Definable, Named {
getStart();
state(name: string): State<M, S>;
whenStart(cb: HostCB<M, S>);
}
export interface To<M, S> extends Definable, Named {
/** Rules followed by a callback to be executed when all Rules are true */
whenAll(...rules: RuleOrAction<M, S>[]);
/** Rules followed by a callback to be executed when one Rule is true */
whenAny(...rules: RuleOrAction<M, S>[]);
}
export interface State<M, S> extends RuleCreator<M, S>, Definable, Named {
to(stateName, func): To<M, S>;
state(name: string): State<M, S>;
}
// Flowchart
//-----------------------------------------------------------------------------
export interface Flowchart<M, S> extends RuleCreator<M, S>, Definable, Named {
getStart();
stage(name: string): Stage<M, S>;
whenStart(func: HostCB<M, S>);
}
export interface Stage<M, S> extends RuleCreator<M, S>, Definable, Named {
run(a: RuleAction<M,S>);
ruleset<M,S>(name: string): Ruleset<M, S>;
statechart<M,S>(name: string): Statechart<M, S>;
flowchart<M,S>(name: string): Flowchart<M, S>;
to(stageName: string): To<M, S>;
}
///////////////////////////////////////////////////////////////////////////////
// Rule Definition
///////////////////////////////////////////////////////////////////////////////
export interface Rule extends Definable { }
export type Term<T> = T & AliasTerm<T>;
export interface AliasTerm<T> extends Rule {
gt(rvalue: T): this;
gte(rvalue: T): this;
lt(rvalue: T): this;
lte(rvalue: T): this;
eq(rvalue: T): this;
neq(rvalue: T): this;
ex(): this;
nex(): this;
setAlias(name: string): this;
and(...args: Rule[]): LogicExpresion;
or(...args: Rule[]): LogicExpresion;
}
export interface Idiom<M, S> extends Host<M, S>, ArithmeticExpression, Definable {
m: M;
s: S & { id(refid: string): S; };
/** Get a certain sid */
id(refid: string): M;
[idx: number]: M;
}
// lexp
export interface LogicExpresion extends Rule {
and(...args: Rule[]): LogicExpresion;
or(...args: Rule[]): LogicExpresion;
setAlias(name: string): LogicExpresion;
}
// aexp
export interface ArithmeticExpression extends Rule {
add(...args: number[]): ArithmeticExpression;
sub(...args: number[]): ArithmeticExpression;
mul(...args: number[]): ArithmeticExpression;
div(...args: number[]): ArithmeticExpression;
}
///////////////////////////////////////////////////////////////////////////////
// Actions
///////////////////////////////////////////////////////////////////////////////
export type HostCB<M,S> = (host: Host<M,S>) => any;
export interface Host<M,S> {
post(rulesetName: string, event: (M & any));
postBatch(rulesetName: string, ...events: (M & any)[]);
assert(rulesetName: string, fact: (M & any));
assertFacts(rulesetName: string, ...facts: (M & any)[]);
retract(rulesetName: string, event: M & any);
patchState(rulesetName: string, state: S & any);
/**
* Timers can be started multiple times if they have different ids
*/
startTimer(timerName: string, seconds: number, id?: string);
}
export interface ActionContext<M, S> extends Host<M, S> {
m: M;
s: S;
[key: string]: any;
}
export type RuleAction<M, S> = (c: any) => any;
///////////////////////////////////////////////////////////////////////////////
// Configuration
///////////////////////////////////////////////////////////////////////////////
export interface DBConnection {
host: string;
port: number;
password?: string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment