3.0.23 • Published 5 years ago

typedux v3.0.23

Weekly downloads
4
License
MIT
Repository
github
Last release
5 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

5 years ago

3.0.22

5 years ago

3.0.21

5 years ago

3.0.20

5 years ago

3.0.18

5 years ago

3.0.19

5 years ago

3.0.17

5 years ago

3.0.16

5 years ago

3.0.14

5 years ago

3.0.15

5 years ago

3.0.13

5 years ago

3.0.12

5 years ago

3.0.10

5 years ago

3.0.11

5 years ago

3.0.9

5 years ago

3.0.8

5 years ago

3.0.7

5 years ago

3.0.6

5 years ago

3.0.5

5 years ago

3.0.4

5 years ago

3.0.3

5 years ago

3.0.2

5 years ago

3.0.1

5 years ago

2.0.35

5 years ago

3.0.0

5 years ago

2.0.34

5 years ago

2.0.33

5 years ago

2.0.31

5 years ago

2.0.30

5 years ago

2.0.28

5 years ago

2.0.29

5 years ago

2.0.26

5 years ago

2.0.23

5 years ago

2.0.21

6 years ago

2.0.20

6 years ago

2.0.19

6 years ago

2.0.18

6 years ago

2.0.17

6 years ago

2.0.16

6 years ago

2.0.13

6 years ago

2.0.12

6 years ago

2.0.11

7 years ago

2.0.10

7 years ago

2.0.9

7 years ago

2.0.8

7 years ago

2.0.7

7 years ago

2.0.5

7 years ago

2.0.4

7 years ago

2.0.3

7 years ago

2.0.2

7 years ago

2.0.0

7 years ago

0.1.20

7 years ago

0.1.18

8 years ago

0.1.17

8 years ago

0.1.16

8 years ago

0.1.15

8 years ago

0.1.13

9 years ago

0.1.12

9 years ago

0.1.10

9 years ago

0.1.9

9 years ago

0.1.8

9 years ago

0.1.7

9 years ago

0.1.6

9 years ago

0.1.5

9 years ago

0.1.4

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago

1.0.0-alpha-13

10 years ago

1.0.0-alpha-12

10 years ago

1.0.0-alpha-11

10 years ago

1.0.0-alpha-10

10 years ago

1.0.0-alpha-9

10 years ago

1.0.0-alpha-8

10 years ago

1.0.0-alpha-7

10 years ago

1.0.0-alpha-6

10 years ago

1.0.0-alpha-5

10 years ago

1.0.0-alpha-4

10 years ago

1.0.0-alpha-3

10 years ago

1.0.0-alpha-2

10 years ago

1.0.0-alpha-1

10 years ago

1.0.0-alpha

10 years ago

0.0.16

10 years ago

0.0.15

10 years ago

0.0.14

10 years ago

0.0.13

10 years ago

0.0.12

10 years ago

0.0.11

10 years ago

0.0.10

10 years ago

0.0.9

10 years ago

0.0.8

10 years ago

0.0.7

10 years ago

0.0.6

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3-ug

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago