Skip to content

Commit a9c0fce

Browse files
committed
docs: update README; ci/cd: add build;feat: add mobx-view-model other package name
1 parent be0fc8f commit a9c0fce

File tree

6 files changed

+90
-19
lines changed

6 files changed

+90
-19
lines changed

.github/workflows/build.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Build
2+
on:
3+
push:
4+
branches:
5+
- master
6+
7+
jobs:
8+
job:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v4
14+
15+
- uses: pnpm/action-setup@v4
16+
name: Install pnpm
17+
with:
18+
version: 9
19+
run_install: false
20+
21+
- name: Install Node.js
22+
uses: actions/setup-node@v4
23+
with:
24+
node-version: 20
25+
cache: 'pnpm'
26+
27+
- name: Install dependencies
28+
run: pnpm install
29+
30+
- name: Build the project
31+
run: pnpm build

README.md

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,46 @@
1-
# mobx-vm-entities
2-
31
MobX ViewModel power for ReactJS
42

3+
## What package has
4+
5+
### [`ViewModelImpl`](src/view-model/view-model.impl.ts), [`AbstractViewModel`](src/view-model/abstract-view-model.ts), [`ViewModel` ](src/view-model/view-model.ts)
6+
It is a class that helps to manage state and lifecycle of a component in **React**.
7+
8+
9+
### [`ViewModelStoreImpl`](src/view-model/view-model.store.impl.ts), [`AbstractViewModelStore`](src/view-model/abstract-view-model.store.ts), [`ViewModelStore`](src/view-model/view-model.store.ts)
10+
It is store for managing view models.
11+
P.S not required entity for targeted usage of this package, but can be helpful for accessing view models from everywhere by view model id or view model class name.
12+
13+
14+
### [`useViewModel()`](src/hooks/use-view-model.ts)
15+
Hook that helps to get access to your view model in **React**.
16+
Possible usage:
17+
- `useViewModel<YourViewModel>()` - using generic to define type of returning view model instance
18+
- `useViewModel<YourViewModel>(id)` - using `id` to define specific identifier of returning view model instance and generic for the same as above usage
19+
20+
21+
### [`withViewModel()`](src/hoc/with-view-model.tsx)
22+
Required for usage HOC that connects your `ViewModel` class with `View` (**React** Component)
23+
Possible usage:
24+
- `withViewModel(ViewModel)(View)` - simple usage
25+
- `withViewModel(ViewModel, { factory })(View)` - advanced usage that needed to create your own implementations of `withViewModel` HOC, `ViewModelStore` and `ViewModel` classes.Example below:
26+
```tsx
27+
withViewModel(ViewModel, {
28+
factory: (config) => {
29+
// also you can achieve this your view model store implementation
30+
return new config.VM(rootStore, config);
31+
}
32+
})(View)
33+
```
34+
35+
36+
### [`withLazyViewModel()`](src/hoc/with-lazy-view-model.tsx)
37+
Optional for usage HOC that doing the same thing as `withViewModel`, but fetching `ViewModel` and `View` "lazy"
38+
539
## Simple usage
640

741
```tsx
842
import { ViewModelProps, ViewModelImpl, withViewModel } from 'mobx-vm-entities';
43+
944
export class MyPageVM extends ViewModelImpl<{ payloadA: string }> {
1045
@observable
1146
accessor state = '';
@@ -30,7 +65,8 @@ const MyPageView = observer(({ model }: ViewModelProps<MyPageVM>) => {
3065
export const MyPage = withViewModel(MyPageVM)(MyPageView);
3166

3267
<MyPage payload={{ payloadA: '1' }} />
33-
```
68+
69+
```
3470

3571
## Detailed Configuration
3672

@@ -132,9 +168,9 @@ export class MyPageVM extends ViewModelImpl {
132168
accessor state = '';
133169

134170
async mount() {
135-
super.mount();
136-
137-
await this.rootStore.myApi.load()
171+
// this.isMounted = false;
172+
await this.rootStore.beerApi.takeBeer();
173+
super.mount(); // this.isMounted = true
138174
}
139175

140176
didMount() {

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
"keywords": [
1919
"mobx",
2020
"react",
21-
"view-model"
21+
"view-model",
22+
"view",
23+
"model"
2224
],
2325
"author": "js2me",
2426
"license": "MIT",
@@ -50,7 +52,7 @@
5052
"@vitest/coverage-istanbul": "2.1.6",
5153
"eslint": "8.57.0",
5254
"js2me-eslint-config": "1.0.4",
53-
"js2me-exports-post-build-script": "2.0.14",
55+
"js2me-exports-post-build-script": "2.0.16",
5456
"jsdom": "25.0.1",
5557
"rimraf": "6.0.1",
5658
"typescript": "5.4.5",

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

post-build.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ postBuildScript({
66
srcDirName: 'src',
77
filesToCopy: ['LICENSE', 'README.md'],
88
updateVersion: process.env.PUBLISH_VERSION,
9-
onDone: (versionsDiff, _, packageJson) => {
9+
onDone: (versionsDiff, _, packageJson, { targetPackageJson}) => {
1010
if (process.env.PUBLISH) {
1111
publishScript({
1212
nextVersion: versionsDiff?.next ?? packageJson.version,
@@ -16,6 +16,8 @@ postBuildScript({
1616
createTag: true,
1717
githubRepoLink: 'https://github.com/js2me/mobx-vm-entities',
1818
cleanupCommand: 'pnpm clean',
19+
targetPackageJson,
20+
otherNames: ['mobx-view-model']
1921
})
2022
}
2123
}

src/utils/create-vm-id-generator.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ import { AnyObject } from './types';
33

44
declare const process: { env: { NODE_ENV?: string } };
55

6-
export const generateVMId = (context: AnyObject) => {
7-
if (!context.generateId) {
6+
export const generateVMId = (ctx: AnyObject) => {
7+
if (!ctx.generateId) {
88
const staticId = internalCounter().toString(16);
99
const counter = createCounter();
1010

11-
context.generateId = () =>
11+
ctx.generateId = () =>
1212
`${staticId}_${counter().toString().padStart(5, '0')}`;
1313
}
1414

1515
if (process.env.NODE_ENV === 'production') {
16-
return context.generateId();
16+
return ctx.generateId();
1717
} else {
18-
return `${context.VM?.name}_${context.generateId()}`;
18+
return `${ctx.VM?.name}_${ctx.generateId()}`;
1919
}
2020
};

0 commit comments

Comments
 (0)