refactor(plugin-model): 去掉provide

This commit is contained in:
wanchun 2022-04-11 13:16:26 +08:00
parent 3ddc355738
commit b73c1f9027
6 changed files with 34 additions and 62 deletions

View File

@ -1,9 +1,6 @@
import { readFileSync } from 'fs';
import { join } from 'path';
import { lodash, winPath } from '@fesjs/utils';
import { getModels } from './utils/getModels';
import { getTmpFile } from './utils/getTmpFile';
const namespace = 'plugin-model';
@ -13,6 +10,10 @@ export default (api) => {
utils: { Mustache }
} = api;
const { lodash, winPath } = require('@fesjs/utils');
const { getModels } = require('./utils/getModels');
const { getTmpFile } = require('./utils/getTmpFile');
function getModelDir() {
return api.config.singular ? 'model' : 'models';
}
@ -25,22 +26,16 @@ export default (api) => {
const srcModelsPath = getModelsPath();
return lodash.uniq([
...getModels(srcModelsPath)
// ...getModels(
// paths.absPagesPath,
// `**/${getModelDir()}/**/*.{js,jsx}`
// ),
// ...getModels(paths.absPagesPath, '**/*.model.{js,jsx}')
]);
}
const absCoreFilePath = join(namespace, 'core.js');
const absRuntimeFilePath = join(namespace, 'runtime.js');
const absInitlaStateFilePath = join(namespace, 'models/initialState.js');
const absInitialStateFilePath = join(namespace, 'models/initialState.js');
api.register({
key: 'addExtraModels',
fn: () => [{
absPath: winPath(join(paths.absTmpPath, absInitlaStateFilePath)),
absPath: winPath(join(paths.absTmpPath, absInitialStateFilePath)),
namespace: '@@initialState'
}]
});
@ -63,16 +58,10 @@ export default (api) => {
})
});
api.writeTmpFile({
path: absRuntimeFilePath,
content: Mustache.render(readFileSync(join(__dirname, 'runtime/runtime.tpl'), 'utf-8'), {
})
});
api.writeTmpFile({
path: absInitlaStateFilePath,
content: Mustache.render(readFileSync(join(__dirname, 'runtime/models/initialState.tpl'), 'utf-8'), {
})
api.copyTmpFiles({
namespace,
path: join(__dirname, 'runtime'),
ignore: ['.tpl']
});
});
@ -82,6 +71,4 @@ export default (api) => {
source: absCoreFilePath
}
]);
api.addRuntimePlugin(() => `@@/${absRuntimeFilePath}`);
};

View File

@ -1,39 +1,29 @@
import { inject } from "vue";
const modelKey = Symbol("plugin-model");
{{{userImports}}}
{{{extraImports}}}
export const models = {
export const models = {
{{#extraModels}}
{{{extraModels}}},
{{/extraModels}}
{{#userModels}}
{{{userModels}}},
{{/userModels}}
}
const cache = new Map();
export const install = (app)=>{
const useModel = (name) => {
const model = models[name];
if(model === undefined){
throw new Error("[plugin-model]: useModel, name is undefined.");
}
if (typeof model !== "function") {
throw new Error("[plugin-model]: useModel is not a function.");
}
if(!cache.has(name)){
cache.set(name, model())
}
return cache.get(name)
};
app.provide(modelKey, useModel);
}
export const useModel = (name) => {
return inject(modelKey)(name);
};
const cache = new Map();
export const useModel = (name) => {
const modelFunc = models[name];
if (modelFunc === undefined) {
throw new Error('[plugin-model]: useModel, name is undefined.');
}
if (typeof modelFunc !== 'function') {
throw new Error('[plugin-model]: useModel is not a function.');
}
if (!cache.has(name)) {
cache.set(name, modelFunc());
}
return cache.get(name);
};

View File

@ -0,0 +1,5 @@
import { inject } from 'vue';
export default function initialStateModel() {
return inject('initialState');
}

View File

@ -1,5 +0,0 @@
import { inject, reactive } from "vue";
export default function initalModel() {
return reactive(inject("initialState"));
}

View File

@ -1,5 +0,0 @@
import { install } from "./core";
export function onAppCreated({ app }) {
install(app)
}

View File

@ -3,13 +3,13 @@ import { getValidFiles } from '.';
export function getModels(cwd, pattern) {
const files = glob
.sync(pattern || '**/*.{js,jsx}', {
.sync(pattern || '**/*.{js,jsx,ts,tsx}', {
cwd
})
.filter(
file => !file.endsWith('.d.ts')
&& !file.endsWith('.test.js')
&& !file.endsWith('.test.jsx')
&& !file.endsWith('.test.js')
&& !file.endsWith('.test.jsx')
);
return getValidFiles(files, cwd);