Skip to content

Function: createStore() ​

Call Signature ​

createStore<Context, Key, Value, CacheDisabled>(options, context): IStore<Key, Value, CacheDisabled> & Store_ContextReturn<Context>

Defined in: store/src/createStore.ts:147

Factory function to create a Store instance with optional business logic augmented into the store instance.

This function provides a convenient way to instantiate a store and attach supplemental logic (context) to it. It supports full type inference for both the store's data and the attached context.

Type Parameters ​

Context ​

Context extends object | ((store) => object)

The type of the context object or factory function.

Key ​

Key

The type of keys stored in the map.

Value ​

Value

The type of values stored in the map.

CacheDisabled ​

CacheDisabled extends boolean

Whether store data caching is disabled.

Parameters ​

options ​

Store_Options<Key, Value, CacheDisabled> | null | undefined

(optional) Configuration options for the store.

context ​

Context & Store_ContextValidate<Context, IStore<Key, Value, CacheDisabled>>

(optional) A plain object or a factory function that returns an object. If function provided, it is executed only once during instantiation.

Purpose: Use context to encapsulate domain-specific business logic, helper methods, or non-reactive state directly into the store instance.

Behavior:

  • Non-Reactive: Updates to context properties do not trigger onChange or RxJS emissions.
  • Non-Persistent: Context data is purely in-memory and is not saved to persistent storage.
  • Access to Store: When a factory function is used, it receives the store instance as an argument.
  • Notes:
    • Built-in store properties are not allowed
    • Augmented methods' "thisArg" will be the context as per default JavaScript behavior.
    • Custom class instances are supported as context. Built-in Store properties take precedence in the event of a naming conflict.

Returns ​

IStore<Key, Value, CacheDisabled> & Store_ContextReturn<Context>

A Store instance with augmented properties (if any).

Examples ​

Basic usage ​

javascript
import { createStore } from '@superutils/store'

const store = createStore({ initialValue: new Map([['count', 0]])})
store.set('count', 1)
store.set('count', prevCount => prevCount + 1)
console.log(store.get('count')) // 2

Store augmented with custom business logic ​

javascript
import { createStore } from '@superutils/store'

const store = createStore({ name: 'user-settings'}, {
  count: 0,
  log(msg) {
    console.log(`[${++this.count}] ${msg}`)
  }
})

store.log('Settings updated')
console.log(store.count)

In-memory store ​

javascript
import fetch from '@superutils/fetch'
import { createStore } from '@superutils/store'

// Bypass the name property to create an in-memory store
const store = createStore()

// This will NOT save the data to localStorage
store.set('key', 'value')

Store with a functional context ​

javascript
import { createStore } from '@superutils/store'

const authStore = createStore(null, store => ({
  isAuthenticated: () => store.has('token'),
  logout: () => {
    store.delete('token')
    console.log('logged out')
  }
}))

if (authStore.isAuthenticated()) {
  authStore.logout()
}

Augmenting with a custom class instance ​

typescript
import { createStore } from '@superutils/store'

class MyCustomClass {
  private _count = 0
  get count() {
    return this._count
  }
  increment = () => this._count++
  decrement() {
    return --this._count
  }
}

const store = createStore(null, new MyCustomClass())
console.log(
  store.count,     // 0
  store.increment(),// function
  store.count,	 // 1
  store.decrement(), // undefined
)

Call Signature ​

createStore<Key, Value, CacheDisabled>(options?): IStore<Key, Value, CacheDisabled>

Defined in: store/src/createStore.ts:161

Factory function to create a Store instance with optional business logic augmented into the store instance.

This function provides a convenient way to instantiate a store and attach supplemental logic (context) to it. It supports full type inference for both the store's data and the attached context.

Type Parameters ​

Key ​

Key

The type of keys stored in the map.

Value ​

Value

The type of values stored in the map.

CacheDisabled ​

CacheDisabled extends boolean = false

Whether store data caching is disabled.

Parameters ​

options? ​

Store_Options<Key, Value, CacheDisabled>

(optional) Configuration options for the store.

Returns ​

IStore<Key, Value, CacheDisabled>

A Store instance with augmented properties (if any).

Examples ​

Basic usage ​

javascript
import { createStore } from '@superutils/store'

const store = createStore({ initialValue: new Map([['count', 0]])})
store.set('count', 1)
store.set('count', prevCount => prevCount + 1)
console.log(store.get('count')) // 2

Store augmented with custom business logic ​

javascript
import { createStore } from '@superutils/store'

const store = createStore({ name: 'user-settings'}, {
  count: 0,
  log(msg) {
    console.log(`[${++this.count}] ${msg}`)
  }
})

store.log('Settings updated')
console.log(store.count)

In-memory store ​

javascript
import fetch from '@superutils/fetch'
import { createStore } from '@superutils/store'

// Bypass the name property to create an in-memory store
const store = createStore()

// This will NOT save the data to localStorage
store.set('key', 'value')

Store with a functional context ​

javascript
import { createStore } from '@superutils/store'

const authStore = createStore(null, store => ({
  isAuthenticated: () => store.has('token'),
  logout: () => {
    store.delete('token')
    console.log('logged out')
  }
}))

if (authStore.isAuthenticated()) {
  authStore.logout()
}

Augmenting with a custom class instance ​

typescript
import { createStore } from '@superutils/store'

class MyCustomClass {
  private _count = 0
  get count() {
    return this._count
  }
  increment = () => this._count++
  decrement() {
    return --this._count
  }
}

const store = createStore(null, new MyCustomClass())
console.log(
  store.count,     // 0
  store.increment(),// function
  store.count,	 // 1
  store.decrement(), // undefined
)