π₯ Make videos programmatically with React
hide effect
JonnyBurger6c0bc1731d15
diff --git a/packages/core/src/effects/create-effect.ts b/packages/core/src/effects/create-effect.tsindex ffe1f3c35d..777a3f288f 100644--- a/packages/core/src/effects/create-effect.ts+++ b/packages/core/src/effects/create-effect.ts@@ -1,14 +1,20 @@import type {EffectDefinition, EffectDescriptor} from './effect-types.js';+// `disabled` is injected by the framework into every effect factory's+// parameter type. When truthy, `runEffectChain` bypasses the effect entirely+// (see `run-effect-chain.ts`). Effect authors do not declare it themselves β+// `createEffect` adds it here so every current and future effect inherits it.+export type WithDisabled<P> = P & {readonly disabled?: boolean};+type EffectFactory<P> = {} extends P- ? (params?: P) => EffectDescriptor<unknown>- : (params: P) => EffectDescriptor<unknown>;+ ? (params?: WithDisabled<P>) => EffectDescriptor<unknown>+ : (params: WithDisabled<P>) => EffectDescriptor<unknown>;// Defines an effect and returns a factory that produces per-frame descriptors.// The returned function is the public API effect authors expose to users://// export const blur = createEffect<BlurParams, BlurState>({ ... });-// // usage: blur({ radius: 4 })+// // usage: blur({ radius: 4, disabled: false })//// The descriptor erases `P` and `S` to `unknown` so descriptors of different// effects can be freely composed inside `EffectsProp` arrays. Without that@@ -17,8 +23,20 @@ type EffectFactory<P> = {} extends Pexport const createEffect = <P, S>(definition: EffectDefinition<P, S>,): EffectFactory<P> => {- const widened = definition as unknown as EffectDefinition<unknown, unknown>;- const factory = (params: P = {} as P): EffectDescriptor<unknown> => ({+ // Wrap `calculateKey` to fold the framework-level `disabled` flag into the+ // memoization key. Without this, toggling `disabled` via code/drag overrides+ // would not invalidate the cached `EffectDefinitionAndStack`.+ const userCalculateKey = definition.calculateKey;+ const widened: EffectDefinition<unknown, unknown> = {+ ...(definition as unknown as EffectDefinition<unknown, unknown>),+ calculateKey: (params: unknown) => {+ const disabled = (params as {disabled?: boolean})?.disabled ?? false;+ return `${userCalculateKey(params as P)}-disabled-${disabled}`;+ },+ };+ const factory = (+ params: WithDisabled<P> = {} as WithDisabled<P>,+ ): EffectDescriptor<unknown> => ({definition: widened,params,effectKey: widened.calculateKey(params),diff --git a/packages/core/src/effects/run-effect-chain.ts b/packages/core/src/effects/run-effect-chain.tsindex a7e8572bfa..6fb5d3bdb3 100644--- a/packages/core/src/effects/run-effect-chain.ts+++ b/packages/core/src/effects/run-effect-chain.ts@@ -74,7 +74,14 @@ export const runEffectChain = async ({const runId = ++state.currentRunId;const isCancelled = () => state.currentRunId !== runId;- const runs = groupByBackend(effects);+ // Bypass any effect with `disabled: true` before grouping by backend, so+ // disabled effects don't create empty runs or force unnecessary backend+ // transitions. The `disabled` flag is injected by `createEffect` and lives+ // on `params` so it flows through code/drag override merging.+ const enabledEffects = effects.filter(+ (e) => !(e.params as {disabled?: boolean})?.disabled,+ );+ const runs = groupByBackend(enabledEffects);let currentImage: CanvasImageSource = source;let lastTarget: HTMLCanvasElement | null = null;diff --git a/packages/core/src/test/create-effect-disabled.test.ts b/packages/core/src/test/create-effect-disabled.test.tsnew file mode 100644index 0000000000..0b0426a8de--- /dev/null+++ b/packages/core/src/test/create-effect-disabled.test.ts@@ -0,0 +1,42 @@+import {expect, test} from 'bun:test';+import {createEffect} from '../effects/create-effect.js';++type FooParams = {+ readonly amount: number;+};++const makeFoo = () =>+ createEffect<FooParams, null>({+ type: 'test/foo',+ label: 'Foo',+ backend: '2d',+ calculateKey: (p) => `foo-${p.amount}`,+ setup: () => null,+ apply: () => undefined,+ cleanup: () => undefined,+ schema: null,+ });++test('createEffect factory accepts `disabled` without complaint', () => {+ const foo = makeFoo();+ const desc = foo({amount: 1, disabled: true});+ expect((desc.params as {disabled?: boolean}).disabled).toBe(true);+});++test('effectKey changes when `disabled` toggles so memoization re-runs', () => {+ const foo = makeFoo();+ const enabled = foo({amount: 1});+ const disabled = foo({amount: 1, disabled: true});+ const explicitlyEnabled = foo({amount: 1, disabled: false});++ expect(enabled.effectKey).not.toBe(disabled.effectKey);+ expect(enabled.effectKey).toBe(explicitlyEnabled.effectKey);+});++test('definition.calculateKey reflects `disabled` for override merge path', () => {+ const foo = makeFoo();+ const desc = foo({amount: 1});+ const enabledKey = desc.definition.calculateKey({amount: 1});+ const disabledKey = desc.definition.calculateKey({amount: 1, disabled: true});+ expect(enabledKey).not.toBe(disabledKey);+});diff --git a/packages/core/src/test/effect-internals.test.ts b/packages/core/src/test/effect-internals.test.tsindex c8c7236db4..e2909a8db5 100644--- a/packages/core/src/test/effect-internals.test.ts+++ b/packages/core/src/test/effect-internals.test.ts@@ -88,3 +88,22 @@ test('groupByBackend returns one run per backend transition', () => {expect(runs).toHaveLength(4);expect(runs.map((r) => r.backend)).toEqual(['2d', 'webgl2', '2d', 'webgl2']);});++test('runEffectChain filters disabled effects before grouping', () => {+ // Mirror of the filter in `runEffectChain` β kept here as a regression+ // guard so the behavior is asserted independently of the canvas-bound+ // chain runner.+ const all: EffectDescriptor<unknown>[] = [+ {...makeDesc('a', '2d'), params: {disabled: false}},+ {...makeDesc('b', '2d'), params: {disabled: true}},+ {...makeDesc('c', 'webgl2'), params: {}},+ {...makeDesc('d', 'webgl2'), params: {disabled: true}},+ ];+ const enabled = all.filter(+ (e) => !(e.params as {disabled?: boolean})?.disabled,+ );+ const runs = groupByBackend(memoizeEffects(enabled));+ expect(runs).toHaveLength(2);+ expect(runs[0].effects.map((e) => e.definition.type)).toEqual(['a']);+ expect(runs[1].effects.map((e) => e.definition.type)).toEqual(['c']);+});