From b902b513d4f904fc2bd6271e0d662f9809206879 Mon Sep 17 00:00:00 2001 From: Anderson Leal Date: Fri, 31 Oct 2025 17:29:40 -0300 Subject: [PATCH 1/2] refactor: replace split with slice and indexOf for key parsing in state adapters --- packages/core/src/state/adapters/default-state-adapter.ts | 4 +++- packages/core/src/state/adapters/memory-state-adapter.ts | 4 ++-- playground/motia.config.ts | 2 +- plugins/plugin-states/src/api.ts | 4 +++- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/core/src/state/adapters/default-state-adapter.ts b/packages/core/src/state/adapters/default-state-adapter.ts index c8378ffae..37ee9c788 100644 --- a/packages/core/src/state/adapters/default-state-adapter.ts +++ b/packages/core/src/state/adapters/default-state-adapter.ts @@ -108,7 +108,9 @@ export class FileStateAdapter implements StateAdapter { return Object.entries(data) .map(([key, value]) => { - const [groupId, itemKey] = key.split(':') + const colonIndex = key.indexOf(':') + const groupId = key.slice(0, colonIndex) + const itemKey = key.slice(colonIndex + 1) const itemValue = JSON.parse(value) return { groupId, key: itemKey, value: itemValue, type: inferType(itemValue) } }) diff --git a/packages/core/src/state/adapters/memory-state-adapter.ts b/packages/core/src/state/adapters/memory-state-adapter.ts index ba483e5e7..ac62934f9 100644 --- a/packages/core/src/state/adapters/memory-state-adapter.ts +++ b/packages/core/src/state/adapters/memory-state-adapter.ts @@ -66,8 +66,8 @@ export class MemoryStateAdapter implements StateAdapter { async items(input: StateItemsInput): Promise { return Object.entries(this.state) .map(([key, value]) => ({ - groupId: key.split(':')[0], - key: key.split(':')[1], + groupId: key.slice(0, key.indexOf(':')), + key: key.slice(key.indexOf(':') + 1), type: inferType(value), value: value as StateItem['value'], })) diff --git a/playground/motia.config.ts b/playground/motia.config.ts index 9ba110a8e..a78f29bbd 100644 --- a/playground/motia.config.ts +++ b/playground/motia.config.ts @@ -42,5 +42,5 @@ function localPluginExample(motia: MotiaPluginContext): MotiaPlugin { } export default config({ - plugins: [statesPlugin, endpointPlugin, logsPlugin, observabilityPlugin, examplePlugin, localPluginExample], + plugins: [observabilityPlugin, statesPlugin, endpointPlugin, logsPlugin, examplePlugin, localPluginExample], }) diff --git a/plugins/plugin-states/src/api.ts b/plugins/plugin-states/src/api.ts index 6d3e3f73d..ef273c675 100644 --- a/plugins/plugin-states/src/api.ts +++ b/plugins/plugin-states/src/api.ts @@ -55,7 +55,9 @@ export const api = (motia: MotiaPluginContext): void => { async (req: ApiRequest, ctx: FlowContext): Promise => { try { for (const id of (req.body as { ids: string[] }).ids) { - const [groupId, key] = id.split(':') + const colonIndex = id.indexOf(':') + const groupId = id.slice(0, colonIndex) + const key = id.slice(colonIndex + 1) await ctx.state.delete(groupId, key) } From a31d02cbcc97dcac16b1a2bc244cba15c0608b18 Mon Sep 17 00:00:00 2001 From: Anderson Leal Date: Fri, 31 Oct 2025 17:44:10 -0300 Subject: [PATCH 2/2] refactor: streamline key parsing in state adapters by using array destructuring --- .../src/state/adapters/default-state-adapter.ts | 5 ++--- .../src/state/adapters/memory-state-adapter.ts | 15 +++++++++------ plugins/plugin-states/src/api.ts | 5 ++--- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/packages/core/src/state/adapters/default-state-adapter.ts b/packages/core/src/state/adapters/default-state-adapter.ts index 37ee9c788..cf741f64a 100644 --- a/packages/core/src/state/adapters/default-state-adapter.ts +++ b/packages/core/src/state/adapters/default-state-adapter.ts @@ -108,9 +108,8 @@ export class FileStateAdapter implements StateAdapter { return Object.entries(data) .map(([key, value]) => { - const colonIndex = key.indexOf(':') - const groupId = key.slice(0, colonIndex) - const itemKey = key.slice(colonIndex + 1) + const [groupId, ...keyParts] = key.split(':') + const itemKey = keyParts.join(':') const itemValue = JSON.parse(value) return { groupId, key: itemKey, value: itemValue, type: inferType(itemValue) } }) diff --git a/packages/core/src/state/adapters/memory-state-adapter.ts b/packages/core/src/state/adapters/memory-state-adapter.ts index ac62934f9..1595db7a0 100644 --- a/packages/core/src/state/adapters/memory-state-adapter.ts +++ b/packages/core/src/state/adapters/memory-state-adapter.ts @@ -65,12 +65,15 @@ export class MemoryStateAdapter implements StateAdapter { async items(input: StateItemsInput): Promise { return Object.entries(this.state) - .map(([key, value]) => ({ - groupId: key.slice(0, key.indexOf(':')), - key: key.slice(key.indexOf(':') + 1), - type: inferType(value), - value: value as StateItem['value'], - })) + .map(([key, value]) => { + const [groupId, ...keyParts] = key.split(':') + return { + groupId, + key: keyParts.join(':'), + type: inferType(value), + value: value as StateItem['value'], + } + }) .filter((item) => (input.groupId ? item.groupId === input.groupId : true)) .filter((item) => (input.filter ? filterItem(item, input.filter) : true)) } diff --git a/plugins/plugin-states/src/api.ts b/plugins/plugin-states/src/api.ts index ef273c675..f573ceedf 100644 --- a/plugins/plugin-states/src/api.ts +++ b/plugins/plugin-states/src/api.ts @@ -55,9 +55,8 @@ export const api = (motia: MotiaPluginContext): void => { async (req: ApiRequest, ctx: FlowContext): Promise => { try { for (const id of (req.body as { ids: string[] }).ids) { - const colonIndex = id.indexOf(':') - const groupId = id.slice(0, colonIndex) - const key = id.slice(colonIndex + 1) + const [groupId, ...keyParts] = id.split(':') + const key = keyParts.join(':') await ctx.state.delete(groupId, key) }