npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tamed-state-machine-frontend

v1.0.5

Published

The frontend library for the tamed-state-machine

Downloads

2

Readme

WHY?

This is the frontend part of the tamed-state-machine library. For full setup please refer to https://github.com/MehmetKaplan/tamed-state-machine.

This, tamed-state-machine-frontend library, is a set of functions that communicate with a backend that utilize tamed-state-machine-backend library to expose the state machine functionality.

Altogether these libraries give a generic method to handle most state machine related tasks.

The frontend functions are handlers that can be embedded into any client application that connects to the backend. This can be a real frontend app like the ones written with, react, react-native or expo. On the other hand there is no limitation for this library for frontend to be a presentation layer. That's why it can also be a backend node application as well.

One of the important features of this library is that it helps you to call pre and post functions just before and right after state machine transitions. The pre and post functions are your frontend application's functions and their names are defined in the database as described in the state machine configuration manual.

Note: Whenever the database is modified with new state machines, states, transitions, etc, the backend server will be able to read and use the definitions since we do not cache the state machine configurations. The rationale behind this is that we do not want to restart the backend server each time a new state machine is defined.

You can use the following functions defined in the API section to present state machine to your end users.

IMPORTANT: This library does not focus on the authorization. It should be handled separately.

SETUP

  1. Add the request handlers as a dependency of your project.
yarn add tamed-state-machine-frontend
  1. Include the tamed-state-machine-frontend library in your project.
const tamedStateMachineFrontendHandlers = require('https://github.com/MehmetKaplan/tamed-state-machine/blob/master/tamed-state-machine-frontend');
  1. Define the backend url.
const apiBackend = 'http://...'; // modify this with your backend that utilize the tamed-state-machine-backend library
  1. Define your pre transition and post transition functions.
const pre1 = (props) => { return "pre1 is called" }
const pre2 = (props) => { return "pre2 is called" }
//...
const post1 = (props) => { return "post1 is called" }
const post2 = (props) => { return "post2 is called" }
//...
  1. Register your pre and post transition functions and backend API to the library.
tamedStateMachineFrontendHandlers.init({
	apiBackend: apiBackend,
	preFunctions: {
		"pre1": pre1
		"pre2": pre2
	},
	postFunctions: {
		"post1": post1
		"post2": post2
	}
});
  1. Now the state machine frontend is ready to be consumed by your application.

API

Callbacks

For all functions (except the init function), there are 2 callback functions that should be passed as parameters. These functions are successCallback and failCallback. These functions are called when the function is successful or not successful respectively with following parameters:

  • props: the data that is posted to the backend
  • retval: the data that is returned from the backend. The details are as described in the API section of the tamed-state-machine-backend library. You can refer to Returns subsection for each route.

init

This function should be called before any other function. It initializes the library with the backend url and pre and post transition functions.

| Parameter | Type | Description | | --- | --- | --- | | p_params | Object | The object that contains the backend url and pre and post transition functions. |

p_params should have following format:

| Key | Type | Description | | --- | --- | --- | | apiBackend | String | The backend url. | | preFunctions | Object | The object that contains the pre transition functions. | | postFunctions | Object | The object that contains the post transition functions. |

preFunctions and postFunctions should have following format:

The below Key for "Function Name" is the name of the function that will be called before or after the transition. It is configured in the database as stated in the state machine configuration manual.

| Key | Type | Description | | --- | --- | --- | | Key for "Function Name" | Function | The function that will be called before or after the transition. |

Returns

If successful, resolves to true If not successful, rejects with an error message.

initiateInstance

Initializes a state machine instance. This instance is association between your application and a configured state machine.

| Parameter | Type | Description | | --- | --- | --- | | externalName| String | Connection to an application, here the value is a free-text. | | externalId | String | Connection to an application, here the value is usually the primary key of the connected document. (For example if you are implementing a document approval process, this is the internal id of the document). | | smName| String | Name of the state machine that is configured within the database. | | generatedBy| String | The user that initiated the state machine. | | successCallback | Function | Callback function that is called when the function is successful as described above in the Callbacks section. | |failCallback | Function | Callback function that is called when the function fails as described above in the Callbacks section. |

getInstance

Gets the instance of the state machine. This instance is association between your application and a configured state machine.

| Parameter | Type | Description | | --- | --- | --- | | externalName| String | Connection to an application, here the value is a free-text. | | externalId | String | Connection to an application, here the value is usually the primary key of the connected document. (For example if you are implementing a document approval process, this is the internal id of the document). | | smName| String | Name of the state machine that is being queried. | | successCallback | Function | Callback function that is called when the function is successful as described above in the Callbacks section. | |failCallback | Function | Callback function that is called when the function fails as described above in the Callbacks section. |

getPossibleTransitions

Finds the state machine instance and returns the possible transitions for the current state.

| Parameter | Type | Description | | --- | --- | --- | | externalName| String | Connection to an application, here the value is a free-text. | | externalId | String | Connection to an application, here the value is usually the primary key of the connected document. (For example if you are implementing a document approval process, this is the internal id of the document). | | smName| String | Name of the state machine that the current state transitions are being queried. | | successCallback | Function | Callback function that is called when the function is successful as described above in the Callbacks section. | |failCallback | Function | Callback function that is called when the function fails as described above in the Callbacks section. |

transitionInstance

Finds the state machine instance and performs the desired transition.

| Parameter | Type | Description | | --- | --- | --- | | externalName| String | Connection to an application, here the value is a free-text. | | externalId | String | Connection to an application, here the value is usually the primary key of the connected document. (For example if you are implementing a document approval process, this is the internal id of the document). | | smName| String | Name of the state machine that the state is requested to be transitioned. | | transitionName| String | Name of the transition that is being requested. | | transitionMadeBy| String | The user that requested the transition. | | comment| String | Comment for the transition for instance history. | | possibleTransitions | Array | Array of possible transitions for the current state. This should be obtained prior to calling transitionInstance from previous transitionInstance or initiateInstance calls. If this value is provided, the pre and post functions are called. | | successCallback | Function | Callback function that is called when the function is successful as described above in the Callbacks section. | |failCallback | Function | Callback function that is called when the function fails as described above in the Callbacks section. |

getInstanceHistory

Finds the state machine instance and returns the history of the transitions.

| Parameter | Type | Description | | --- | --- | --- | | externalName| String | Connection to an application, here the value is a free-text. | | externalId | String | Connection to an application, here the value is usually the primary key of the connected document. (For example if you are implementing a document approval process, this is the internal id of the document). | | smName| String | Name of the state machine that the history is being queried. | | successCallback | Function | Callback function that is called when the function is successful as described above in the Callbacks section. | |failCallback | Function | Callback function that is called when the function fails as described above in the Callbacks section. |

getAllPossibleTransitions

Gives the state machine transition configurations.

| Parameter | Type | Description | | --- | --- | --- | | smName| String | Name of the state machine that the transitions are being queried. | | successCallback | Function | Callback function that is called when the function is successful as described above in the Callbacks section. | |failCallback | Function | Callback function that is called when the function fails as described above in the Callbacks section. |

deleteInstance

Deletes the state machine instance.

| Parameter | Type | Description | | --- | --- | --- | | externalName| String | Connection to an application, here the value is a free-text. | | externalId | String | Connection to an application, here the value is usually the primary key of the connected document. (For example if you are implementing a document approval process, this is the internal id of the document). | | smName| String | Name of the state machine that the instance is being deleted. | | successCallback | Function | Callback function that is called when the function is successful as described above in the Callbacks section. | |failCallback | Function | Callback function that is called when the function fails as described above in the Callbacks section. |

Example

For a better understanding of how to use this library, please refer to this example.

License

The license is MIT and full text here.

Used Modules

  • tick-log license here
  • tamed-pg license here
  • fetch-lean license here