Overview of Harlem state management for Vue 3
reactive objects.website·Indexed 32 days ago
https://harlemjs.com/A global state management solution for Vue 3 featuring a functional API for creating, reading, and mutating state. Harlem provides core concepts such as stores, state, getters, actions, and mutations, and is extensible via a system of plugins and extensions including official tools for SSR, Devtools, storage, and transactions.
reactive objects.// store.js
import STATE from './state';
import { createStore } from '@harlem/core';
export const {
state,
getter,
mutation,
...store
} = createStore('store1', STATE);Harlem uses a Redux-like state management pattern divided into four main concepts to manage data flow:
Unlike monolithic state trees, Harlem uses individual stores to create logical boundaries between disparate data. While mutations are foundational, they are optional when using actions, as actions can mutate state directly while remaining auditable by devtools.
Harlem uses two different mechanisms for extending functionality:
name field for identification and an install method. The install method provides access to the app instance, an eventEmitter for reacting to store events (mutations, errors) or communicating with other plugins, and a stores Map containing all registered store instances. When using TypeScript, it is recommended to install harlem or @harlem/core as a devDependency and cast the plugin object as a HarlemPlugin for full type support.import type { HarlemPlugin } from 'harlem';
export default {
name: 'my-plugin',
install(app, eventEmitter, stores) {
// Your plugin logic here
}
} as HarlemPlugin;harlem-plugin- prefix in the package name to make it easily discoverable for other users.import type {
InternalStore,
BaseState,
} from '@harlem/core';
interface Options {
option1: string;
option2: number;
}
export default function resetExtension<TState extends BaseState>(options?: Options) {
return (store: InternalStore<TState>) => {
function doSomethingWithTheStore(input: string) {
// implementation logic
}
return {
doSomethingWithTheStore
};
};
}@harlem/extension-trace package and include it in the extensions array when calling createStore.yarn add @harlem/extension-trace
# or
npm install @harlem/extension-traceimport traceExtension from '@harlem/extension-trace';
import { createStore } from 'harlem';
const STATE = { firstName: 'Jane', lastName: 'Smith' };
const {
state,
getter,
mutation,
startTrace,
stopTrace,
onTraceResult
} = createStore('example', STATE, {
extensions: [
traceExtension({
autoStart: true,
debug: true,
})
]
});