mirror of
https://github.com/smartxworks/sunmao-ui.git
synced 2025-01-30 17:09:35 +08:00
Merge pull request #360 from webzard-io/fix/windlike-patch
Fix/windlike patch
This commit is contained in:
commit
7817abe300
@ -101,6 +101,7 @@
|
||||
"@sunmao-ui/core": "^0.4.1",
|
||||
"@sunmao-ui/editor-sdk": "^0.0.0",
|
||||
"@sunmao-ui/runtime": "^0.4.1",
|
||||
"@sinclair/typebox": "^0.21.2",
|
||||
"formik": "^2.2.9",
|
||||
"immer": "^9.0.6",
|
||||
"lodash-es": "^4.17.21",
|
||||
|
@ -5,8 +5,14 @@ import { initApiService } from './services/apiService';
|
||||
import { initGlobalHandlerMap } from './services/handler';
|
||||
import { UtilMethodManager } from './services/UtilMethodManager';
|
||||
import { AppHooks, UtilMethod } from './types';
|
||||
import { enableES5, setAutoFreeze } from 'immer';
|
||||
import './style.css';
|
||||
|
||||
// immer would make some errors when read the states, so we do these to avoid it temporarily
|
||||
// ref: https://github.com/immerjs/immer/issues/916
|
||||
enableES5();
|
||||
setAutoFreeze(false);
|
||||
|
||||
export type SunmaoUIRuntimeProps = {
|
||||
dependencies?: Record<string, any>;
|
||||
utilMethods?: UtilMethod<any>[];
|
||||
|
@ -10,11 +10,12 @@ const PropsSchema = Type.Object({
|
||||
|
||||
export const generateCallback = (
|
||||
handler: Omit<Static<typeof EventHandlerSchema>, 'type'>,
|
||||
rawHandler: Static<typeof EventHandlerSchema>,
|
||||
services: UIServices
|
||||
) => {
|
||||
const send = () => {
|
||||
// Eval before sending event to assure the handler object is evaled from the latest state.
|
||||
const evaledHandler = services.stateManager.deepEval(handler);
|
||||
const evaledHandler = services.stateManager.deepEval(rawHandler);
|
||||
|
||||
if (evaledHandler.disabled && typeof evaledHandler.disabled === 'boolean') {
|
||||
return;
|
||||
@ -42,15 +43,18 @@ export const generateCallback = (
|
||||
};
|
||||
|
||||
const EventTraitFactory: TraitImplFactory<Static<typeof PropsSchema>> = () => {
|
||||
return ({ handlers, services }) => {
|
||||
return ({ trait, handlers, services }) => {
|
||||
const callbackQueueMap: Record<string, Array<() => void>> = {};
|
||||
const rawHandlers = trait.properties.handlers as Static<typeof EventHandlerSchema>[];
|
||||
// setup current handlers
|
||||
for (const i in handlers) {
|
||||
const handler = handlers[i];
|
||||
if (!callbackQueueMap[handler.type]) {
|
||||
callbackQueueMap[handler.type] = [];
|
||||
}
|
||||
callbackQueueMap[handler.type].push(generateCallback(handler, services));
|
||||
callbackQueueMap[handler.type].push(
|
||||
generateCallback(handler, rawHandlers[i], services)
|
||||
);
|
||||
}
|
||||
|
||||
const callbackMap: CallbackMap<string> = {};
|
||||
|
@ -1,7 +1,10 @@
|
||||
import { createTrait } from '@sunmao-ui/core';
|
||||
import { Static, Type } from '@sinclair/typebox';
|
||||
import { TraitImplFactory } from '../../types';
|
||||
import { FetchTraitPropertiesSchema } from '../../types/traitPropertiesSchema';
|
||||
import {
|
||||
FetchTraitPropertiesSchema,
|
||||
EventHandlerSchema,
|
||||
} from '../../types/traitPropertiesSchema';
|
||||
import { generateCallback } from './Event';
|
||||
|
||||
const FetchTraitFactory: TraitImplFactory<Static<typeof FetchTraitPropertiesSchema>> =
|
||||
@ -15,6 +18,8 @@ const FetchTraitFactory: TraitImplFactory<Static<typeof FetchTraitPropertiesSche
|
||||
headers: _headers,
|
||||
body,
|
||||
bodyType,
|
||||
onComplete,
|
||||
onError,
|
||||
mergeState,
|
||||
services,
|
||||
subscribeMethods,
|
||||
@ -37,10 +42,10 @@ const FetchTraitFactory: TraitImplFactory<Static<typeof FetchTraitPropertiesSche
|
||||
|
||||
mergeState({
|
||||
fetch: {
|
||||
...(services.stateManager.store[componentId].fetch || {}),
|
||||
code: undefined,
|
||||
codeText: '',
|
||||
loading: true,
|
||||
data: undefined,
|
||||
error: undefined,
|
||||
},
|
||||
});
|
||||
@ -90,8 +95,12 @@ const FetchTraitFactory: TraitImplFactory<Static<typeof FetchTraitPropertiesSche
|
||||
const rawOnComplete = trait.properties.onComplete as Static<
|
||||
typeof FetchTraitPropertiesSchema
|
||||
>['onComplete'];
|
||||
rawOnComplete?.forEach(handler => {
|
||||
generateCallback(handler, services)();
|
||||
rawOnComplete?.forEach((rawHandler, index) => {
|
||||
generateCallback(
|
||||
onComplete![index],
|
||||
rawHandler as Static<typeof EventHandlerSchema>,
|
||||
services
|
||||
)();
|
||||
});
|
||||
} else {
|
||||
// TODO: Add FetchError class and remove console info
|
||||
@ -109,8 +118,12 @@ const FetchTraitFactory: TraitImplFactory<Static<typeof FetchTraitPropertiesSche
|
||||
const rawOnError = trait.properties.onError as Static<
|
||||
typeof FetchTraitPropertiesSchema
|
||||
>['onError'];
|
||||
rawOnError?.forEach(handler => {
|
||||
generateCallback(handler, services)();
|
||||
rawOnError?.forEach((rawHandler, index) => {
|
||||
generateCallback(
|
||||
onError![index],
|
||||
rawHandler as Static<typeof EventHandlerSchema>,
|
||||
services
|
||||
)();
|
||||
});
|
||||
}
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user