0.2.3 • Published 3 years ago

@storasy/core v0.2.3

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Storasy

Storasy Header library for working with asynchronous data

Quick Start

NodeJS

Inside your project directory, run in terminal:

yarn add @storasy/core

Or with npm:

npm install @storasy/core

Browser

...
<body>
...
<script src="https://unpkg.com/@storasy/core/dist/umd/storasy.production.js"></script>
<script>
  const storasy = window.storasy;
  ...
</script>
...
</body>

API

StorasyClient

import { createStorasyClient } from './create-storasy-client';

//options - optional
const storasyClient = createStorasyClient({
  abortController: {
    createAbortController: () => new AbortController(),
    getSignal: controller => controller.signal,
    abort: controller => controller.abort(),
    checkOnError: error => error.name === 'AbortError'
  }
});
Accept:
- abortController

Optional, creates an AbortController by default. Allows you to cancel asynchronous requests.

Axios Example:

const abortController = {
  createAbortController: () => axios.CancelToken.source(),
  getSignal: (token: CancelTokenSource) => token.token,
  abort: (token: CancelTokenSource) => token.cancel(),
  checkOnError: error => axios.isCancel(error)
}
Return:
- get

Getter for storasy item

const item = storasyClient.get('key');
- create

Creation storasy item.

//initialState - optional
storasyClient.create('key', initialState = 1)
- include

Checking for the presence of an item in the store.

storasyClient.include('key')
- put

Changing an existing item in the store.

storasyClient.put('key', 'newState');

// or with callback

storasyClient.put('key', oldState => 'newState');
- delete

Secure deletion of an item if there are no subscribers. Return a Boolean value.

const isDelete = storasyClient.delete('key');

console.log(isDelete);
- run

Starting the generator within the storasy item

function* generator(params) {}

const { refetch, cancel } = storasyClient.run('key', generator, {
    enabled: true, // If true, the generator will start immediately 
    params // Parameters that will get into the generator
});

// newParams - optional. Otherwise, the old ones get caught
refetch(newParams)
- subscribe

Subscription to change the state of the item

const state = null;

const unsubscribe = storasyClient.subscribe('key', item => state = item.state);

StorasyItem

const item = storasyClient.get('key');
- subscribersLength

Get the number of subscribers from the item

const subsLength = storasyClient.get('key').subscribersLength;
- getItem

Getter for state of the item

const item = storasyClient.get('key').getItem();

type TStorasyItem<T> = {
  state?: T;
  status: TStorasyItemStatus;
  error?: string;
  isLoaded: boolean;
  isLoading: boolean;
  isError: boolean;
  isStale: boolean;
};
- getState

Getter for item data

const data = storasyClient.get('key').getState();
- putState

Setter for item data

const item = storasyClient.get('key');

item.put('newData');

// or with callback

item.put(oldData => 'newData');
- putStatus

Set new item status

const item = storasyClient.get('key');
const newStatus = 'loading' | 'loaded';

item.putStatus(newStatus);
- putItem

Setter for item

const item = storasyClient.get('key');

item.putStatus(
    newState, //  newState | (oldState) => newState
    newStatus, // 'loading' | 'loaded'
    error, // string | undefined
);
- subscribe

Subscribe to item change

const item = storasyClient.get('key');
const state = null;

const unsubscribe = item.subscribe(itemState => state = itemState.state);
- getAbortController

Get the controller to cancel the request

const item = storasyClient.get('key');

const ac = item.getAbortController();

Examples

License

The MIT License.

0.2.1

3 years ago

0.2.0

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.1.0

3 years ago

0.1.1

3 years ago

0.0.10

3 years ago

0.0.11

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago