jakesgordon / javascript-state-machine

A javascript finite state machine library
MIT License
8.69k stars 964 forks source link

Total rewrite in TypeScript #204

Closed kevguy closed 3 years ago

kevguy commented 3 years ago

This is a total rewrite of the whole library in TypeScript, and the changes are almost completely compatible with the original version.

  1. The lib is compiled with rollup instead of webpack with three formats: umd, cjs and es.
  2. Most of the objects in the library created using function prototypes are now using class.
  3. The example code (including bin/example.js and the example state machines in the examples folder) was modified accordingly and all work.
  4. You can do condition transition with an async function now (addressing this issue).
  5. All the tests are rewritten in TypeScript and stored in the test folder using jest instead of ava, due to ava requiring changes that conflicts with rollup (look at this ava issue) in order to support TypeScript.
  6. The original tests are in the test-js folder with minimal changes which will be covered later. All test cases passed except one.
  7. You cannot create a machine using const machine = StateMachine() anymore without the new keyword. One of the test cases was covering this, which is the case that didn't pass.
  8. When you run the factory method to create the machine and you have an base class constructor, you don't need to explicitly run this._fsm() in it anymore.
  9. When you use the apply method to extend your object to a state machine, you will get a new object instead of mutating the original object, that is running StateMachine.apply(obj) and use obj as it is, you have to do const newObj = StateMachine.apply(obj) and use newObj instead.

All in all, changes are mostly compatible with a few enhancements, and with type support .

kungfooman commented 3 years ago

I would rather have ES6 with import and export without the need of annoying bundlers (rollup). JSDoc will do the type checking.

james-relyea commented 3 years ago

This is great. @jakesgordon would you be willing to add new community members as maintainers for this library to keep it updated?

nanchengjiumeng commented 2 years ago

how can i install 4.0.0-beta use yarn.

lantica commented 2 years ago

It's cool for the async support. May I ask is there any install guide for this branch? I've tried using the build script rollup -c but it cannot build. @kevguy The error message is as below:

$ /Users/admin/javascript-state-machine/node_modules/.bin/rollup -c

src/app/index.ts → dist/umd/index.js...
[!] (plugin rpt2) Error: /Users/admin/javascript-state-machine/src/jsm.ts(291,44): semantic error TS2343: This syntax requires an imported helper named '__spreadArray' which does not exist in 'tslib'. Consider upgrading your version of 'tslib'.
src/jsm.ts
Error: /Users/admin/javascript-state-machine/src/jsm.ts(291,44): semantic error TS2343: This syntax requires an imported helper named '__spreadArray' which does not exist in 'tslib'. Consider upgrading your version of 'tslib'.
    at error (/Users/admin/javascript-state-machine/node_modules/rollup/dist/shared/rollup.js:158:30)
    at throwPluginError (/Users/admin/javascript-state-machine/node_modules/rollup/dist/shared/rollup.js:21784:12)
    at Object.error (/Users/admin/javascript-state-machine/node_modules/rollup/dist/shared/rollup.js:22488:20)
    at Object.error (/Users/admin/javascript-state-machine/node_modules/rollup/dist/shared/rollup.js:21960:38)
    at RollupContext.error (/Users/admin/javascript-state-machine/node_modules/rollup-plugin-typescript2/src/rollupcontext.ts:37:18)
    at /Users/admin/javascript-state-machine/node_modules/rollup-plugin-typescript2/src/print-diagnostics.ts:41:11
    at arrayEach (/Users/admin/javascript-state-machine/node_modules/rollup-plugin-typescript2/node_modules/lodash/lodash.js:516:11)
    at Function._.each [as forEach] (/Users/admin/javascript-state-machine/node_modules/rollup-plugin-typescript2/node_modules/lodash/lodash.js:9368:14)
    at printDiagnostics (/Users/admin/javascript-state-machine/node_modules/rollup-plugin-typescript2/src/print-diagnostics.ts:9:2)
    at Object.transform (/Users/admin/javascript-state-machine/node_modules/rollup-plugin-typescript2/src/index.ts:244:5)

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.