mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-03-26 18:31:54 +00:00
42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
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 = async (options: {
|
|
effects: Effects
|
|
utils: Utils<Store>
|
|
}) => {
|
|
const actions: Record<string, CreatedAction<Store, any>> = {}
|
|
for (const action of createdActions) {
|
|
const actionMetadata = await action.metaData(options)
|
|
actions[actionMetadata.id] = action
|
|
}
|
|
return actions
|
|
}
|
|
const answer: {
|
|
actions: ExpectedExports.actions
|
|
actionsMetadata: ExpectedExports.actionsMetadata
|
|
} = {
|
|
actions(options: { effects: Effects }) {
|
|
const utils = createUtils<Store>(options.effects)
|
|
|
|
return myActions({
|
|
...options,
|
|
utils,
|
|
})
|
|
},
|
|
async actionsMetadata({ effects }: { effects: Effects }) {
|
|
const utils = createUtils<Store>(effects)
|
|
return Promise.all(
|
|
createdActions.map((x) => x.ActionMetadata({ effects, utils })),
|
|
)
|
|
},
|
|
}
|
|
return answer
|
|
}
|