Skip to content

Commit 8616687

Browse files
committed
feat: add global useWatch/useSubscribe/form.watch
1 parent fc17f62 commit 8616687

33 files changed

+646
-660
lines changed

.vscode/settings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
"strings": "on"
77
}
88
}
9-
}
9+
}

scripts/generateApiData.js

-167
This file was deleted.

scripts/generateApimd.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,16 @@ function writeMarkdownFileSync(fileName, doscInstance) {
111111
const outputFilePath = path.join(outputDir, `${fileName}.md`);
112112
const { props, tags } = doscInstance;
113113

114-
const { group, groupOrder, embed, name, pageTitle, order, toc } = tags || {};
114+
const { group, groupOrder, embed, name, pageTitle, order, toc, version } =
115+
tags || {};
115116

116117
const content = generateWriteContent(props);
117118

118119
const title = name || fileName;
119120

120121
const groupStr = `
121122
group:
122-
title: ${group}
123+
title: ${group || ''}
123124
order: ${groupOrder || 0}
124125
`;
125126

@@ -134,7 +135,7 @@ nav:
134135
title: ${title}
135136
order: ${order || 0}
136137
${toc === 'false' ? '' : tocStr}
137-
${group ? groupStr : ''}
138+
${group || groupOrder ? groupStr : ''}
138139
---
139140
140141
`;

src/utils/controller.ts renamed to src/driver/ControllerDriver/controller.ts

+6-67
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
import { cloneDeep } from 'lodash';
22
import ProxyPolyfillBuilder from 'proxy-polyfill/src/proxy';
33
import React from 'react';
4-
import { isObject, isObjectOrArray } from './util';
4+
import type { DefaultRecord } from 'react-form-simple';
5+
import { isObject, isObjectOrArray } from 'react-form-simple/utils/util';
56

6-
export const proxyMap = new WeakMap();
7-
export const rawMap = new WeakMap();
8-
9-
const _proxyMap = proxyMap;
10-
const _rawMap = rawMap;
11-
12-
const proxyPolyfill = ProxyPolyfillBuilder();
13-
const Proxys = window.Proxy || proxyPolyfill;
7+
const ProxyPolyfill = window.Proxy || ProxyPolyfillBuilder();
148

159
export type ObserverOptions = {
1610
path?: string[];
17-
proxyMap: WeakMap<object, any>;
18-
rawMap: WeakMap<object, any>;
1911
onChangeLength?: () => void;
2012
};
2113

@@ -131,28 +123,14 @@ export const updateProxyValue = (
131123
}
132124
};
133125

134-
export const observer = <T extends object>(
126+
export const observer = <T extends DefaultRecord>(
135127
initialVal: T,
136128
cb?: (args: ObserverCb) => void,
137129
options?: ObserverOptions,
138130
): T => {
139-
const {
140-
path = [],
141-
onChangeLength,
142-
proxyMap,
143-
rawMap,
144-
} = (options || {}) as ObserverOptions;
145-
146-
const existingProxy = proxyMap.get(initialVal);
147-
if (existingProxy) {
148-
return existingProxy;
149-
}
150-
151-
if (rawMap.has(initialVal)) {
152-
return initialVal;
153-
}
131+
const { path = [], onChangeLength } = (options || {}) as ObserverOptions;
154132

155-
const proxy = new Proxys(initialVal, {
133+
const proxy = new ProxyPolyfill(initialVal, {
156134
get(target, key, receiver) {
157135
const ret = Reflect.get(target, key, receiver);
158136
if (React.isValidElement(ret)) return ret;
@@ -184,42 +162,3 @@ export const observer = <T extends object>(
184162

185163
return proxy;
186164
};
187-
188-
export const createControllerObserver = <T extends object>(
189-
initialVal: T,
190-
cb?: (args: ObserverCb) => void,
191-
options?: ObserverOptions,
192-
): T => {
193-
const { path = [], rawMap = _rawMap, proxyMap = _proxyMap } = options || {};
194-
const existingProxy = proxyMap.get(initialVal);
195-
if (existingProxy) {
196-
return existingProxy;
197-
}
198-
199-
if (rawMap.has(initialVal)) {
200-
return initialVal;
201-
}
202-
203-
const proxy = new Proxys(initialVal, {
204-
get(target, key, receiver) {
205-
const ret = Reflect.get(target, key, receiver);
206-
if (React.isValidElement(ret)) return ret;
207-
return isObjectOrArray(ret)
208-
? createControllerObserver(ret as T, cb, {
209-
...(options as ObserverOptions),
210-
path: [...path, key.toString()],
211-
})
212-
: ret;
213-
},
214-
set(target, key, val) {
215-
const newPath = [...path, key.toString()];
216-
const ret = Reflect.set(target, key, val);
217-
cb?.({ path: newPath.join('.'), value: val });
218-
return ret;
219-
},
220-
});
221-
222-
proxyMap.set(initialVal, proxy);
223-
rawMap.set(proxy, initialVal);
224-
return proxy;
225-
};

src/driver/ControllerDriver/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './controller';

src/driver/ObserverDriver/Factory.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import SubscribeMediator from './mediator/SubscribeMediator';
2+
import WatchMediator from './mediator/WatchMediator';
3+
4+
export class ObserverFactory<T> {
5+
public watchManager: WatchMediator<T> = {} as WatchMediator<T>;
6+
public subscribeManager: SubscribeMediator<T> = {} as SubscribeMediator<T>;
7+
8+
private checkState(instance: WatchMediator<T> | SubscribeMediator<T>) {
9+
return instance.getMountedState && instance.getMountedState();
10+
}
11+
create(type: 'watch' | 'subscribe') {
12+
switch (type) {
13+
case 'watch':
14+
if (!this.checkState(this.watchManager)) {
15+
this.watchManager = new WatchMediator<T>();
16+
}
17+
case 'subscribe':
18+
if (!this.checkState(this.subscribeManager)) {
19+
this.subscribeManager = new SubscribeMediator<T>();
20+
}
21+
}
22+
}
23+
}
24+
25+
export default ObserverFactory;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export abstract class AbstractImp {
2+
abstract emit(...args: any[]): void;
3+
abstract update(...args: any[]): void;
4+
}
5+
6+
export default AbstractImp;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { isMeaningful } from 'react-form-simple/utils/util';
2+
import type { KeyType } from '../type';
3+
4+
export abstract class AbstractObserver<
5+
T extends { key: KeyType; destroy: () => void; emit: () => void },
6+
> {
7+
protected readonly queue: T[] = [];
8+
protected mounted: boolean = false;
9+
abstract getMountedState(): boolean;
10+
constructor() {
11+
this.mounted = true;
12+
}
13+
public notify() {
14+
this.get().forEach((observer) => void observer.emit?.());
15+
}
16+
protected get(index: number): T;
17+
protected get(): T[];
18+
protected get(index?: number) {
19+
return isMeaningful(index) ? this.queue[index as number] : this.queue;
20+
}
21+
22+
abstract register(...args: any[]): void;
23+
protected findKeyIndex(key: KeyType) {
24+
return this.get().findIndex((v) => v.key === key);
25+
}
26+
public destroy(key: KeyType) {
27+
const index = this.findKeyIndex(key);
28+
if (index >= 0) {
29+
const instance = this.get(index);
30+
instance.destroy();
31+
this.get().splice(index, 1);
32+
}
33+
}
34+
}
35+
36+
export default AbstractObserver;

0 commit comments

Comments
 (0)