Skip to content

Commit

Permalink
feat(runtime): add watcher data source
Browse files Browse the repository at this point in the history
  • Loading branch information
tanbowensg committed Sep 1, 2023
1 parent 59d5e6a commit 79aab55
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
52 changes: 52 additions & 0 deletions packages/runtime/src/components/core/Watcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Type } from '@sinclair/typebox';
import {
CORE_VERSION_V2,
CoreComponentName,
PRESET_PROPERTY_CATEGORY,
} from '@sunmao-ui/shared';
import { implementRuntimeComponent } from '../../utils/buildKit';
import { useEffect } from 'react';
import { watch } from '../..';

export default implementRuntimeComponent({
version: CORE_VERSION_V2,
metadata: {
name: CoreComponentName.Watcher,
displayName: 'Watcher',
description: 'Watch expression changing and trigger events',
exampleProperties: { exp: '' },
isDataSource: true,
annotations: {
category: 'Data',
},
},
spec: {
properties: Type.Object({
exp: Type.String({
title: 'Expression',
category: PRESET_PROPERTY_CATEGORY.Basic,
}),
}),
state: Type.Object({ value: Type.Any() }),
methods: {},
slots: {},
styleSlots: [],
events: ['onChange'],
},
})(({ component, services, mergeState, callbackMap }) => {
useEffect(() => {
const stop = watch(
() => {
const v = services.stateManager.deepEval(component.properties.exp, {});
return v;
},
newV => {
mergeState({ value: newV });
callbackMap?.onChange();
}
);

return stop;
}, [callbackMap, component.properties.exp, mergeState, services.stateManager]);
return null;
});
2 changes: 2 additions & 0 deletions packages/runtime/src/services/Registry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import CoreStack from '../components/core/Stack';
import CoreFileInput from '../components/core/FileInput';
import CoreList from '../components/core/List';
import CoreIframe from '../components/core/Iframe';
import CoreWatcher from '../components/core/Watcher';

// traits
import CoreArrayState from '../traits/core/ArrayState';
Expand Down Expand Up @@ -257,6 +258,7 @@ export function initRegistry(
registry.registerComponent(CoreFileInput);
registry.registerComponent(CoreList);
registry.registerComponent(CoreIframe);
registry.registerComponent(CoreWatcher);

registry.registerTrait(CoreState);
registry.registerTrait(CoreArrayState);
Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/constants/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export enum CoreComponentName {
ModuleContainer = 'moduleContainer',
Text = 'text',
Iframe = 'iframe',
Watcher = 'watcher',
}
// core traits
export enum CoreTraitName {
Expand Down

0 comments on commit 79aab55

Please sign in to comment.