New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

lunex

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lunex

A lightweight, framework agnostic, state management library

latest
Source
npmnpm
Version
1.0.4
Version published
Weekly downloads
7
16.67%
Maintainers
1
Weekly downloads
 
Created
Source

Lunex

Lunex is a lightweight, framework agnostic, typescript friendly, state management library inspired by Vuex/Pinia.

Installation

yarn add lunex
npm install lunex

Getting Started

Create a store:

// authStore.ts
import { createStore } from 'lunex';
import { api } from '~/api';

type User = {
  name: string;
};

type StoreState = {
  user?: User;
};

type StoreGetters = {
  isLoggedIn: boolean;
};

type StoreActions = {
  login: (payload: User) => Promise<void>;
  logout: () => Promise<void>;
};

export const authStore = createStore<StoreState, StoreGetters, StoreActions>({
  state: {},
  actions: {
    login: async (
      state: StoreState,
      payload: { username: string; password: string },
    ) => {
      const user = await api.login(username, password);
      return { user };
    },
    logout: async (state: StoreState) => {
      await api.logout();
      return {};
    },
  },
  getters: {
    isLoggedIn: (state: StoreState): boolean => !!state.user,
  },
  plugins: [
    (state: StoreState) => console.debug('PLUGIN: ', JSON.stringify(state)),
  ],
});

then use the store:

import { authStore } from '~/stores/authStore';

console.log(JSON.stringify(authStore.state));

// {}

const unsubscribeFromState = authStore.state$.subscribe((state) =>
  console.log(`SUBSCRIPTION(state$):  ${JSON.stringify(state)}`),
);

// SUBSCRIPTION(state$): {}

console.log(JSON.stringify(authStore.isLoggedIn));

// false

const unsubscribeFromIsLoggedIn = authStore.isLoggedIn$.subscribe(
  (isLoggedIn) =>
    console.log(
      `SUBSCRIPTION(isLoggedIn$): user is ${isLoggedIn ? '' : 'not'} logged in`,
    ),
);

// SUBSCRIPTION(isLoggedIn$): user is not logged in

authStore.login({ username: 'user-1', password: 'pass123' });

// PLUGIN: { user: { name: 'curtis' } }

// SUBSCRIPTION(state$):  { user: { name: 'curtis' } }

// SUBSCRIPTION(isLoggedIn$): user is logged in

console.log(JSON.stringify(authStore.state));

// { user: { name: 'curtis' } }

console.log(JSON.stringify(authStore.isLoggedIn));

// true

authStore.logout();

// PLUGIN: {}

// SUBSCRIPTION(state$):  {}

// SUBSCRIPTION(isLoggedIn$): user is not logged in

unsubscribeFromState();

unsubscribeFromIsLoggedIn();

Keywords

state management

FAQs

Package last updated on 17 May 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts