Skip to content

Commit

Permalink
refactor: remove instance
Browse files Browse the repository at this point in the history
  • Loading branch information
taixw2 committed Oct 14, 2020
1 parent dab4052 commit 5e9e703
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 60 deletions.
1 change: 0 additions & 1 deletion packages/core/src/dx/create-store/store-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const invariant = require('invariant');

/**
* 将 options 中传入的 models 处理
* @param inst
* @param options
*/
export function storeModel(options: CreateOption): void {
Expand Down
8 changes: 3 additions & 5 deletions packages/core/src/dx/exports/collect.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { MODEL_NAME } from '@dxjs/shared/symbol';
import { store } from '../../helper/store';
import { DxModel } from '../../dx-model/model';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
// eslint-disable-next-line @typescript-eslint/no-var-requires
import { DxModel, DxModelContstructor } from '../../dx-model/model';

// TODO: support HMR
export function collectFactory() {
return (name?: string) => {
return function Decorate(ModelTarget: { new (): DxModel }): { new (): DxModel } {
return function Decorate(ModelTarget: DxModelContstructor): DxModelContstructor {
if (__DEV__) {
require('invariant')(
ModelTarget.prototype instanceof DxModel,
Expand Down
16 changes: 3 additions & 13 deletions packages/core/src/dx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,14 @@ import { createFactory } from './exports/create';
import { modelsFactory } from './exports/models';
import { collectFactory } from './exports/collect';
import { createStoreFactory } from './create-store';
import { changeDxInstance, store } from '../helper/store';

export function DxFactory() {
const inst = Symbol('__dx');

changeDxInstance(inst);

// init
if (store.plugins.size === 0) {
store.plugins = new Map();
}

return {
createStore: createStoreFactory(),
create: createFactory(),
getModels: modelsFactory(),
collect: collectFactory(),
inst: process.env.NODE_ENV === 'test' ? inst : undefined,
// 后面可能会有所改动
UNSAFE_getModels: modelsFactory(),
UNSAFE_collect: collectFactory(),
};
}

Expand Down
46 changes: 6 additions & 40 deletions packages/core/src/helper/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,16 @@ interface ModelRefs {
map: { [key: string]: DxModelContstructor };
}

// TODO: 多实例待测试
let dxInstance: SymbolType = '';

export function changeDxInstance(instance: SymbolType): void {
dxInstance = instance;
}

const plugins = new Map<SymbolType, Map<Hook, unknown[]>>();
const models = new Map<SymbolType, ModelRefs>();
const reduxStore = new Map<SymbolType, Store>();
const effectTypes = new Map<SymbolType, Set<SymbolType>>();

export const store = {
set plugins(value: Map<Hook, unknown[]>) {
plugins.set(dxInstance, value);
},

get plugins(): Map<Hook, unknown[]> {
return plugins.get(dxInstance) ?? new Map();
},

set models(value: ModelRefs) {
models.set(dxInstance, value);
},

get models(): ModelRefs {
return models.get(dxInstance)!;
},

set reduxStore(value: Store) {
reduxStore.set(dxInstance, value);
},
// plugin
plugins: new Map<Hook, unknown[]>(),

get reduxStore(): Store {
return reduxStore.get(dxInstance)!;
},
// models
models: {} as ModelRefs,

set effectTypes(value: Set<SymbolType>) {
effectTypes.set(dxInstance, value);
},
reduxStore: null as Store | null,

get effectTypes(): Set<SymbolType> {
return effectTypes.get(dxInstance)!;
},
effectTypes: new Set<SymbolType>(),

getModels(): ModelRefs {
if (!store.models) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import 'reflect-metadata';
import 'es6-symbol';

export { DxModel } from './dx-model/model';
export { DxFactory, Dx } from './dx';
export { Dx } from './dx';
export { connect, useDispatch, useSelector, useStore, shallowEqual, batch, Provider } from 'react-redux';

0 comments on commit 5e9e703

Please sign in to comment.