3.0.23 • Published 4 years ago

typedux v3.0.23

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

TypeDux / Redux / ReTyped

DOCS NEED UPDATING, USE v2 FOR NOW

A leaf based TypeScript 4 Immutable wrapper around REDUX - all decoration driven

To start, like everyone else, I love the benefits of an immutable functional paradigm, but the overhead of implementing it in the real world consistently proves difficult for a number of reasons

  • Learning curve
  • Type Safety
  • Re-usability
  • Scope access
  • Integration into a current application

The issues arise from a completely decoupled solution, which in practice provides an optimal usage pattern, but provides hurdle after hurdle in terms of learning and on-boarding as well as reusing existing code.

Enter TypeDux, simply put it's redux with an immutable & observable root state that is statically typed at every node and end, reuse and manage

Install

Same as every other package, note that reflect-metadata, ImmutableJS and Redux are peer dependencies

NOTE: Runtime requires ES6 level polyfills - so babel-polyfill or transform-runtime, etc work just fine

npm i --save typdux

Getting Started

  1. Create a leaf state and message (message is optional, only for typescript and can be any)
//Typescript

import * as Immutable from 'immutable'

/**
 * Leaf record defines allowed props
 */
const ExampleLeafRecord = Immutable.Record({
	str1: 'str1',
	str2: null
})

/**
 * Mock leaf state, dumb test state with test props
 */
class ExampleLeafState extends ExampleLeafRecord {
	str1:string
	str2:string

	constructor(props:any = {}) {
		super(props)

		Object.assign(this,props)
	}
}

/**
 * Typed action message (optional)
 */
interface ExampleMessage extends ActionMessage<ExampleLeafState> {

}
  1. Create an ActionFactory
//Typescript
import {ActionFactory,ActionReducer,ActionThunk} from 'typedux'

class ExampleActionFactory extends ActionFactory<ExampleLeafState,ExampleLeafMessage> {

	constructor() {
  		super(ExampleLeafState)
  	}

  	leaf():string {
  		return 'exampleLeafKey';
  	}

  	/**
    * State Accessors are SUPER easy
		  */
    getStr1() {
			return this.state.str1
    }

    /**
    * Reducers (if you don't know what a reducer is checkout the redux docs)
    * are super easy, annotate ActionReducer and return a function that takes state
		 */
  	@ActionReducer()
  	exampleStr1Update(val:string) {
  		return (state:ExampleLeafState) => state.set('str1',val)
  	}

  	/**
  	* Thunks are now track-able and wrapped in promises,
  	* getState is superfluous as it dispatch because this.state and
  	* calling any action directly provides required context
		 */
  	@ActionThunk()
    exampleThunk() {
      return Promised((dispatch,getState) => {
        return Promise.delay(1000).then(() => "example")
      })
    }
}
  1. Create the store

NOTE: ALL ACTION FACTORIES AND DECORATIONS MUST BE LOADED BEFORE CREATING THE STORE

// Typescript
const store = ObservableStore.createObservableStore(

	// Array of reducers (can be an empty array if only using @ActionFactory)
	reducers,

	// Additional enhancers for dev, etc
	compose.call(null, ...enhancers) as StoreEnhancer<any>,

	// Initial state
	null
)
  1. Observe store keys and leafs
// Create an observer
const unsub = store.observe(['exampleLeafKey','str1'],(newStr1,oldStr1) => {
	console.log(`str1 change from`,oldStr1,`to`,newStr1)
})

// Unsubscribe when done
unsub()

CREDIT

Jonathan Glanz @jglanz

3.0.23

4 years ago

3.0.22

4 years ago

3.0.21

4 years ago

3.0.20

4 years ago

3.0.18

4 years ago

3.0.19

4 years ago

3.0.17

4 years ago

3.0.16

4 years ago

3.0.14

4 years ago

3.0.15

4 years ago

3.0.13

4 years ago

3.0.12

4 years ago

3.0.10

4 years ago

3.0.11

4 years ago

3.0.9

4 years ago

3.0.8

4 years ago

3.0.7

4 years ago

3.0.6

4 years ago

3.0.5

4 years ago

3.0.4

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

2.0.35

4 years ago

3.0.0

4 years ago

2.0.34

4 years ago

2.0.33

4 years ago

2.0.31

4 years ago

2.0.30

4 years ago

2.0.28

4 years ago

2.0.29

4 years ago

2.0.26

4 years ago

2.0.23

4 years ago

2.0.21

5 years ago

2.0.20

5 years ago

2.0.19

5 years ago

2.0.18

5 years ago

2.0.17

5 years ago

2.0.16

5 years ago

2.0.13

5 years ago

2.0.12

5 years ago

2.0.11

5 years ago

2.0.10

5 years ago

2.0.9

6 years ago

2.0.8

6 years ago

2.0.7

6 years ago

2.0.5

6 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.0

6 years ago

0.1.20

6 years ago

0.1.18

7 years ago

0.1.17

7 years ago

0.1.16

7 years ago

0.1.15

7 years ago

0.1.13

7 years ago

0.1.12

8 years ago

0.1.10

8 years ago

0.1.9

8 years ago

0.1.8

8 years ago

0.1.7

8 years ago

0.1.6

8 years ago

0.1.5

8 years ago

0.1.4

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago

1.0.0-alpha-13

8 years ago

1.0.0-alpha-12

8 years ago

1.0.0-alpha-11

8 years ago

1.0.0-alpha-10

8 years ago

1.0.0-alpha-9

8 years ago

1.0.0-alpha-8

8 years ago

1.0.0-alpha-7

8 years ago

1.0.0-alpha-6

8 years ago

1.0.0-alpha-5

8 years ago

1.0.0-alpha-4

8 years ago

1.0.0-alpha-3

8 years ago

1.0.0-alpha-2

8 years ago

1.0.0-alpha-1

8 years ago

1.0.0-alpha

8 years ago

0.0.16

8 years ago

0.0.15

8 years ago

0.0.14

8 years ago

0.0.13

8 years ago

0.0.12

8 years ago

0.0.11

8 years ago

0.0.10

8 years ago

0.0.9

8 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3-ug

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago