Skip to content

Commit

Permalink
Merge pull request #2800 from opral/martinlysk1/mesdk-106
Browse files Browse the repository at this point in the history
Trigger signals and callbacks for messages not present in messages provided by loadMessages plugin
  • Loading branch information
martin-lysk authored May 21, 2024
2 parents cc565c5 + 9e0dee6 commit 9567b71
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-moose-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@inlang/sdk": patch
---

fixes reactivity for deleted messages
19 changes: 17 additions & 2 deletions inlang/source-code/sdk/src/createMessagesQuery.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import type { Message } from "@inlang/message"
import { ReactiveMap } from "./reactivity/map.js"
import { createEffect } from "./reactivity/solid.js"
import { createEffect, onCleanup } from "./reactivity/solid.js"
import { createSubscribable } from "./loadProject.js"
import type { InlangProject, MessageQueryApi, MessageQueryDelegate } from "./api.js"
import type { ResolvedPluginApi } from "./resolve-modules/plugins/types.js"
import type { resolveModules } from "./resolve-modules/resolveModules.js"
import { createNodeishFsWithWatcher } from "./createNodeishFsWithWatcher.js"
import type { NodeishFilesystem } from "@lix-js/fs"
import { onCleanup } from "solid-js"
import { stringifyMessage } from "./storage/helper.js"
import { acquireFileLock } from "./persistence/filelock/acquireFileLock.js"
import _debug from "debug"
Expand Down Expand Up @@ -323,6 +322,8 @@ async function loadMessagesViaPlugin(

let loadedMessageCount = 0

const deletedMessages = new Set(messages.keys())

for (const loadedMessage of loadedMessages) {
const loadedMessageClone = structuredClone(loadedMessage)

Expand All @@ -338,6 +339,8 @@ async function loadMessagesViaPlugin(
// - this could be the case if one edits the aliase manualy
throw new Error("more than one message with the same id or alias found ")
} else if (currentMessages.length === 1) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- length has checked beforhand
deletedMessages.delete(currentMessages[0]!.id)
// update message in place - leave message id and alias untouched
loadedMessageClone.alias = {} as any

Expand Down Expand Up @@ -403,6 +406,18 @@ async function loadMessagesViaPlugin(
loadedMessageCount = 0
}
}

loadedMessageCount = 0
for (const deletedMessageId of deletedMessages) {
messages.delete(deletedMessageId)
delegate?.onMessageDelete(deletedMessageId)
loadedMessageCount++
if (loadedMessageCount > maxMessagesPerTick) {
await sleep(0)
loadedMessageCount = 0
}
}

await releaseLock(fs as NodeishFilesystem, lockDirPath, "loadMessage", lockTime)
lockTime = undefined

Expand Down
42 changes: 40 additions & 2 deletions inlang/source-code/sdk/src/loadProject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,23 @@ describe("functionality", () => {
],
}

const newMessage = {
id: "test2",
selectors: [],
variants: [
{
match: [],
languageTag: "en",
pattern: [
{
type: "Text",
value: "test",
},
],
},
],
}

await fs.writeFile("./messages.json", JSON.stringify(messages))

const getMessages = async (customFs: NodeishFilesystemSubset) => {
Expand Down Expand Up @@ -1121,9 +1138,11 @@ describe("functionality", () => {
})

let counter = 0
let messageCount = 0

project.query.messages.getAll.subscribe(() => {
project.query.messages.getAll.subscribe((messages) => {
counter = counter + 1
messageCount = messages.length
})

// subscribe fires once
Expand All @@ -1135,21 +1154,40 @@ describe("functionality", () => {

// we didn't change the message we write into message.json - shouldn't change the messages
expect(counter).toBe(1)
expect(messageCount).toBe(1)

// saving the file without changing should trigger a change
messages.data[0]!.variants[0]!.pattern[0]!.value = "changed"
await fs.writeFile("./messages.json", JSON.stringify(messages))
await new Promise((resolve) => setTimeout(resolve, 200)) // file event will lock a file and be handled sequentially - give it time to pickup the change

expect(counter).toBe(2)
expect(messageCount).toBe(1)

messages.data[0]!.variants[0]!.pattern[0]!.value = "changed3"

// change file
// change file - update message
await fs.writeFile("./messages.json", JSON.stringify(messages))
await new Promise((resolve) => setTimeout(resolve, 200)) // file event will lock a file and be handled sequentially - give it time to pickup the change

expect(counter).toBe(3)
expect(messageCount).toBe(1)

// change file - add a message
messages.data.push(newMessage)
await fs.writeFile("./messages.json", JSON.stringify(messages))
await new Promise((resolve) => setTimeout(resolve, 200)) // file event will lock a file and be handled sequentially - give it time to pickup the change

expect(counter).toBe(4)
expect(messageCount).toBe(2)

// change file - remove a message
messages.data.pop()
await fs.writeFile("./messages.json", JSON.stringify(messages))
await new Promise((resolve) => setTimeout(resolve, 200)) // file event will lock a file and be handled sequentially - give it time to pickup the change

expect(counter).toBe(5)
expect(messageCount).toBe(1)
})
})
})

0 comments on commit 9567b71

Please sign in to comment.