Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions library/src/engine/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { camel, snake } from '../utils/text'
import { DATASTAR, DSP, DSS } from './consts'
import { initErr, runtimeErr } from './errors'
import type {
ActionContext,
ActionPlugins,
AttributePlugin,
Computed,
Expand Down Expand Up @@ -1000,19 +1001,23 @@ function applyAttributePlugin(

const cleanup = plugin.onLoad(ctx)
if (cleanup) {
let cleanups = removals.get(el)
if (cleanups) {
cleanups.get(rawKey)?.()
} else {
cleanups = new Map()
removals.set(el, cleanups)
}
cleanups.set(rawKey, cleanup)
setCleanup(cleanup, el, rawKey);
}
}
}
}

function setCleanup(cleanup: OnRemovalFn, el: HTMLOrSVG, key: string): void {
let cleanups = removals.get(el)
if (cleanups) {
cleanups.get(key)?.()
} else {
cleanups = new Map()
removals.set(el, cleanups)
}
cleanups.set(key, cleanup)
}

// Set up a mutation observer to run plugin removal and apply functions
function observe(mutations: MutationRecord[]) {
const ignore = `[${aliasify('ignore')}]`
Expand Down Expand Up @@ -1160,6 +1165,7 @@ function generateReactiveExpression(
const actionMatches = [...expr.matchAll(actionsRe)]
const actionNames = new Set<string>()
const actionFns = new Set<(...args: any[]) => any>()
const actionCtx: ActionContext = { ...ctx, setCleanup: (cleanup: OnRemovalFn, key: string) => setCleanup(cleanup, ctx.el, key) }
if (actionMatches.length) {
const actionPrefix = `${DATASTAR}Act_`
for (const match of actionMatches) {
Expand All @@ -1175,7 +1181,7 @@ function generateReactiveExpression(
// Add ctx to action calls
expr = expr.replace(`@${actionName}(`, `${name}(`)
actionNames.add(name)
actionFns.add((...args: any[]) => action.fn(ctx, ...args))
actionFns.add((...args: any[]) => action.fn(actionCtx, ...args))
}
}

Expand Down
6 changes: 5 additions & 1 deletion library/src/engine/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export type WatcherPlugin = {
}

export type ActionPlugins = Record<string, ActionPlugin>
export type ActionMethod = (ctx: RuntimeContext, ...args: any[]) => any
export type ActionMethod = (ctx: ActionContext, ...args: any[]) => any

export type ActionPlugin = {
type: 'action'
Expand Down Expand Up @@ -101,6 +101,10 @@ export type RuntimeContext = InitContext & {
runtimeErr: (reason: string, metadata?: object) => Error
}

export type ActionContext = RuntimeContext & {
setCleanup: (fn: OnRemovalFn, key: string) => void // sets a cleanup function for this element and key
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since actions can return values to callers, I needed to add a new context API that allow setting a cleanup based on a key. The key can't be auto-set to the action name because there could be different fetch actions used on the same element and we only want to keep the removalFn for the last fetch action.

}

export type RuntimeExpressionFunction = (
ctx: RuntimeContext,
...args: any[]
Expand Down
1 change: 1 addition & 0 deletions library/src/plugins/backend/actions/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const createHttpMethod = (

if (!isDisabled && !(requestCancellation instanceof AbortController)) {
fetchAbortControllers.set(el, controller)
ctx.setCleanup(() => controller.abort(), `fetch`)
}

try {
Expand Down