Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/model/src/clients/client-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { CommonResponse } from './interceptor';
import type { ClientOptions, Params, FetchPolicy, RequestConfig, RestParams } from './types';
import type { HydrationStatus } from '../store';
import { ReplaySubject } from 'rxjs';
import { MODE } from '../const';
import { RENDER_MODE } from '../const';

import { createCache } from '../cache';
import { hash } from '../cache/hash';
Expand Down Expand Up @@ -117,14 +117,14 @@ export function clientFactory<ClientType extends 'GQL'| 'REST'>(

if (!opts.fetch) {
// 避免Node环境下的判断,所以没法简化写=。=,因为window.fetch会触发一次RHS导致报错
if (MODE === 'SPA') {
if (RENDER_MODE === 'SPA') {
// 小程序因为没有window,所以需要这里绕一下
if (typeof window !== 'undefined' && window.fetch) {
opts.fetch = (resource, options) => window.fetch(resource, options);
} else {
throw new Error('There is no useful "fetch" function');
}
} else if (MODE === 'SSR') {
} else {
if (globalThis.fetch) {
opts.fetch = (resource, options) => globalThis.fetch(resource, options);
} else {
Expand All @@ -142,7 +142,7 @@ export function clientFactory<ClientType extends 'GQL'| 'REST'>(
const timeoutPromise = new Promise<DOMException | TimeoutError>((resolve) => {
setTimeout(
() => {
if (MODE !== 'SSR' && typeof DOMException !== 'undefined') {
if (RENDER_MODE === 'SPA' && typeof DOMException !== 'undefined') {
resolve(new DOMException('The request has been timeout'))
} else {
resolve(new TimeoutError('The request has been timeout'))
Expand All @@ -155,7 +155,7 @@ export function clientFactory<ClientType extends 'GQL'| 'REST'>(
}).then((res) => {
// 浏览器断网情况下有可能会是null
if (res === null) {
res = MODE !== 'SSR' && typeof DOMException !== 'undefined'
res = RENDER_MODE === 'SPA' && typeof DOMException !== 'undefined'
? new DOMException('The request has been timeout')
: new TimeoutError('The request has been timeout');
}
Expand Down
8 changes: 4 additions & 4 deletions packages/model/src/const.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { RebornClient } from './clients';
import type { storeFactory } from './store';
import type { RenderMode, storeFactory } from './store';

import { getCurrentInstance, inject } from 'vue-demi';

Expand All @@ -15,10 +15,10 @@ export const ROOT_STORE_MAP = new WeakMap<
RootStore
>();

export let MODE: 'SPA' | 'SSR' = 'SPA';
export let RENDER_MODE: RenderMode = 'SPA';

export function setMode(mode: 'SPA' | 'SSR') {
MODE = mode;
export function setRenderMode(mode: RenderMode) {
RENDER_MODE = mode;
}

export function getRootStore(): RootStore {
Expand Down
4 changes: 2 additions & 2 deletions packages/model/src/model/class-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
createRestQuery,
} from '../operations';

import { getRootStore, MODE } from '../const';
import { getRootStore, RENDER_MODE } from '../const';
import { computed, reactive, getCurrentInstance, type ComputedRef } from 'vue-demi';
import { type StateStatus, useStatus } from '../operations/status';

Expand Down Expand Up @@ -360,7 +360,7 @@ export function createModelFromClass<T>(ctor: Constructor<T>): ModelCotrInfo<T>
});

// 延迟初始化,保证query间依赖
if (queryList.length && MODE !== 'SSR') {
if (queryList.length && RENDER_MODE === 'SPA') {
queryList.forEach(query => query.init());
}

Expand Down
4 changes: 2 additions & 2 deletions packages/model/src/model/fn-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
createRestQuery,
} from '../operations';
import { getCurrentInstance, shallowReactive, toRefs, watch } from 'vue-demi';
import { getRootStore, MODE } from '../const';
import { getRootStore, RENDER_MODE } from '../const';
import { StateStatus, useStatus } from '../operations/status';


Expand Down Expand Up @@ -142,7 +142,7 @@ export function createModelFromCA<T>(
const { model, queryList } = fn.creator();

// 延迟初始化,保证query间依赖
if (queryList.length && MODE !== 'SSR') {
if (queryList.length && RENDER_MODE === 'SPA') {
queryList.forEach(query => query.init?.());
}

Expand Down
7 changes: 5 additions & 2 deletions packages/model/src/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { createModelFromCA } from './fn-type'
import { createModelFromClass } from './class-type';

import { getRootStore } from '../const';
import { getRootStore, RENDER_MODE } from '../const';

export type * from './types';
import { createModel, type FNModelConstructor } from './fn-type';
Expand Down Expand Up @@ -84,7 +84,10 @@ export function useModel<T extends MyCon<any> = MyCon<any>>(ctor: T): RebornInst
}
});
onServerPrefetch(async () => {
await storeModelInstance.instance?.prefetch();
// SSG 不进行请求
if(RENDER_MODE !== 'SSG') {
await storeModelInstance.instance?.prefetch();
}
storeModelInstance.count--;
if (storeModelInstance.count === 0 && storeModelInstance.instance) {
storeModelInstance.instance.destroy();
Expand Down
4 changes: 2 additions & 2 deletions packages/model/src/operations/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { RequestReason, type InfoDataType } from './status';
import { reactive, computed, watch, type ComputedRef } from 'vue-demi';
import { fromWatch } from '../utils';
import { Observable, merge, map } from 'rxjs';
import { MODE } from '../const';
import { RENDER_MODE } from '../const';

export { isDef } from '../utils';

Expand Down Expand Up @@ -70,7 +70,7 @@ export function generateQueryOptions<ModelType, DataType, VariablesType>(
let timeout: ReturnType<typeof setTimeout>;

const poll = () => {
if(MODE === 'SSR') {
if(RENDER_MODE !== 'SPA') {
return
}
clearTimeout(timeout);
Expand Down
20 changes: 12 additions & 8 deletions packages/model/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import type { Ref } from 'vue-demi';
import type { Client, RebornClient } from '../clients';

import { ref } from 'vue-demi';
import { INJECT_KEY, ROOT_STORE_MAP, setMode } from '../const';
import { INJECT_KEY, ROOT_STORE_MAP, setRenderMode } from '../const';

export type RenderMode = 'SPA' | 'SSR' | 'SSG';

export type GetModelInstance = ReturnType<typeof storeFactory>['getModelInstance'];

Expand All @@ -16,7 +18,9 @@ export type HydrationStatus = Ref<0 | 1 | 2>;
export function storeFactory() {
const modelMap = new Map<ModelInfo<any>['constructor'], ModelInfo<any>>();

function getModelInstance<T>(constructor: ModelInfo<T>['constructor']): RebornInstanceType<typeof constructor> | null {
function getModelInstance<T>(
constructor: ModelInfo<T>['constructor'],
): RebornInstanceType<typeof constructor> | null {
return modelMap.get(constructor)?.instance?.model as unknown as RebornInstanceType<typeof constructor>;
}

Expand Down Expand Up @@ -75,7 +79,7 @@ export function createStore() {
}

// 为了适配 vue 不同版本这里只能用 any 了
function install(app: any, ssrMode: boolean = false) {
function install(app: any, renderMode: boolean | RenderMode = false) {
// vue3
if (app.config && typeof app.config.globalProperties === 'object') {
app.config.globalProperties.rebornStore = store;
Expand All @@ -85,7 +89,7 @@ export function createStore() {
store,
rebornClient,
});
// vue2.7
// vue2.7
} else {
app.mixin({
provide(this) {
Expand All @@ -94,7 +98,7 @@ export function createStore() {
[INJECT_KEY]: {
store,
rebornClient,
}
},
};
}
},
Expand All @@ -104,13 +108,13 @@ export function createStore() {
ROOT_STORE_MAP.set(this, {
store,
rebornClient,
})
});
}
}
},
});
}

setMode(ssrMode ? 'SSR' : 'SPA');
setRenderMode(typeof renderMode === 'string' ? renderMode : renderMode ? 'SSR' : 'SPA');
}

const result = {
Expand Down