-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(runtime): add watcher data source
- Loading branch information
1 parent
59d5e6a
commit 79aab55
Showing
3 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters