Match-id-caaaaddbbc27d8d6fc7beb95bf9fc050a8f86051
This commit is contained in:
parent
93f529b55e
commit
072ceecd13
|
@ -25,7 +25,7 @@ import {
|
|||
useReducer,
|
||||
useRef,
|
||||
useState,
|
||||
useDebugValue
|
||||
useDebugValue,
|
||||
} from './src/renderer/hooks/HookExternal';
|
||||
import { asyncUpdates } from './src/renderer/TreeBuilder';
|
||||
import { callRenderQueueImmediate } from './src/renderer/taskExecutor/RenderQueue';
|
||||
|
@ -86,7 +86,7 @@ const Horizon = {
|
|||
useStore,
|
||||
clearStore,
|
||||
reduxAdapter,
|
||||
watch
|
||||
watch,
|
||||
};
|
||||
|
||||
export const version = __VERSION__;
|
||||
|
@ -127,7 +127,7 @@ export {
|
|||
useStore,
|
||||
clearStore,
|
||||
reduxAdapter,
|
||||
watch
|
||||
watch,
|
||||
};
|
||||
|
||||
export default Horizon;
|
||||
|
|
|
@ -7,7 +7,6 @@ import { launchUpdateFromVNode } from '../../renderer/TreeBuilder';
|
|||
import { getProcessingVNode } from '../../renderer/GlobalVar';
|
||||
import { VNode } from '../../renderer/vnode/VNode';
|
||||
export interface IObserver {
|
||||
|
||||
useProp: (key: string) => void;
|
||||
|
||||
addListener: (listener: () => void) => void;
|
||||
|
@ -26,14 +25,13 @@ export interface IObserver {
|
|||
}
|
||||
|
||||
export class Observer implements IObserver {
|
||||
|
||||
vNodeKeys = new WeakMap();
|
||||
|
||||
keyVNodes = new Map();
|
||||
|
||||
listeners: (() => void)[] = [];
|
||||
|
||||
watchers={} as {[key:string]:((key:string, oldValue:any, newValue:any)=>void)[]}
|
||||
watchers = {} as { [key: string]: ((key: string, oldValue: any, newValue: any) => void)[] };
|
||||
|
||||
useProp(key: string | symbol): void {
|
||||
const processingVNode = getProcessingVNode();
|
||||
|
|
|
@ -22,8 +22,8 @@ function get(rawObj: any[], key: string, receiver: any) {
|
|||
observer.watchers[prop].push(handler);
|
||||
return () => {
|
||||
observer.watchers[prop] = observer.watchers[prop].filter(cb => cb !== handler);
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
if (isValidIntegerKey(key) || key === 'length') {
|
||||
|
|
|
@ -44,8 +44,8 @@ function get(rawObj: { size: number }, key: any, receiver: any): any {
|
|||
observer.watchers[prop].push(handler);
|
||||
return () => {
|
||||
observer.watchers[prop] = observer.watchers[prop].filter(cb => cb !== handler);
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
return Reflect.get(rawObj, key, receiver);
|
||||
|
|
|
@ -4,5 +4,5 @@ export function watch(stateVariable:any,listener:(state:any)=>void){
|
|||
|
||||
return () => {
|
||||
stateVariable.removeListener(listener);
|
||||
}
|
||||
};
|
||||
}
|
|
@ -35,9 +35,7 @@ type StoreHandler<S extends object, A extends UserActions<S>, C extends UserComp
|
|||
$queue: QueuedStoreActions<S, A>;
|
||||
$a: StoreActions<S, A>;
|
||||
$c: UserComputedValues<S>;
|
||||
} & { [K in keyof S]: S[K] } &
|
||||
{ [K in keyof A]: Action<A[K], S> } &
|
||||
{ [K in keyof C]: ReturnType<C[K]> };
|
||||
} & { [K in keyof S]: S[K] } & { [K in keyof A]: Action<A[K], S> } & { [K in keyof C]: ReturnType<C[K]> };
|
||||
|
||||
type PlannedAction<S extends object, F extends ActionFunction<S>> = {
|
||||
action: string;
|
||||
|
@ -103,7 +101,7 @@ export function createStore<S extends object, A extends UserActions<S>, C extend
|
|||
const $a: Partial<StoreActions<S, A>> = {};
|
||||
const $queue: Partial<StoreActions<S, A>> = {};
|
||||
const $c: Partial<ComputedValues<S, C>> = {};
|
||||
const handler = ({
|
||||
const handler = {
|
||||
$subscribe,
|
||||
$unsubscribe,
|
||||
$a: $a as StoreActions<S, A>,
|
||||
|
@ -111,7 +109,7 @@ export function createStore<S extends object, A extends UserActions<S>, C extend
|
|||
$c: $c as ComputedValues<S, C>,
|
||||
$config: config,
|
||||
$queue: $queue as QueuedStoreActions<S, A>,
|
||||
} as unknown) as StoreHandler<S, A, C>;
|
||||
} as unknown as StoreHandler<S, A, C>;
|
||||
|
||||
function tryNextAction() {
|
||||
if (!plannedActions.length) {
|
||||
|
@ -227,7 +225,7 @@ function hookStore() {
|
|||
|
||||
if (processingVNode.tag === FunctionComponent) {
|
||||
// from FunctionComponent
|
||||
const vNodeRef = (useRef(null) as unknown) as { current: VNode };
|
||||
const vNodeRef = useRef(null) as unknown as { current: VNode };
|
||||
vNodeRef.current = processingVNode;
|
||||
|
||||
useEffect(() => {
|
||||
|
|
|
@ -1,12 +1,6 @@
|
|||
import type { VNode } from '../Types';
|
||||
|
||||
import {
|
||||
ContextProvider,
|
||||
DomComponent,
|
||||
DomPortal,
|
||||
TreeRoot,
|
||||
SuspenseComponent,
|
||||
} from '../vnode/VNodeTags';
|
||||
import { ContextProvider, DomComponent, DomPortal, TreeRoot, SuspenseComponent } from '../vnode/VNodeTags';
|
||||
import { setContext, setNamespaceCtx } from '../ContextSaver';
|
||||
import { FlagUtils } from '../vnode/VNodeFlags';
|
||||
import { onlyUpdateChildVNodes } from '../vnode/VNodeCreator';
|
||||
|
@ -40,11 +34,7 @@ export function captureVNode(processing: VNode): VNode | null {
|
|||
|
||||
if (processing.tag !== SuspenseComponent) {
|
||||
// 该vNode没有变化,不用进入capture,直接复用。
|
||||
if (
|
||||
!processing.isCreated &&
|
||||
processing.oldProps === processing.props &&
|
||||
!processing.shouldUpdate
|
||||
) {
|
||||
if (!processing.isCreated && processing.oldProps === processing.props && !processing.shouldUpdate) {
|
||||
// 复用还需对stack进行处理
|
||||
handlerContext(processing);
|
||||
|
||||
|
|
|
@ -1,24 +1,24 @@
|
|||
import { createStore } from "@cloudsop/horizon/src/horizonx/store/StoreHandler";
|
||||
import { watch } from "@cloudsop/horizon/src/horizonx/proxy/watch";
|
||||
import { createStore } from '@cloudsop/horizon/src/horizonx/store/StoreHandler';
|
||||
import { watch } from '@cloudsop/horizon/src/horizonx/proxy/watch';
|
||||
|
||||
describe("watch",()=>{
|
||||
describe('watch', () => {
|
||||
it('shouhld watch promitive state variable', async () => {
|
||||
const useStore = createStore({
|
||||
state: {
|
||||
variable:'x'
|
||||
variable: 'x',
|
||||
},
|
||||
actions: {
|
||||
change:(state)=>state.variable = "a"
|
||||
}
|
||||
change: state => (state.variable = 'a'),
|
||||
},
|
||||
});
|
||||
|
||||
const store = useStore();
|
||||
let counter = 0;
|
||||
|
||||
watch(store.$s,(state)=>{
|
||||
watch(store.$s, state => {
|
||||
counter++;
|
||||
expect(state.variable).toBe('a');
|
||||
})
|
||||
});
|
||||
|
||||
store.change();
|
||||
|
||||
|
@ -27,11 +27,11 @@ describe("watch",()=>{
|
|||
it('shouhld watch object variable', async () => {
|
||||
const useStore = createStore({
|
||||
state: {
|
||||
variable:'x'
|
||||
variable: 'x',
|
||||
},
|
||||
actions: {
|
||||
change:(state)=>state.variable = "a"
|
||||
}
|
||||
change: state => (state.variable = 'a'),
|
||||
},
|
||||
});
|
||||
|
||||
const store = useStore();
|
||||
|
@ -39,7 +39,7 @@ describe("watch",()=>{
|
|||
|
||||
store.$s.watch('variable', () => {
|
||||
counter++;
|
||||
})
|
||||
});
|
||||
|
||||
store.change();
|
||||
|
||||
|
@ -49,11 +49,11 @@ describe("watch",()=>{
|
|||
it('shouhld watch array item', async () => {
|
||||
const useStore = createStore({
|
||||
state: {
|
||||
arr:['x']
|
||||
arr: ['x'],
|
||||
},
|
||||
actions: {
|
||||
change:(state)=>state.arr[0]='a'
|
||||
}
|
||||
change: state => (state.arr[0] = 'a'),
|
||||
},
|
||||
});
|
||||
|
||||
const store = useStore();
|
||||
|
@ -61,7 +61,7 @@ describe("watch",()=>{
|
|||
|
||||
store.arr.watch('0', () => {
|
||||
counter++;
|
||||
})
|
||||
});
|
||||
|
||||
store.change();
|
||||
|
||||
|
@ -71,13 +71,11 @@ describe("watch",()=>{
|
|||
it('shouhld watch collection item', async () => {
|
||||
const useStore = createStore({
|
||||
state: {
|
||||
collection:new Map([
|
||||
['a', 'a'],
|
||||
])
|
||||
collection: new Map([['a', 'a']]),
|
||||
},
|
||||
actions: {
|
||||
change:(state)=>state.collection.set('a','x')
|
||||
}
|
||||
change: state => state.collection.set('a', 'x'),
|
||||
},
|
||||
});
|
||||
|
||||
const store = useStore();
|
||||
|
@ -85,7 +83,7 @@ describe("watch",()=>{
|
|||
|
||||
store.collection.watch('a', () => {
|
||||
counter++;
|
||||
})
|
||||
});
|
||||
|
||||
store.change();
|
||||
|
||||
|
@ -96,12 +94,12 @@ describe("watch",()=>{
|
|||
const useStore = createStore({
|
||||
state: {
|
||||
bool1: true,
|
||||
bool2:false
|
||||
bool2: false,
|
||||
},
|
||||
actions: {
|
||||
toggle1:state=>state.bool1=!state.bool1,
|
||||
toggle2:state=>state.bool2=!state.bool2
|
||||
}
|
||||
toggle1: state => (state.bool1 = !state.bool1),
|
||||
toggle2: state => (state.bool2 = !state.bool2),
|
||||
},
|
||||
});
|
||||
|
||||
let counter1 = 0;
|
||||
|
@ -110,7 +108,7 @@ describe("watch",()=>{
|
|||
|
||||
watch(store.$s, () => {
|
||||
counterAll++;
|
||||
})
|
||||
});
|
||||
|
||||
store.$s.watch('bool1', () => {
|
||||
counter1++;
|
||||
|
@ -128,5 +126,5 @@ describe("watch",()=>{
|
|||
|
||||
expect(counter1).toBe(3);
|
||||
expect(counterAll).toBe(6);
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
|
|
|
@ -8,18 +8,18 @@ const { unmountComponentAtNode } = Horizon;
|
|||
const useStore1 = createStore({
|
||||
state: { counter: 1 },
|
||||
actions: {
|
||||
add:(state)=>state.counter++,
|
||||
reset: (state)=>state.counter=1
|
||||
}
|
||||
})
|
||||
add: state => state.counter++,
|
||||
reset: state => (state.counter = 1),
|
||||
},
|
||||
});
|
||||
|
||||
const useStore2 = createStore({
|
||||
state: { counter2: 1 },
|
||||
actions: {
|
||||
add2:(state)=>state.counter2++,
|
||||
reset: (state)=>state.counter2=1
|
||||
}
|
||||
})
|
||||
add2: state => state.counter2++,
|
||||
reset: state => (state.counter2 = 1),
|
||||
},
|
||||
});
|
||||
|
||||
describe('Using multiple stores', () => {
|
||||
let container: HTMLElement | null = null;
|
||||
|
@ -65,9 +65,11 @@ describe('Using multiple stores', () => {
|
|||
>
|
||||
add
|
||||
</button>
|
||||
<p id={RESULT_ID}>{counter} {counter2}</p>
|
||||
<p id={RESULT_ID}>
|
||||
{counter} {counter2}
|
||||
</p>
|
||||
</div>
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -76,28 +78,25 @@ describe('Using multiple stores', () => {
|
|||
expect(document.getElementById(RESULT_ID)?.innerHTML).toBe('1 1');
|
||||
Horizon.act(() => {
|
||||
triggerClickEvent(container, BUTTON_ID);
|
||||
|
||||
});
|
||||
expect(document.getElementById(RESULT_ID)?.innerHTML).toBe('2 1');
|
||||
|
||||
Horizon.act(() => {
|
||||
triggerClickEvent(container, BUTTON_ID2);
|
||||
|
||||
});
|
||||
expect(document.getElementById(RESULT_ID)?.innerHTML).toBe('2 2');
|
||||
|
||||
});
|
||||
|
||||
it('Should use use stores in cycles and multiple methods', () => {
|
||||
interface App {
|
||||
store:any,
|
||||
store2:any
|
||||
store: any;
|
||||
store2: any;
|
||||
}
|
||||
class App extends Horizon.Component {
|
||||
constructor() {
|
||||
super();
|
||||
this.store = useStore1();
|
||||
this.store2 = useStore2()
|
||||
this.store2 = useStore2();
|
||||
}
|
||||
|
||||
render() {
|
||||
|
@ -129,9 +128,11 @@ describe('Using multiple stores', () => {
|
|||
>
|
||||
add
|
||||
</button>
|
||||
<p id={RESULT_ID}>{counter} {counter2}</p>
|
||||
<p id={RESULT_ID}>
|
||||
{counter} {counter2}
|
||||
</p>
|
||||
</div>
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -140,16 +141,13 @@ describe('Using multiple stores', () => {
|
|||
expect(document.getElementById(RESULT_ID)?.innerHTML).toBe('1 1');
|
||||
Horizon.act(() => {
|
||||
triggerClickEvent(container, BUTTON_ID);
|
||||
|
||||
});
|
||||
expect(document.getElementById(RESULT_ID)?.innerHTML).toBe('2 1');
|
||||
|
||||
Horizon.act(() => {
|
||||
triggerClickEvent(container, BUTTON_ID2);
|
||||
|
||||
});
|
||||
expect(document.getElementById(RESULT_ID)?.innerHTML).toBe('2 2');
|
||||
|
||||
});
|
||||
|
||||
it('Should use multiple stores in function component', () => {
|
||||
|
@ -176,7 +174,9 @@ describe('Using multiple stores', () => {
|
|||
>
|
||||
add
|
||||
</button>
|
||||
<p id={RESULT_ID}>{counter} {counter2}</p>
|
||||
<p id={RESULT_ID}>
|
||||
{counter} {counter2}
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
@ -186,13 +186,11 @@ describe('Using multiple stores', () => {
|
|||
expect(document.getElementById(RESULT_ID)?.innerHTML).toBe('1 1');
|
||||
Horizon.act(() => {
|
||||
triggerClickEvent(container, BUTTON_ID);
|
||||
|
||||
});
|
||||
expect(document.getElementById(RESULT_ID)?.innerHTML).toBe('2 1');
|
||||
|
||||
Horizon.act(() => {
|
||||
triggerClickEvent(container, BUTTON_ID2);
|
||||
|
||||
});
|
||||
expect(document.getElementById(RESULT_ID)?.innerHTML).toBe('2 2');
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue