diff --git a/dist/index.d.ts b/dist/index.d.ts new file mode 100644 index 0000000..de33095 --- /dev/null +++ b/dist/index.d.ts @@ -0,0 +1,62 @@ +export type Event_T = [name: string, payload: any]; +export interface Machine_T { + states: Array; +} +export interface State_T { + name: string; + eventReactionCouplings: Array; +} +export interface EventReactionCouplings_T { + eventName: string; + reactions: Array; +} +export type Reaction_T = SideEffect_T | ContextMutation_T | Goto_T; +export interface SideEffect_T { + type: 'SideEffect'; + fn: SideEffectFunction_T; +} +export type SideEffectFunction_T = (ctx: any, e: Event_T, self: Interpreter_T) => void; +export interface ContextMutation_T { + type: 'ContextMutation'; + fn: ContextMutationFunction_T; +} +export type ContextMutationFunction_T = (ctx: any, e: Event_T, self: Interpreter_T) => any; +export interface Goto_T { + type: 'Goto'; + targetStateName: string; +} +export declare const Machine: (...states: Array) => Machine_T; +export declare const State: (name: string, ...eventReactionCouplings: Array) => State_T; +export declare const On: (eventName: string, ...reactions: Array) => EventReactionCouplings_T; +export declare const SideEffect: (fn: SideEffectFunction_T) => SideEffect_T; +export declare const Goto: (targetStateName: string) => Goto_T; +export declare const Context: (fn: ContextMutationFunction_T) => ContextMutation_T; +export interface Interpreter_T { + machine: Machine_T; + state: string; + context: any; + eventQueue: Array; + isTransitioning: boolean; + subscriptions: Record; +} +export declare function interpret(machine: Machine_T, options: { + state?: string; + context: any; +}): Interpreter_T; +/** Inject an Event into the Interpreter's "tick queue". + * + * An event can be signify something "new" happening, such that its reactions should run on the next Tick; + * or it can signify a milestone "within" the current Tick, such that a Tick can be thought of as having + * "sub-Ticks". + * + * This distinction is significant for proper ordering of reaction execution, and also for determining + * whether to run a reaction at all. If an Event is received, and is specified to be applied on a past + * Tick, it is discarded. + */ +export declare function send(interpreter: Interpreter_T, event: Event_T): void; +export declare const enqueue: typeof send; +export type SubscriptionCallbackFunction_T = (self: Interpreter_T) => void; +export declare function subscribe(interpreter: Interpreter_T, callback: SubscriptionCallbackFunction_T): number; +export declare function unsubscribe(interpreter: Interpreter_T, subscriptionId: string): void; +export declare const Spawn: () => void; +export declare const Unspawn: () => void; diff --git a/dist/tests/00-basic.d.ts b/dist/tests/00-basic.d.ts new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/dist/tests/00-basic.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/dist/tests/01-ping-pong.d.ts b/dist/tests/01-ping-pong.d.ts new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/dist/tests/01-ping-pong.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/package.json b/package.json index a2da098..b0c5dd8 100644 --- a/package.json +++ b/package.json @@ -5,14 +5,18 @@ "license": "MIT", "main": "./dist/index.js", "type": "module", + "types": "./dist/index.d.ts", "exports": { "import": "./dist/index.js" }, "scripts": { "build": "node build.mjs", + "build-types": "node tsc", "build-tests": "esbuild --bundle --outdir=dist/tests src/tests/*.ts" }, "devDependencies": { - "esbuild": "^0.17.18" + "@types/node": "^20.1.4", + "esbuild": "^0.17.18", + "typescript": "^5.0.4" } } diff --git a/tsconfig.json b/tsconfig.json index 6100268..4b48cc7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,6 +2,7 @@ "compilerOptions": { "emitDeclarationOnly": true, "declaration": true, - "lib": ["ES2020"] + "lib": ["ES2020"], + "outDir": "dist" } } \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 7c0324b..a2838b6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -112,6 +112,11 @@ resolved "https://registry.yarnpkg.com/@esbuild/win32-x64/-/win32-x64-0.17.18.tgz#3a8e57153905308db357fd02f57c180ee3a0a1fa" integrity sha512-qU25Ma1I3NqTSHJUOKi9sAH1/Mzuvlke0ioMJRthLXKm7JiSKVwFghlGbDLOO2sARECGhja4xYfRAZNPAkooYg== +"@types/node@^20.1.4": + version "20.1.4" + resolved "https://verdaccio.torahanytime.com/@types/node/-/node-20.1.4.tgz#83f148d2d1f5fe6add4c53358ba00d97fc4cdb71" + integrity sha512-At4pvmIOki8yuwLtd7BNHl3CiWNbtclUbNtScGx4OHfBd4/oWoJC8KRCIxXwkdndzhxOsPXihrsOoydxBjlE9Q== + esbuild@^0.17.18: version "0.17.18" resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.17.18.tgz#f4f8eb6d77384d68cd71c53eb6601c7efe05e746" @@ -139,3 +144,8 @@ esbuild@^0.17.18: "@esbuild/win32-arm64" "0.17.18" "@esbuild/win32-ia32" "0.17.18" "@esbuild/win32-x64" "0.17.18" + +typescript@^5.0.4: + version "5.0.4" + resolved "https://verdaccio.torahanytime.com/typescript/-/typescript-5.0.4.tgz#b217fd20119bd61a94d4011274e0ab369058da3b" + integrity sha512-cW9T5W9xY37cc+jfEnaUvX91foxtHkza3Nw3wkoF4sSlKn0MONdkdEndig/qPBWXNkmplh3NzayQzCiHM4/hqw==