actiore
TypeScript icon, indicating that this package has built-in type declarations

1.0.0-alpha.2 • Public • Published

Actiore

NpmLicense npm npm npm type definitions


Installation

NPM

# with npm
$ npm install actiore

# with yarn
$ yarn add actiore

Features

  • Store managment made for React
  • TypeScript support
  • Fast and simple setup
  • Redux alternative

Documentation

Types

type State<Type> = Type;
type Action<PayloadType, ReturnType, StateType> = (
  store: Store<StateType>,
  payload: PayloadType
) => ReturnType;
type ActionType = string;
type Listener = () => void;
type Middleware<StateType> = (state: State<StateType>) => State<StateType>;

Interfaces

interface ActionDef {
  [type: string]: Action<any, any, any>;
}
interface ActionHolder {
  [name: string]: Action<any, any, any>;
}

Classes

Store

Generics
class Store<StateType> { ... }
Constructor
constructor(initialState: State<StateType>, actions: ActionDef)
Properties
private __STATE__: State<StateType>;
private __ACTIONS__: ActionHolder;
private __SNAPSHOT__: State<StateType>;
private __LISTENER__: Listener;
private __MIDDLEWARES__: Middleware<StateType>[];
private __NODE__: React$Node;
Methods
// Returns State of Store
get state(): State<StateType>;
// Triggers an Action by his Type name :: Returns the return value of the Action
trigger(type: ActionType, payload?: any): any;
// Replaces the current State of Store
push(newState: State<StateType>): void;
// Pipes Store to Object (by default window)
pipe(name: string = "Store", to?: object = window): void;
// Takes a Snapshot of the current State and returns it
snapshot(): State<StateType>;
// Restores a Snapshot
restore(state?: State<StateType> = this.__SNAPSHOT__): void;
// Subscribes to State changes :: triggers on push
subscribe(listener: Listener): void;
// Functions to be called on any State manipulation
middlewares(middlewares: (Middleware<StateType>[]): void;

Functions

/*
  Connects Store to React Node with Name

  AS:
  this.name && this["name"]
*/

export function connect(store: Store<any>)
  => (node: React$Node)
  => (name: string)
  => void;

Example

import * as React from "react";
import { Store, connect, State, Action } from "actiore";

// Store configuration
type mainState = {
  time: string;
};

const nextTime: Action<void, void, mainState> = (
  store: Store<mainState>
): void => {
  setInterval(() => store.push({ time: new Date() }), 1000);
};

const mainStore: Store<mainState> = new Store(
  {
    time: new Date().toLocaleTimeString()
  },
  {
    NEXTTIME: nextTime
  }
);

mainStore.middlewares([
  (state: State<mainState>): State<mainState> => ({
    time: new Date(state.time).toLocaleTimeString()
  })
]);

mainStore.subscribe(() => {
  console.log("Store received new State!");
});

// Component
class TimeComponent extends React.Component<{}, {}> {
  componentWillMount() {
    connect(mainStore)(this)("store");
    this.store.trigger("NEXTTIME");
  }

  render() {
    return <div>{this.store.state.time}</div>;
  }
}

License (MIT)

Copyright (c) 2018 Gent Serifi, Geecy

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Package Sidebar

Install

npm i actiore

Weekly Downloads

2

Version

1.0.0-alpha.2

License

MIT

Unpacked Size

28.7 kB

Total Files

7

Last publish

Collaborators

  • geecy