Skip to content

@opencraft/providence


Interface: SingleController\<T>

Defined in: singles/types/SingleController.ts:11

Controllers are the public interface for interacting with providence modules in your store of choice. There will only ever be one controller per registered module, thanks to the registry. The controller should not hold information that can't be reconstructed upon hot reload. It should merely be an interface for manipulating the registered module.

Extends

Type Parameters

T

T

Properties

attr

attr: \<Key>(attrName) => SingleState\<T>[Key]

Defined in: types/BaseController.ts:40

Retrieves a particular attribute from the state for this module.

Type Parameters

Key

Key extends "errors" | "name" | "endpoint" | "persistent" | "fetching" | "ready" | "failed" | "params" | "x" | "deleted" | "patchers"

Parameters

attrName

Key

Returns

SingleState\<T>[Key]

Inherited from

BaseController.attr


commit

commit: \<OutsideModuleDefinition, Key>(funcName, ...payload) => void

Defined in: types/BaseController.ts:44

Commits a mutation for this module.

Type Parameters

OutsideModuleDefinition

OutsideModuleDefinition extends BaseModule\<any, any, any> = BaseSingleModule\<T>

Key

Key extends string = string & keyof OutsideModuleDefinition["mutations"]

Parameters

funcName

Key | `${string}/${Key}`

payload

...ParametersExceptFirst\<OutsideModuleDefinition["mutations"][Key]>

Returns

void

Inherited from

BaseController.commit


delete

delete: () => Promise\<void>

Defined in: singles/types/SingleController.ts:84

Sends a deletion request to the server. If successful, marks x as null and sets the deleted flag to true and sets the deleted flag to true.

Returns

Promise\<void>


deleted

deleted: boolean

Defined in: singles/types/SingleController.ts:143

getter/setter to indicate whether the remote value we're tracking has been deleted.


dispatch

dispatch: (\<Key>(funcName, ...payload) => ReturnType\<SingleTasks\<T, BaseSingleModule\<T>>[Key]>) | (\<Key>(funcName, ...payload) => ReturnType\<SingleTasks\<T, BaseSingleModule\<T>>[Key]>)

Defined in: types/BaseController.ts:48

Dispatches a task for this module.

Inherited from

BaseController.dispatch


endpoint

endpoint: string

Defined in: singles/types/SingleController.ts:19

The endpoint property on the single module. This is a getter/setter, so you can update the endpoint.


errors

errors: default

Defined in: singles/types/SingleController.ts:89

Retrieves the stored error information from our last attempt at getting the remote object.


failed

failed: boolean

Defined in: singles/types/SingleController.ts:139

getter/setter to indicate that the attempt to fetch x failed. Can be set manually if needed for testing.


fetching

fetching: boolean

Defined in: singles/types/SingleController.ts:134

getter/setter to indicate that x is being fetched via get request. Can be set manually if needed for testing.


get

get: () => Promise\<T>

Defined in: singles/types/SingleController.ts:63

Performs a get request. Will update the value of x based on what it retrieves, and return a promise containing the new value.

Returns

Promise\<T>


getOnce

getOnce: () => void

Defined in: singles/types/SingleController.ts:58

Fetch this object from its endpoint. No matter how many times this function is called, it only runs once, allowing you to call it in any component that may need the function and not worry about several requests being sent.

Returns

void


initializePatcherSettings

initializePatcherSettings: \<AttrName>(attrName) => void

Defined in: singles/types/SingleController.ts:103

Initialize a patcher's settings. You might need this if the object returned from your initial fetching is incomplete and does not have the field you want to add a patcher for, or if you need to initialize a patcher without fetching the remote resource for some reason.

Type Parameters

AttrName

AttrName extends string | number | symbol

Parameters

attrName

AttrName

Returns

void


makeReady

makeReady: (val) => void

Defined in: singles/types/SingleController.ts:97

Set x and mark ready as true. Mostly useful for testing.

Parameters

val

T

Returns

void


managedNames

managedNames: string[]

Defined in: types/BaseController.ts:36

Returns the names of all modules this module manages. For singles, that will only be the current module name. For lists, it will be the current module name and all the names of the singles it manages.

Inherited from

BaseController.managedNames


moduleType

moduleType: "single"

Defined in: singles/types/SingleController.ts:15

Returns 'single', the type of module this controller handles.


name

name: string

Defined in: types/BaseController.ts:21

The name of the dynamically created module. This is a dot separated name. You can construct an escaped, valid name with use of the flattenNamespace function.

Inherited from

BaseController.name


namespace

namespace: string[]

Defined in: types/BaseController.ts:31

Useful for when needing to walk through modules for an operation, namespace returns an array of strings representing this module's location in the hierarchy.

Inherited from

BaseController.namespace


p

p: BoundPatchers\<T>

Defined in: singles/types/SingleController.ts:45

A proxy object that creates patchers for any property on the object. So, for instance, if you have a type:

.. code-block:: typescript

Text Only
declare interface MyType {
  id: number,
  name: string,
  fun: boolean,
}

Then this object will contain the keys id, name, and fun. Each of these attributes will be a Patcher bound to its specific field.


params

params: QueryParams | null

Defined in: singles/types/SingleController.ts:147

getter/setter for query parameters used when interacting with the endpoint.


patch

patch: (val) => Promise\<T>

Defined in: singles/types/SingleController.ts:68

Sends a patch request. Updates x with the value returned from the server, and returns a promise containing the new value.

Parameters

val

Partial\<T>

Returns

Promise\<T>


post

post: \<I, O>(val) => Promise\<AxiosResponse\<O, any, { }, any>>

Defined in: singles/types/SingleController.ts:78

Sends a post request. DOES NOT update x with the value returned from the server. Returns a promise containing the value the server sent back.

Type Parameters

I

I

O

O = I

Parameters

val

I

Returns

Promise\<AxiosResponse\<O, any, { }, any>>


preDestroy

preDestroy: () => void

Defined in: types/BaseController.ts:53

A function which is run after the controller is no longer needed (because all listeners have disconnected), before it is removed from the store.

Returns

void

Inherited from

BaseController.preDestroy


put

put: (val) => Promise\<T>

Defined in: singles/types/SingleController.ts:73

Sends a put request. Updates x with the value returned from the server, and returns a promise containing the new value.

Parameters

val

Partial\<T>

Returns

Promise\<T>


rawState

rawState: SingleState\<T>

Defined in: types/BaseController.ts:26

The raw state of the dynamically created module. This should be a copy so mutating the output doesn't affect the state.

Inherited from

BaseController.rawState


ready

ready: boolean

Defined in: singles/types/SingleController.ts:129

getter/setter to indicate that x is initialized and ready to be interacted with. Mostly set internally, but can be explicitly set during testing.


resetErrors

resetErrors: () => void

Defined in: singles/types/SingleController.ts:93

Empties out the values in errors.

Returns

void


setX

setX: (val) => void

Defined in: singles/types/SingleController.ts:49

Completely replace x.

Parameters

val

T | null

Returns

void


updateX

updateX: (val) => void

Defined in: singles/types/SingleController.ts:53

Update the value of x in place by setting a subset of its values.

Parameters

val

Partial\<T>

Returns

void


x

x: T | null

Defined in: singles/types/SingleController.ts:29

The data structure you're tracking in the single module. It can also be null if you've neither fetched nor preset its value. This is a getter and setter, so you can entirely replace x if you like.

Note that you should not mutate the attributes of x directly. Instead, if you need to change x, use updateX, setX, set the x setter directly, or use the Patchers`.