Options
All
  • Public
  • Public/Protected
  • All
Menu

Namespace Nucleon

Nucleon is a generic state machine for performing CRUD operations on a resource.

States

  • idle - resource can be edited;
    • template - this is a new resource, context.data is null;
      • clean - no edits have been made, context.edits is null;
        • valid - actions.validate allows submitting empty template with services.sendPost;
        • invalid - actions.validate disallows submitting empty template with services.sendPost;
      • dirty - template has been edited, context.edits is not null;
        • valid - actions.validate allows submitting template with services.sendPost;
        • invalid - actions.validate disallows submitting template with services.sendPost;
    • snapshot - this is an existing resource stored in context.data;
      • clean - no edits have been made, context.edits is null;
        • valid - actions.validate allows sending PATCH with no changes using services.sendPatch;
        • invalid - actions.validate requires changes before submitting changes with services.sendPatch;
      • dirty - existing resource has been edited, context.edits is not null;
        • valid - actions.validate allows submitting edits with services.sendPatch;
        • invalid - actions.validate disallows submitting edits with services.sendPatch;
  • busy - CRUD operation is in progress;
    • fetching - services.sendGet is loading the resource;
    • creating - services.sendPost is creating the resource;
    • updating - services.sendPatch is updating the resource;
    • deleting - services.sendDelete is deleting the resource;
  • fail - one of the CRUD operations has failed

Context

  • data - loaded resource or null;
  • edits - changes to the resource in context.data;
  • errors - array of validation errors, format defined by actions.validate;

Events

  • SET_DATA - sets context.data and transitions to the appropriate template or snapshot state;
  • EDIT (idle only) - adds an edit to context.edits and transitions to the appropriate valid or invalid state;
  • UNDO (idle only) - removes all edits and transitions to the appropriate valid or invalid state;
  • FETCH - loads a resource by transitioning to busy.fetching and running services.sendGet;
  • DELETE - deletes a resource by transitioning to busy.deleting and running services.sendDelete;
  • SUBMIT (valid only) - updates or creates a resource;

Actions:

  • validate - runs every time resource changes, put your v8n logic there;

Services

  • sendGet - fetches resource snapshot, returning parsed JSON or throwing an error;
  • sendPost - creates a resource, returning its parsed JSON or throwing an error;
  • sendPatch - updates a resource, returning its full parsed JSON or throwing an error;
  • sendDelete - deletes a resource, returning anything or throwing an error;

Index

Type Aliases

Variables

Type Aliases

Context<TData, TError, TFailure>: { data: TData | null; edits: Partial<TData> | null; errors: TError[]; failure: TFailure | null }

Type Parameters

  • TData extends Data = Data

    Type of data stored in the context, usually a hypermedia resource.

  • TError = unknown

    Type of error returned by the validate action.

  • TFailure = unknown

    Type of non-validation error thrown in request services.

Type declaration

  • data: TData | null

    Unmodified API resource snapshot. Can be null before it's loaded or once it's deleted.

  • edits: Partial<TData> | null

    Local changes to the API resource snapshot. Can be null before resource is loaded or once it's deleted.

  • errors: TError[]

    Form validation errors returned by the validate action or with request rejection.

  • failure: TFailure | null

    Request rejection info in fail state when it's unrelated to form validation.

Event<TData>: { data: TData | null; type: "SET_DATA" } | { type: "REFRESH" } | { data: Partial<TData>; type: "EDIT" } | { type: "UNDO" } | { type: "FETCH" } | { type: "DELETE" } | { type: "SUBMIT" }

Type Parameters

  • TData extends Data = Data

    Type of data stored in the context, usually a hypermedia resource.

State<TData, TError, TFailure>: { context: Core.Nucleon.Context<TData, TError> & { failure: TFailure }; value: "fail" } | { context: Core.Nucleon.Context<TData, TError> & { failure: null }; value: "busy" } | { context: Core.Nucleon.Context<TData, TError> & { failure: null }; value: { busy: "fetching" } } | { context: Core.Nucleon.Context<TData, TError> & { data: null; edits: Partial<TData>; failure: null }; value: { busy: "creating" } } | { context: Core.Nucleon.Context<TData, TError> & { data: TData; edits: Partial<TData>; failure: null }; value: { busy: "updating" } } | { context: Core.Nucleon.Context<TData, TError> & { data: TData; failure: null }; value: { busy: "deleting" } } | { context: Core.Nucleon.Context<TData, TError> & { failure: null }; value: "idle" } | { context: Core.Nucleon.Context<TData, TError> & { data: TData; failure: null }; value: { idle: "snapshot" } } | { context: Core.Nucleon.Context<TData, TError> & { data: TData; edits: null; failure: null }; value: { idle: { snapshot: "clean" } } } | { context: Core.Nucleon.Context<TData, TError> & { data: TData; edits: null; failure: null }; value: { idle: { snapshot: { clean: "valid" } } } } | { context: Core.Nucleon.Context<TData, TError> & { data: TData; edits: null; failure: null }; value: { idle: { snapshot: { clean: "invalid" } } } } | { context: Core.Nucleon.Context<TData, TError> & { data: TData; edits: Partial<TData>; failure: null }; value: { idle: { snapshot: "dirty" } } } | { context: Core.Nucleon.Context<TData, TError> & { data: TData; edits: Partial<TData>; failure: null }; value: { idle: { snapshot: { dirty: "valid" } } } } | { context: Core.Nucleon.Context<TData, TError> & { data: TData; edits: Partial<TData>; failure: null }; value: { idle: { snapshot: { dirty: "invalid" } } } } | { context: Core.Nucleon.Context<TData, TError> & { data: null; failure: null }; value: { idle: "template" } } | { context: Core.Nucleon.Context<TData, TError> & { data: null; edits: null; failure: null }; value: { idle: { template: "clean" } } } | { context: Core.Nucleon.Context<TData, TError> & { data: null; edits: null; failure: null }; value: { idle: { template: { clean: "valid" } } } } | { context: Core.Nucleon.Context<TData, TError> & { data: null; edits: null; failure: null }; value: { idle: { template: { clean: "invalid" } } } } | { context: Core.Nucleon.Context<TData, TError> & { data: null; edits: Partial<TData>; failure: null }; value: { idle: { template: "dirty" } } } | { context: Core.Nucleon.Context<TData, TError> & { data: null; edits: Partial<TData>; failure: null }; value: { idle: { template: { dirty: "valid" } } } } | { context: Core.Nucleon.Context<TData, TError> & { data: null; edits: Partial<TData>; failure: null }; value: { idle: { template: { dirty: "invalid" } } } }

Nucleon state machine typestate.

Type Parameters

  • TData extends Data = Data

    Type of data stored in the context, usually a hypermedia resource.

  • TError = unknown

    Type of error returned by the validate action.

  • TFailure = unknown

Variables

machine: StateMachine<Core.Nucleon.Context<Record<string, unknown>, unknown, unknown>, any, Core.Nucleon.Event<Record<string, unknown>>, Core.Nucleon.State<Record<string, unknown>, unknown, unknown>> = ...

Generated using TypeDoc