Skip to content

Commit 0c07e21

Browse files
committed
0.5
1 parent 95c50b7 commit 0c07e21

File tree

4 files changed

+47
-9
lines changed

4 files changed

+47
-9
lines changed

README.md

+30
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,36 @@ document.addEventListener('DOMContentLoaded', () => {
111111
})
112112
```
113113

114+
#### Define A Global Store
115+
116+
```tsx
117+
import { define } from 'mobz'
118+
119+
120+
const Counter = define(() => ({
121+
count: 0,
122+
inc() { this.count++ }
123+
}))
124+
125+
const store = new Counter(1) // Use new to construct outside the component
126+
127+
function Inc() {
128+
const count = store(s => s.count)
129+
const inc = store(s => s.inc)
130+
131+
return <div>
132+
<div>{count}</div>
133+
<button onClick={inc}>Inc</button>
134+
</div>
135+
}
136+
137+
document.addEventListener('DOMContentLoaded', () => {
138+
render(<div id='app'>
139+
<Inc></Inc>
140+
</div>, document.querySelector('#app'))
141+
})
142+
```
143+
114144
## Hooks
115145

116146
hook version of mobx basic api

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mobz",
3-
"version": "0.4.0",
3+
"version": "0.5.0",
44
"description": "zustand style mobx api",
55
"main": "./dist/mobz.cjs.js",
66
"module": "./dist/mobz.es.js",

src/mobz.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,22 @@ export function create<T extends object>(obj: NoFunc<T> | CreateFn<T>): T & UseS
168168
})
169169
}
170170

171-
export function define<T extends object>(def: (self: () => T) => NoFunc<T>): () => T & UseStore<T>
172-
export function define<T extends object, A extends any[]>(def: (self: () => T) => (...args: A) => T): (...args: A) => T & UseStore<T>
171+
export function define<T extends object>(def: (self: () => T) => NoFunc<T>): (() => T & UseStore<T>) & (new () => T & UseStore<T>)
172+
export function define<T extends object, A extends any[]>(def: (self: () => T) => (...args: A) => T): ((...args: A) => T & UseStore<T>) & (new (...args: A) => T & UseStore<T>)
173173
export function define<T extends object, A extends any[]>(def: (self: () => T) => NoFunc<T> | ((...args: A) => T)): (...args: A) => T & UseStore<T> {
174-
return (...args: any) => useState(() => create<T>(self => {
175-
const r = def(self)
176-
if (typeof r === 'function') {
177-
return (r as any)(...args)
174+
return function (...args: any) {
175+
function build() {
176+
return create<T>(self => {
177+
const r = def(self)
178+
if (typeof r === 'function') {
179+
return (r as any)(...args)
180+
}
181+
return r
182+
})
178183
}
179-
return r
180-
}))[0]
184+
if (!new.target) return useState(build)[0]
185+
return build()
186+
}
181187
}
182188

183189
export default create;

src/tests/inc.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const useCounter = define<{ count: number, inc: () => void }, [a: number]>(self
2222
inc() { self().count++ }
2323
}))
2424

25+
//const store = new useCounter(-1)
26+
2527
function Inc() {
2628
const store = useCounter(1)
2729
const count = store(s => s.count)

0 commit comments

Comments
 (0)