Skip to main content
This release is 1 version behind 0.0.5 — the latest version of @front-work/state-core. Jump to latest

extensible core state/state-context functionality for performant state keeping for the front-work framework.

Works with
This package works with Bun
It is unknown whether this package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Browsers
JSR Score
94%
Published
3 weeks ago
deno add @front-work/state-core
import * as mod from "@front-work/state-core";
npx jsr add @front-work/state-core
import * as mod from "@front-work/state-core";
yarn dlx jsr add @front-work/state-core
import * as mod from "@front-work/state-core";
pnpm dlx jsr add @front-work/state-core
import * as mod from "@front-work/state-core";
bunx jsr add @front-work/state-core
import * as mod from "@front-work/state-core";

front-state-core

extensible core state/state-context functionality for performant state keeping for the front-work framework

the library is not meant to be used independently rather as a lower level library to wrapped within custom functionality wrappers

examples

managing state contexts

import state from "@front-work/state-utils";
import stateCore, {
  setContext,
  createContext,
  removeContext,
  globalContext
} from "@front-work/state-core";

function meetingScopedStates(user: User) {
  const context = setContext(createContext(globalContext));

  const participants = state<User[]>()
  const chat = participants.to(list => list.map(user => user.chat))

  setContext(globalContext)

  function endMeeting(){
    removeContext(context)
  }

  function addParticipant(user: User){
    participants.value = [...participants.value, user]
  }
  function removeParticipant(user: User){
    participants.value = participants.filter(({id}) => user.id !== id )
  }

  return {
    chats,
    addParticipant,
    removeParticipant,
    endMeeting
  }
}

managing un/registration, updates using stateCore

import {IMut} from "@front-work/state-types";
import stateCore from "@front-work/state-core";

function countMinutes(): IState<number> {
  const { watch, unWatch, notify } = stateCore<number>();

  let value = 0;

  setState(() => {
    notify(value++, value)
  }, 1000);

  return {
    watch,
    unWatch,
    get value() {
      return value;
    }
  }
}