io.contextsAPIinterface
8 Methods
all()⚓︎
() => string[]
Returns all existing context names. Using the context name you can subscribe for context changes, updates or set context values.
destroy()⚓︎
(name: string) => Promise<any>
Destroys a context and all the data associated with it
Parameters (1)
| Name | Type | Required | Description |
|---|---|---|---|
| name⚓︎ | string | Name of the context to be removed |
get()⚓︎
(name: string) => Promise<any>
Return the context data immediately or asynchronously as soon as any data becomes available.
Parameters (1)
| Name | Type | Required | Description |
|---|---|---|---|
| name⚓︎ | string | Name of the context from which you want to get data. |
set()⚓︎
(name: string, data: any) => Promise<void>
setPath()⚓︎
(name: string, path: string, data: any) => Promise<void>
Sets a path in the context to some value. Use this to update values that are not on top level in the context.
Parameters (3)
| Name | Type | Required | Description |
|---|---|---|---|
| name⚓︎ | string | Name of the context to be updated |
|
| path⚓︎ | string | Path to be updated. Path should be in the format "prop1.prop2" |
|
| data⚓︎ | any | The object that will be applied to the path |
subscribe()⚓︎
(name: string, callback: (data: any, delta: any, removed: string[], unsubscribe: () => void, extraData?: any) => void) => Promise<() => void>
Subscribes for context events. Returns an unsubscribe function which you can use to stop receiving context updates.
Parameters (2)
| Name | Type | Required | Description |
|---|---|---|---|
| name⚓︎ | string | Name of the context to which you want to subscribe. |
|
| callback⚓︎ | (data: any, delta: any, removed: string[], unsubscribe: () => void, extraData?: any) => void | Function that will handle the updates. |
update()⚓︎
(name: string, data: any) => Promise<void>
Updates a context with the supplied object. This method updates only the specified context properties. Any other existing context properties will remain intact.
If the context does not exist, the update() method will create it.
Parameters (2)
| Name | Type | Required | Description |
|---|---|---|---|
| name⚓︎ | string | Name of the context to be updated. |
|
| data⚓︎ | any | The object that will be applied to the context. |
Example
io.contexts.update("app-styling", {
backgroundColor: "red",
alternativeColor: "green",
});