6.3.0 • Published 5 years ago

subjective v6.3.0

Weekly downloads
162
License
MIT
Repository
github
Last release
5 years ago

Subjective

  • Opinionated state management
  • Type safety by design. Type inference works for both Update and Selector functions.
  • Logging. Inspect where Update function was called.
  • Pause observable stream, if needed.
  • Always receive the whole state, if needed.

Concepts

State

const state = new Subjective(
    productState,
    productStateFns,
);
Default Logger
const state = new Subjective(
    productState,
    productStateFns,
    // use default Logger (dev only)
    true
);
Custom Logger
const state = new Subjective(
    productState,
    productStateFns,
    // use custom Logger (dev only)
    (updateFnName: string, payload: any, updateFnRef: Function) => {
        // LOG
        const data = JSON.stringify(payload);
        const dataTrimmed = data.substring(0, 80);
        // logging to console
        console.groupCollapsed(
            `%c${fnName}: %c${dataTrimmed}${
                dataTrimmed.length < data.length ? '…' : ''
            }`,
            `color: green; font-weight: 300;`,
            `color: gray; font-weight: 100;`,
        );
        console.log(payload);
        console.log(updateFnRef);
        console.groupEnd();
    }
);

EXAMPLE

Selector function

Subscribe to state.filter.type and notify with its value
state.select(s => s.filter.type).subscribe();
Subscribe to state.filter.type and notify with a whole state
state.select(s => s.filter.type, true).subscribe();

EXAMPLE

Update function

Update value of state.filter.type
state.update(f => f.updateFilterType, type);
Update value of state.filter.type and do not notify subscribers
state.update(f => f.updateFilterType, type, false);
Update value of state.filter.type and return updated state
const updatedState = state.update(f => f.updateFilterType, type);

EXAMPLE

Examples

NOTES

Immutable pattern

Always use immutable pattern otherwise it will not work. We can't rely on mutations since object reference is always the same.

Type-safety

Types are always inferred either from state class or payload parameter of the update function.

FUTURE

I've been thinking of this new syntax...check it out.

Credits

6.3.0

5 years ago

6.2.0

5 years ago

6.1.0

5 years ago

6.0.0

6 years ago

5.0.1

6 years ago

5.0.0

6 years ago

4.1.0

6 years ago

4.0.2

6 years ago

4.0.1

6 years ago

4.0.0

6 years ago

3.1.0

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.2.0

6 years ago

2.1.0

6 years ago

2.0.0

6 years ago

1.1.0

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.0.0

6 years ago