0.1.60 • Published 4 years ago

@lespantsfancy/hive v0.1.60

Weekly downloads
-
License
UNLICENSED
Repository
github
Last release
4 years ago

Methods

As a general rule, if the method is spelled in camelCase, then it is an internal, this-applicable method; if it is ProperCase, then it is a static method, and must be invoked via ClassName.MethodName.

Package Exports

import Hive, { spawnStateNode, useNodeContext } from "@lespantsfancy/hive";

Import/Export

ImportTypeParametersDescription
*defaultnoneUsed to perform any modifications before the reducers are called. Contains { Message, Node, fn }
spawnStateNodenamed(state, ...reducers)This should be used as the React:Context itself. e.g. React.createContext(spawnStateNode(state, ...reducers)))
useNodeContextnamed(context)This should be used in place of any useContext call and will return { node, state }

Message

new Message(type, payload, emitter, { id, timestamp });

export default Message;

@id and @timestamp are automatically populated, and are really only there for de/serialization reasons.

Methods

MethodParametersDescription
toJson()Convert the Message into a json string.
toObject()Convert the Message into a basic Object.
FromJson(json|obj)Convert a json string or Object into a Message, provided it was previously serialized or contains the appropriate properties.
Conforms(obj)Check if @obj conforms to the shape of Message
JsonConforms(json)Check if @json conforms to the shape of Message

Node

new Node(state = {});

export default Node, { EnumEventType };

Node extends EventEmitter and, as such, has all of the .on, .off, etc. methods available to it. Two (2) events are prepopulated (EnumEventType.STATE and EnumEventType.MESSAGE), which have native handlers. The EventEmitter allows for multiple handlers, so you can have additional custom handlers to any event--including EnumEventType.STATE and EnumEventType.MESSAGE--without direct consequences by that fact itself.

EnumEventType

Contains two (2) entries on which Node has explicit customized functionality.

  • STATE
  • MESSAGE

Methods

MethodParametersDescription
.watchState(node, twoWay=false)A node can watch the EnumEventType.STATE event of another node by way of a Message sent to its .onState method (which is overwritable if needed, but probably shouldn't be). @twoWay=true will cause @node to reciprocate .watchState, making both watch each other.
.watchMessages(node, twoWay=false)A node can watch the EnumEventType.MESSAGE event of another node by way of a Message sent to its .onMessage method (which is overwritable if needed, but probably shouldn't be). @twoWay=true will cause @node to reciprocate .watchMessages, making both watch each other.
.addReducer(fn)|(type, fn)All reducers should return the new state. If a @type is also specified, the reducer will only fire if message.type === @type. If no state is returned, then the current this.state will be used; this allows for "viewing" methods to be injected without consequence e.g. .addReducer(console.log).
.dispatch(type, payload)Used to .emit(EnumEventType.MESSAGE, new Message(type, payload, this));
.before(state, message, node)Used to perform any modifications before the reducers are called.
.after(state, message, node)Used as a pseudo useEffect proxy, called after all of the reducers have run, immediately after the this.state has been altered.
.flatten()This will convert the this.state into an array of dot notation, value arrays. For example, if the state = { cat: { count : 5 }, dog: "bob" }, .flatten will return [ [ "cat.count", 5 ], [ "dog", "bob" ] ].
.unflatten(Node.flatten())This undoes .flatten. An input of [ [ "cat.count", 5 ], [ "dog", "bob" ] ] will return { cat: { count : 5 }, dog: "bob" } AND set this.state to the newly-created object.

Example Usage

import React from "react";
import { spawnStateNode, useNodeContext } from "@lespantsfancy/hive";

const initStateNode = spawnStateNode({ cats: 0 }, [
    "cats",
    (state, msg, node) => {
        return {
            ...state,
            cats: state.cats + 1
        };
    }
]);

export const Context = React.createContext(initStateNode);

function SubComponent(props) {
    const { node, state } = useNodeContext(Context);
    
    return (
        <div>
            <div>Cats: { state.cats }</div>
            <button onClick={ e => node.dispatch("cats", state.cats + 1 )}>Click Me</button>
        </div>
    )
}


// To explicate *sic*, the `value` is not missing inputs; `useNodeContext` extracts its properties from the passed `Node`.  `node`, however, is **required** for `useNodeContext` to function properly.
export default function App() {
    return (
        <Context.Provider value={{ node: initStateNode }}> {/* sic */}
            <SubComponent />
            <SubComponent />
            <SubComponent />
        </Context.Provider>
    );
};
0.1.60

4 years ago

0.1.59

4 years ago

0.1.58

4 years ago

0.1.57

4 years ago

0.1.56

4 years ago

0.1.55

4 years ago

0.1.54

4 years ago

0.1.53

4 years ago

0.1.52

4 years ago

0.1.51

4 years ago

0.1.50

4 years ago

0.1.49

4 years ago

0.1.48

4 years ago

0.1.47

4 years ago

0.1.46

4 years ago

0.1.44

4 years ago

0.1.45

4 years ago

0.1.43

4 years ago

0.1.42

4 years ago

0.1.41

4 years ago

0.1.40

4 years ago

0.1.39

4 years ago

0.1.38

4 years ago

0.1.37

4 years ago

0.1.35

4 years ago

0.1.36

4 years ago

0.1.34

4 years ago

0.1.33

4 years ago

0.1.32

4 years ago

0.1.30

4 years ago

0.1.31

4 years ago

0.1.29

4 years ago

0.1.28

4 years ago

0.1.27

4 years ago

0.1.26

4 years ago

0.1.25

4 years ago

0.1.24

4 years ago

0.1.23

4 years ago

0.1.22

4 years ago

0.1.20

4 years ago

0.1.21

4 years ago

0.1.19

4 years ago

0.1.16

4 years ago

0.1.17

4 years ago

0.1.18

4 years ago

0.1.15

4 years ago

0.1.14

4 years ago

0.1.13

4 years ago

0.1.12

4 years ago

0.1.11

4 years ago

0.1.10

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago