Skip to content

Commit 567be0b

Browse files
committed
Package for official release
1 parent 9cc7aa2 commit 567be0b

File tree

9 files changed

+2887
-4124
lines changed

9 files changed

+2887
-4124
lines changed

LICENSE.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2020 Dave Stewart
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

README.md

Lines changed: 51 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,36 @@
11
# Vue Class Store
22

3-
> Fully-reactive, zero-boilerplate, class-based stores for Vue
3+
> Universal Vue stores you write once and use anywhere
44
5-
## Abstract
5+
![logo](docs/logo.png)
66

7-
Vue Class Store is a one-liner upgrade that adds reactivity, computed properties, caching and watches to regular TS/ES6 classes.
7+
## Abstract
88

9-
It allows you to leverage the advanced reactivity features of Vue whilst retaining familiar classical syntax, structure and functionality, such as simple instantiation, parametized constructors and inheritance – with zero setup or bloat.
9+
### So Vue, Vuex and Vue Class Store walk into a bar...
1010

11-
With Vue Class Store there's no refactoring data into components or abstracting into modules, writing mutations or mapping getters; you simply write and instantiate classes then work with them directly.
11+
**Vue says:** "I'll give you reactivity, computed properties and watches, but only in components, only using a special objects-and-function schema, and I demand initial values be passed in from a parent component using props! I don't get along particularly well with TypeScript either, so good luck with figuring that one out, buddy"
1212

13-
Stores can be used locally or globally, outside or inside components, nested in other stores and are fully compatible with the Vue ecosystem (including Nuxt) because they are transparently converted into `Vue` instances.
13+
**Vuex says:** "I'll give you global reactivity and computed properties, but I'm going to call them esoteric names and require you set them up globally with a convoluted schema, access them only through a centralised store, and I'll force you to make all updates through string-based paths, with different formats, helpers, terminology and best practices. I'll also make it difficult to go more than a couple of levels deep, and I'll give you watches but you'll hate them so probably best not to use those either"
1414

15-
Working with stores in both the IDE and DevTools is easy as they are *just* classes, which means source maps, debugging and breakpoints work like you expect:
15+
**Vue Class Store says:** "I'll give you reactivity, computed properties and watches, written in standard JavaScript or TypeScript, with no setup or boilerplate, and you can use me anywhere"
1616

17-
![devtools](https://raw.githubusercontent.com/davestewart/vue-class-store/master/dev/devtools.png)
17+
*The end*
1818

19-
## Installation
19+
## Usage
2020

21-
### Setup
21+
### Installation
2222

2323
Install the package from NPM:
2424

2525
```bash
26-
npm i vue-class-store
27-
```
26+
#vue 2
27+
npm i vue-class-store@^2.0.0
2828

29-
```bash
30-
yarn add vue-class-store
29+
#vue 3
30+
npm i vue-class-store@^3.0.0
3131
```
3232

33-
In your main project file, install the plugin:
34-
35-
```javascript
36-
import Vue from 'vue'
37-
import VueStore from 'vue-class-store'
38-
39-
Vue.use(VueStore.install)
40-
```
41-
42-
## Usage
33+
Yarn users, replace `npm i` with `yarn add`.
4334

4435
### Declaration
4536

@@ -79,53 +70,60 @@ export class Store {
7970
}
8071
```
8172

82-
When the class is instantiated, the decorator will convert it to a new Vue instance and return it.
83-
8473
### Instantiation
8574

8675
To use a store, simply instantiate the class.
8776

8877
You can do this outside of a component, and it will be completely reactive:
8978

9079
```typescript
91-
const store = new Store(10)
80+
const store = new Store({ ... })
9281
```
9382

9483
Or you can instantiate within a component:
9584

9685
```javascript
9786
export default {
98-
props: {
99-
value: Number
100-
},
101-
87+
...
10288
computed: {
10389
model () {
104-
return new Store(this.value)
90+
return new Store({ ... })
10591
}
10692
}
10793
}
10894
```
10995

110-
Alternatively, you can make any non-decorated class reactive using the static `.create()` method:
96+
Alternatively, you can make any non-decorated class reactive on the fly using the static `.create()` method:
11197

11298
```typescript
11399
import VueStore from 'vue-class-store'
114-
import Model from './Model'
100+
import Store from './Store'
115101

116-
const reactive: Model = VueStore.create(new Model(1, 2, 3))
102+
const store: Store = VueStore.create(new Store({ ... }))
117103
```
118104

119-
Wherever you do it, the decorator will return a new reactive `Vue` instance, but your IDE will think it's an instance of the original class, and it will have *exactly* the same properties.
105+
## How it works
106+
107+
This is probably a good point to stop and explain what is happening under the hood.
108+
109+
Immediately after the class is instantiated, the decorator function extracts the class' properties and methods and rebuilds either a new Vue instance (Vue 2) or a Proxy object (Vue 3).
110+
111+
This functionally-identical object is then returned, and thanks to TypeScript generics your IDE and the TypeScript compiler will think it's an instance of the *original* class, so code completion will just work.
112+
113+
Additionally, because all methods have their scope rebound to the original class, breakpoints will stop in the right place, and you can even call the class keyword `super` and it will resolve correctly up the prototype chain.
114+
115+
![devtools](https://raw.githubusercontent.com/davestewart/vue-class-store/master/dev/devtools.png)
116+
117+
Note that the object will of course be a `Vue` or `Proxy` instance, so running code like `store instanceof Store` will return `false` .
120118

121119
## Inheritance
122120

123-
The decorator supports class inheritance, by chaining the prototypes as Vue `extends` options, meaning you can do things like this:
121+
The decorator supports class inheritance meaning you can do things like this:
124122

125123
```typescript
126124
class Rectangle {
127-
width: number
128-
height: number
125+
width = 0
126+
height = 0
129127

130128
constructor (width, height) {
131129
this.width = width
@@ -186,7 +184,7 @@ Because the class itself is reactive, you could inject it into a component tree,
186184
export default {
187185
provide () {
188186
return {
189-
$store: new Store(10)
187+
$products: new ProductsStore()
190188
}
191189
},
192190
}
@@ -195,80 +193,33 @@ export default {
195193
```javascript
196194
export default {
197195
inject: [
198-
'$store'
196+
'$products'
199197
],
200198

201199
computed: {
202-
value () {
203-
return this.$store.value
200+
items () {
201+
return this.$products.items
204202
}
205203
},
206204

207-
setValue (value) {
208-
this.$store.value = value
205+
filterProducts (value) {
206+
this.$products.filter = value
209207
}
210208
}
211209
```
212210

213-
## Nuxt
214-
215-
Because all data is passed by the constructor, Vue Class Store just works with SSR.
216-
217-
To set up, add a plugin file and config option:
218-
219-
```javascript
220-
// plugins/vue-class-store.js
221-
import Vue from 'vue'
222-
import VueStore from 'vue-class-store'
223-
224-
Vue.use(VueStore.install)
225-
```
226-
227-
```javascript
228-
// nuxt.config.js
229-
plugins: [
230-
'~/plugins/vue-class-store',
231-
],
232-
```
233-
234-
## Demo
235-
236-
### Vue
237-
238-
The demo folder compares various state management approaches; check `demo/src/examples/*` .
239-
240-
Class Store:
241-
242-
- [Basic Class Store](demo/src/examples/class-store/basic)
243-
- [Inline Class Store](demo/src/examples/class-store/inline)
244-
- [Class Store with Inheritance](demo/src/examples/class-store/inherit)
245-
- [Global Class Store](demo/src/examples/class-store/global)
246-
247-
Alternatives:
248-
249-
- [Vue Component](demo/src/examples/other/vue-component)
250-
- [Vue Model](demo/src/examples/other/vue-model)
251-
- [Vuex](demo/src/examples/other/vuex)
211+
## Development
252212

253-
To run the demo, clone the repo and install and run the `demo`:
213+
### Demo
254214

255-
```
256-
git clone https://github.com/davestewart/vue-class-store.git
257-
cd vue-class-store/demo
258-
npm install
259-
npm run demo
260-
```
215+
The library has demos for Vue 2, Vue 3 and Nuxt, and can be found in the following repo:
261216

262-
### Nuxt
217+
- https://github.com/davestewart/vue-class-store-demos
263218

264-
Nuxt has its own demo here:
219+
### Scripts
265220

266-
- https://github.com/davestewart/nuxt-class-store
267-
268-
## Development
221+
The package uses Yarn, and has only two scripts, to build for development and production:
269222

270-
The package has various scripts:
223+
- `yarn dev` - build and watch for development
224+
- `yarn build` - build for production
271225

272-
- `npm run dev` - build and watch for development
273-
- `npm run build` - build for production
274-
- `npm run demo` - run the demo

build/rollup.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import typescript from 'rollup-plugin-typescript2'
1212
import { uglify } from 'rollup-plugin-uglify'
1313

1414
const pkg = require('../package.json')
15-
const external = Object.keys(pkg.dependencies || {})
1615
const pkgName = pkg.name
1716
const className = pkgName.replace(/(^\w|-\w)/g, c => c.replace('-', '').toUpperCase())
1817

@@ -22,6 +21,9 @@ function output (ext, format = 'umd') {
2221
file: `dist/${pkgName}.${ext}`,
2322
format: format,
2423
exports: 'named',
24+
globals: {
25+
vue: 'Vue',
26+
},
2527
}
2628
}
2729

@@ -31,7 +33,9 @@ function output (ext, format = 'umd') {
3133

3234
const umd = {
3335
input: 'src/index.ts',
34-
external: external,
36+
external: [
37+
'vue'
38+
],
3539
output: output('js'),
3640
plugins: [
3741
resolve({

dev/stats.js

Lines changed: 0 additions & 16 deletions
This file was deleted.
File renamed without changes.

docs/logo.png

55.2 KB
Loading

0 commit comments

Comments
 (0)