mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-04-01 21:13:11 +00:00
feat: Create a dynamic action
This commit is contained in:
@@ -1,24 +1,34 @@
|
||||
import { Effects, ExpectedExports } from "../types"
|
||||
import { createUtils } from "../util"
|
||||
import { once } from "../util/once"
|
||||
import { Utils } from "../util/utils"
|
||||
import { CreatedAction } from "./createAction"
|
||||
|
||||
export function setupActions<Store>(
|
||||
...createdActions: CreatedAction<Store, any>[]
|
||||
) {
|
||||
const myActions = once(() => {
|
||||
const myActions = async (options: {
|
||||
effects: Effects
|
||||
utils: Utils<Store>
|
||||
}) => {
|
||||
const actions: Record<string, CreatedAction<Store, any>> = {}
|
||||
for (const action of createdActions) {
|
||||
actions[action.myMetaData.id] = action
|
||||
const actionMetadata = await action.metaData(options)
|
||||
actions[actionMetadata.id] = action
|
||||
}
|
||||
return actions
|
||||
})
|
||||
}
|
||||
const answer: {
|
||||
actions: ExpectedExports.actions
|
||||
actionsMetadata: ExpectedExports.actionsMetadata
|
||||
} = {
|
||||
get actions() {
|
||||
return myActions()
|
||||
actions(options: { effects: Effects }) {
|
||||
const utils = createUtils<Store>(options.effects)
|
||||
|
||||
return myActions({
|
||||
...options,
|
||||
utils,
|
||||
})
|
||||
},
|
||||
async actionsMetadata({ effects }: { effects: Effects }) {
|
||||
const utils = createUtils<Store>(effects)
|
||||
|
||||
Reference in New Issue
Block a user