├── .editorconfig
├── .gitignore
├── .travis.yml
├── CODE_OF_CONDUCT.md
├── README.md
├── devtools.d.ts
├── devtools.js
├── index.d.ts
├── package.json
├── preact.d.ts
├── react.d.ts
├── src
├── combined
│ ├── preact.js
│ └── react.js
├── index.js
├── integrations
│ ├── preact.js
│ └── react.js
└── util.js
└── test
├── fixtures
└── preact-8.min.js
├── preact
├── builds.test.js
├── preact-8.test.js
└── preact.test.js
├── react
├── .babelrc
├── builds.test.js
└── react.test.js
└── unistore.test.js
/.editorconfig:
--------------------------------------------------------------------------------
1 | root = true
2 |
3 | [*]
4 | indent_style = tab
5 | end_of_line = lf
6 | charset = utf-8
7 | trim_trailing_whitespace = true
8 | insert_final_newline = true
9 |
10 | [{package.json,.*rc,*.yml}]
11 | indent_style = space
12 | indent_size = 2
13 |
14 | [*.md]
15 | trim_trailing_whitespace = false
16 | indent_style = space
17 | indent_size = 2
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | npm-debug.log
3 | /dist
4 | /full
5 | package-lock.json
6 | /preact.js
7 | /preact.js.map
8 | /react.js
9 | /react.js.map
10 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 | node_js:
3 | - 10
4 |
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Contributor Covenant Code of Conduct
2 |
3 | ## Our Pledge
4 |
5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6 |
7 | ## Our Standards
8 |
9 | Examples of behavior that contributes to creating a positive environment include:
10 |
11 | * Using welcoming and inclusive language
12 | * Being respectful of differing viewpoints and experiences
13 | * Gracefully accepting constructive criticism
14 | * Focusing on what is best for the community
15 | * Showing empathy towards other community members
16 |
17 | Examples of unacceptable behavior by participants include:
18 |
19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances
20 | * Trolling, insulting/derogatory comments, and personal or political attacks
21 | * Public or private harassment
22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission
23 | * Other conduct which could reasonably be considered inappropriate in a professional setting
24 |
25 | ## Our Responsibilities
26 |
27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28 |
29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30 |
31 | ## Scope
32 |
33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
34 |
35 | ## Enforcement
36 |
37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at jason@developit.ca. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
38 |
39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
40 |
41 | ## Attribution
42 |
43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
44 |
45 | [homepage]: http://contributor-covenant.org
46 | [version]: http://contributor-covenant.org/version/1/4/
47 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | # unistore
8 |
9 | > A tiny 350b centralized state container with component bindings for [Preact] & [React].
10 |
11 | - **Small** footprint complements Preact nicely _(unistore + unistore/preact is ~650b)_
12 | - **Familiar** names and ideas from Redux-like libraries
13 | - **Useful** data selectors to extract properties from state
14 | - **Portable** actions can be moved into a common place and imported
15 | - **Functional** actions are just reducers
16 | - **NEW**: seamlessly run Unistore in a worker via [Stockroom](https://github.com/developit/stockroom)
17 |
18 | ## Table of Contents
19 |
20 | - [Install](#install)
21 | - [Usage](#usage)
22 | - [Examples](#examples)
23 | - [API](#api)
24 | - [License](#license)
25 |
26 | ## Install
27 |
28 | This project uses [node](http://nodejs.org) and [npm](https://npmjs.com). Go check them out if you don't have them locally installed.
29 |
30 | ```sh
31 | npm install --save unistore
32 | ```
33 |
34 | Then with a module bundler like [webpack](https://webpack.js.org) or [rollup](http://rollupjs.org), use as you would anything else:
35 |
36 | ```js
37 | // The store:
38 | import createStore from 'unistore'
39 |
40 | // Preact integration
41 | import { Provider, connect } from 'unistore/preact'
42 |
43 | // React integration
44 | import { Provider, connect } from 'unistore/react'
45 | ```
46 |
47 | Alternatively, you can import the "full" build for each, which includes both `createStore` and the integration for your library of choice:
48 |
49 | ```js
50 | import { createStore, Provider, connect } from 'unistore/full/preact'
51 | ```
52 |
53 | The [UMD](https://github.com/umdjs/umd) build is also available on [unpkg](https://unpkg.com):
54 |
55 | ```html
56 |
57 |
58 |
59 |
60 |
61 |
62 | ```
63 |
64 | You can find the library on `window.unistore`.
65 |
66 | ### Usage
67 |
68 | ```js
69 | import createStore from 'unistore'
70 | import { Provider, connect } from 'unistore/preact'
71 |
72 | let store = createStore({ count: 0, stuff: [] })
73 |
74 | let actions = {
75 | // Actions can just return a state update:
76 | increment(state) {
77 | // The returned object will be merged into the current state
78 | return { count: state.count+1 }
79 | },
80 |
81 | // The above example as an Arrow Function:
82 | increment2: ({ count }) => ({ count: count+1 }),
83 |
84 | // Actions receive current state as first parameter and any other params next
85 | // See the "Increment by 10"-button below
86 | incrementBy: ({ count }, incrementAmount) => {
87 | return { count: count+incrementAmount }
88 | },
89 | }
90 |
91 | // If actions is a function, it gets passed the store:
92 | let actionFunctions = store => ({
93 | // Async actions can be pure async/promise functions:
94 | async getStuff(state) {
95 | const res = await fetch('/foo.json')
96 | return { stuff: await res.json() }
97 | },
98 |
99 | // ... or just actions that call store.setState() later:
100 | clearOutStuff(state) {
101 | setTimeout(() => {
102 | store.setState({ stuff: [] }) // clear 'stuff' after 1 second
103 | }, 1000)
104 | }
105 |
106 | // Remember that the state passed to the action function could be stale after
107 | // doing async work, so use getState() instead:
108 | async incrementAfterStuff(state) {
109 | const res = await fetch('foo.json')
110 | const resJson = await res.json()
111 | // the variable 'state' above could now be old,
112 | // better get a new one from the store
113 | const upToDateState = store.getState()
114 |
115 | return {
116 | stuff: resJson,
117 | count: upToDateState.count + resJson.length,
118 | }
119 | }
120 | })
121 |
122 | // Connecting a react/preact component to get current state and to bind actions
123 | const App1 = connect('count', actions)(
124 | ({ count, increment, incrementBy }) => (
125 |
126 |
Count: {count}
127 |
128 |
129 |
130 | )
131 | )
132 |
133 | // First argument to connect can also be a string, array or function while
134 | // second argument can be an object or a function. Here we pass an array and
135 | // a function.
136 | const App2 = connect(['count', 'stuff'], actionFunctions)(
137 | ({ count, stuff, getStuff, clearOutStuff, incrementAfterStuff }) => (
138 |