Skip to content

Commit 7176cbe

Browse files
committed
feat: 增加监听store change的功能$watchStoreChange
1 parent 42525d9 commit 7176cbe

File tree

7 files changed

+62
-40
lines changed

7 files changed

+62
-40
lines changed

README.md

+14-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
2-
31
# jgb-store
42

5-
jgb数据流插件。用于多页面、多组件数据同步更新。
3+
jgb 数据流插件。用于多页面、多组件数据同步更新。
64

75
## 使用
86

@@ -17,20 +15,20 @@ import { init } from 'jgb-store';
1715
init();
1816
```
1917

20-
### 定义store
18+
### 定义 store
2119

2220
```ts
2321
// store.ts
2422
import { createStore } from 'jgb-store';
2523

26-
export const store = createStore({
24+
export const store = createStore({
2725
data: {
2826
testStore: 1
2927
}
3028
});
3129
```
3230

33-
### 页面使用store
31+
### 页面使用 store
3432

3533
```ts
3634
// pages/index.ts
@@ -52,40 +50,40 @@ JPage({
5250
});
5351
```
5452

55-
56-
5753
### 扩展参数说明
5854

5955
#### Page or Component
6056

61-
* `$useAll`
57+
- `$useAll`
6258

6359
判断是否全部使用`store.data``app.globalStore.data`
6460

6561
**默认只有在`data`中定义的同名数据才会赋值**
6662

67-
* `$store`
63+
- `$store`
6864

6965
使用定义的`store`数据
7066

71-
72-
7367
#### App
7468

75-
* globalStore
69+
- globalStore
7670

7771
全局`store`, 当时用`$useAll`时会更新此数据
7872

7973
### 扩展属性说明
8074

81-
* `$store` - `InnerStore`
75+
- `$store` - `InnerStore`
8276

8377
`$store`参数的实例化
8478

85-
* `$update`
79+
- `$update`
8680

8781
更新`store`数据变化,使用`diff`更新
8882

83+
- `$watchStoreChange`
84+
85+
监听当前页面或组件`store`数据变化
86+
8987
## 感谢
9088

91-
[westore](https://github.com/Tencent/westore)
89+
[westore](https://github.com/Tencent/westore)

example/src/component/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ JComponent({
77
data: {},
88
attached() {
99
console.log(this.data);
10+
this.$watchStoreChange(() => {
11+
console.log('component: watchStoreChange');
12+
});
1013
},
1114
methods: {
1215
onTap() {

example/src/pages/index/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ JPage({
99
},
1010
onLoad() {
1111
console.log(this.data);
12+
this.$watchStoreChange((diff: any) => {
13+
console.log('diff', diff);
14+
});
1215
},
1316
onTap() {
1417
this.$store.data.storeData = 'page';

example/src/store/index.ts

-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ const data = {
66

77
export const store = createStore({
88
data,
9-
onChange(diff, ctx) {
10-
console.log('diff =>', ctx);
11-
},
129
updateStore() {
1310
this.$update();
1411
}

src/create.ts

+17-12
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ export function create(
4040
defineReadOnly(this, '$store', store);
4141
defineReadOnly(this, '$update', storeUpdate);
4242
defineReadOnly(this, '$useAll', !!opts.$useAll);
43+
defineReadOnly(this, '$watchStoreChange', function(this: any, cb: any) {
44+
store.onChange(cb, this);
45+
});
4346

4447
store.addInstance(this);
4548
globalStore.update = this.$update;
@@ -59,23 +62,25 @@ export function create(
5962
}
6063

6164
if (type === 'Component') {
65+
hook(opts, 'created', function(this: IPageExtenstion) {
66+
defineReadOnly(this, '$store', store);
67+
defineReadOnly(this, '$update', storeUpdate);
68+
defineReadOnly(this, '$useAll', !!opts.$useAll);
69+
defineReadOnly(this, '$watchStoreChange', function(this: any, cb: any) {
70+
store.onChange(cb, this);
71+
});
72+
});
6273
// Component.attached -> Page.onLoad -> Component.ready
63-
// 要在ready注册
74+
// 获取当前页面路径要在ready注册
6475
hook(opts, 'ready', function(this: IPageExtenstion) {
6576
const curPage: IPageExtenstion = getPage() as any;
6677
// @ts-ignore
6778
this[page] = curPage;
68-
if (curPage.$store) {
69-
this.globalStore = curPage.globalStore;
70-
defineReadOnly(this, '$store', store);
71-
defineReadOnly(this, '$update', storeUpdate);
72-
defineReadOnly(this, '$useAll', !!opts.$useAll);
73-
74-
this.route = curPage.route;
75-
store.addInstance(this);
76-
getInitState($store.data, opts.data, opts.$useAll);
77-
this.setData(opts.data);
78-
}
79+
this.globalStore = curPage.globalStore;
80+
this.route = curPage.route;
81+
store.addInstance(this);
82+
getInitState($store.data, opts.data, opts.$useAll);
83+
this.setData(opts.data);
7984
});
8085

8186
hook(opts, 'detached', function(this: IPageExtenstion) {

src/store.ts

+21-9
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,18 @@ interface Data<D extends DataOption> {
2121
* `data`
2222
*/
2323
data: D;
24-
25-
/**
26-
* 监听数据变化,返回diff内容
27-
* @param ctx Page or Component 的context
28-
*/
29-
onChange?: (diff: IAnyObject, ctx: any) => void;
3024
}
3125

3226
type Options<
3327
TData extends DataOption,
3428
TCustom extends CustomOption
3529
> = (TCustom & Partial<Data<TData>>) & ThisType<Instance<TData, TCustom>>;
3630

31+
/**
32+
* store change callback
33+
*/
34+
type IDiffCallback = (diff: IAnyObject) => void;
35+
3736
/**
3837
* 创建store
3938
* */
@@ -46,6 +45,7 @@ export function createStore<
4645

4746
export class InnerStore {
4847
data: IAnyObject;
48+
private cbs = [] as { cb: IDiffCallback; ctx: any }[];
4949

5050
constructor(private $store: any) {
5151
this.data = $store.data || {};
@@ -74,8 +74,10 @@ export class InnerStore {
7474
}
7575
promiseArr.push(
7676
setState(vm, obj).then(diff => {
77-
if (Object.keys(diff).length && this.$store.onChange) {
78-
this.$store.onChange(diff, vm);
77+
if (!diff || Object.keys(diff).length === 0) return;
78+
const cbs = this.cbs.filter(({ ctx }) => ctx === vm);
79+
for (const { ctx, cb } of cbs) {
80+
cb.call(ctx, diff);
7981
}
8082
})
8183
);
@@ -87,6 +89,16 @@ export class InnerStore {
8789
return innerInstances.get(this.$store) || {};
8890
}
8991

92+
/**
93+
* 绑定store change回调
94+
*/
95+
onChange(cb: IDiffCallback, ctx: any) {
96+
if (typeof cb !== 'function')
97+
throw new Error(`onChange callback should be a Function`);
98+
99+
this.cbs.push({ cb, ctx });
100+
}
101+
90102
/**
91103
* 添加当前页面/组件实例
92104
*/
@@ -112,7 +124,7 @@ export function getGlobalStore(): IAnyObject {
112124
return getApp().globalStore || {};
113125
}
114126

115-
const newState = Symbol()
127+
const newState = Symbol();
116128

117129
/**
118130
* 设置数据

types/extension.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ interface IStoreExtensionInstance {
3333
* @returns Promise<diff> 返回差异
3434
*/
3535
$update: IStoreUpdate;
36+
/**
37+
* 监听store变化
38+
*/
39+
$watchStoreChange(diff: IAnyObject): void;
3640
}
3741

3842
declare module 'jgb-weapp/types/JComponent' {

0 commit comments

Comments
 (0)