Type Aliases
Context
Context
<TData, TError, TFailure>: { data
: TData | null; edits
: Partial<TData> | null; errors
: TError[]; failure
: TFailure | null }Type Parameters
TData extends Data = Data
TError = unknown
TFailure = unknown
Type declaration
data: TData | null
edits: Partial<TData> | null
errors: TError[]
failure: TFailure | null
Event
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
State
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" } } } }Type Parameters
TData extends Data = Data
TError = unknown
TFailure = unknown
Variables
Const machine
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>> = ...
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
isnull
;clean
- no edits have been made,context.edits
is null;valid
-actions.validate
allows submitting empty template withservices.sendPost
;invalid
-actions.validate
disallows submitting empty template withservices.sendPost
;dirty
- template has been edited,context.edits
is notnull
;valid
-actions.validate
allows submitting template withservices.sendPost
;invalid
-actions.validate
disallows submitting template withservices.sendPost
;snapshot
- this is an existing resource stored incontext.data
;clean
- no edits have been made,context.edits
is null;valid
-actions.validate
allows sending PATCH with no changes usingservices.sendPatch
;invalid
-actions.validate
requires changes before submitting changes withservices.sendPatch
;dirty
- existing resource has been edited,context.edits
is notnull
;valid
-actions.validate
allows submitting edits withservices.sendPatch
;invalid
-actions.validate
disallows submitting edits withservices.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 failedContext
data
- loaded resource ornull
;edits
- changes to the resource incontext.data
;errors
- array of validation errors, format defined byactions.validate
;Events
SET_DATA
- setscontext.data
and transitions to the appropriate template or snapshot state;EDIT
(idle only) - adds an edit tocontext.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 tobusy.fetching
and runningservices.sendGet
;DELETE
- deletes a resource by transitioning tobusy.deleting
and runningservices.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;