@deepseek-ai/cordis-plugin-include 1.0.11-alpha.1 → 1.0.11-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -0
- package/lib/index.js +648 -150
- package/lib/types/index.d.ts +54 -59
- package/lib/types/index.d.ts.map +1 -1
- package/lib/types/journal.d.ts +69 -0
- package/lib/types/journal.d.ts.map +1 -0
- package/lib/types/patch.d.ts +68 -0
- package/lib/types/patch.d.ts.map +1 -0
- package/package.json +4 -4
- package/src/index.ts +363 -241
- package/src/journal.ts +242 -0
- package/src/patch.ts +261 -0
package/src/journal.ts
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import type { EntryChange, EntryOptions } from '@deepseek-ai/cordis-plugin-loader'
|
|
2
|
+
import { deepEqual, type Dict, omit } from '@deepseek-ai/cosmokit'
|
|
3
|
+
|
|
4
|
+
/** One tree-side mutation that has not reached the file yet, keyed by entry id. */
|
|
5
|
+
export type JournalRecord = JournalRecord.Remove | JournalRecord.Upsert
|
|
6
|
+
|
|
7
|
+
export namespace JournalRecord {
|
|
8
|
+
export interface Remove {
|
|
9
|
+
kind: 'remove'
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface Upsert {
|
|
13
|
+
kind: 'upsert'
|
|
14
|
+
/** The entry did not exist when the tree first reported it. */
|
|
15
|
+
created: boolean
|
|
16
|
+
/**
|
|
17
|
+
* The group the tree placed the entry in (`null` for the root), or
|
|
18
|
+
* `undefined` when the tree did not relocate it.
|
|
19
|
+
*/
|
|
20
|
+
parent: string | null | undefined
|
|
21
|
+
/** Index within `parent`; only meaningful alongside a `parent`. */
|
|
22
|
+
position: number
|
|
23
|
+
/**
|
|
24
|
+
* Per-key delta relative to the options the entry had when the change
|
|
25
|
+
* was reported; `undefined` deletes a key. Collected at report time: the
|
|
26
|
+
* live tree is reconciled asynchronously and may lag behind the file.
|
|
27
|
+
*/
|
|
28
|
+
changes: Dict
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type Journal = Map<string, JournalRecord>
|
|
33
|
+
|
|
34
|
+
export function diffOptions(legacy: EntryOptions, options: EntryOptions) {
|
|
35
|
+
const changes: Dict = {}
|
|
36
|
+
for (const key of new Set([...Object.keys(legacy), ...Object.keys(options)])) {
|
|
37
|
+
if (key === 'id') continue
|
|
38
|
+
if (deepEqual(legacy[key], options[key])) continue
|
|
39
|
+
changes[key] = options[key]
|
|
40
|
+
}
|
|
41
|
+
return changes
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Fold a later record into an earlier one for the same entry. */
|
|
45
|
+
export function mergeRecords(older: JournalRecord | undefined, newer: JournalRecord): JournalRecord | undefined {
|
|
46
|
+
if (!older) return newer
|
|
47
|
+
if (newer.kind === 'remove') {
|
|
48
|
+
// an entry created and removed before ever being written leaves no trace
|
|
49
|
+
return older.kind === 'upsert' && older.created ? undefined : newer
|
|
50
|
+
}
|
|
51
|
+
if (older.kind === 'remove') {
|
|
52
|
+
return { ...newer, created: false }
|
|
53
|
+
}
|
|
54
|
+
const relocated = newer.parent !== undefined
|
|
55
|
+
return {
|
|
56
|
+
kind: 'upsert',
|
|
57
|
+
created: older.created,
|
|
58
|
+
parent: relocated ? newer.parent : older.parent,
|
|
59
|
+
position: relocated ? newer.position : older.position,
|
|
60
|
+
changes: { ...older.changes, ...newer.changes },
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function merge(journal: Journal, id: string, record: JournalRecord) {
|
|
65
|
+
const merged = mergeRecords(journal.get(id), record)
|
|
66
|
+
if (merged) {
|
|
67
|
+
journal.set(id, merged)
|
|
68
|
+
} else {
|
|
69
|
+
journal.delete(id)
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function record(journal: Journal, change: EntryChange, parent: string | null) {
|
|
74
|
+
const { id, options, legacy } = change
|
|
75
|
+
if (!options) {
|
|
76
|
+
return merge(journal, id, { kind: 'remove' })
|
|
77
|
+
}
|
|
78
|
+
const position = change.group.data.indexOf(options)
|
|
79
|
+
if (!legacy) {
|
|
80
|
+
return merge(journal, id, { kind: 'upsert', created: true, parent, position, changes: omit(options, ['id']) })
|
|
81
|
+
}
|
|
82
|
+
merge(journal, id, {
|
|
83
|
+
kind: 'upsert',
|
|
84
|
+
created: false,
|
|
85
|
+
parent: change.from ? parent : undefined,
|
|
86
|
+
position,
|
|
87
|
+
changes: diffOptions(legacy, options),
|
|
88
|
+
})
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface FlatEntry {
|
|
92
|
+
parent: string | null
|
|
93
|
+
position: number
|
|
94
|
+
options: EntryOptions
|
|
95
|
+
list: EntryOptions[]
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Index every entry (including nested group members) by id. */
|
|
99
|
+
export function flatten(data: EntryOptions[], parent: string | null = null, result = new Map<string, FlatEntry>()) {
|
|
100
|
+
data.forEach((options, position) => {
|
|
101
|
+
if (options.id) result.set(options.id, { parent, position, options, list: data })
|
|
102
|
+
if (options.group && Array.isArray(options.config)) {
|
|
103
|
+
flatten(options.config, options.id ?? null, result)
|
|
104
|
+
}
|
|
105
|
+
})
|
|
106
|
+
return result
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function applyChanges(options: EntryOptions, changes: Dict, filter?: (key: string) => boolean) {
|
|
110
|
+
for (const [key, value] of Object.entries(changes)) {
|
|
111
|
+
if (filter && !filter(key)) continue
|
|
112
|
+
if (value === undefined) {
|
|
113
|
+
delete options[key]
|
|
114
|
+
} else {
|
|
115
|
+
options[key] = value
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function detach(data: EntryOptions[], id: string) {
|
|
121
|
+
const flat = flatten(data).get(id)
|
|
122
|
+
if (!flat) return
|
|
123
|
+
flat.list.splice(flat.list.indexOf(flat.options), 1)
|
|
124
|
+
return flat.options
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Insert `options` under `parent` (a group id, or `null` for the root) at
|
|
129
|
+
* `position`, clamped to the group's length. Returns false when the parent
|
|
130
|
+
* group does not exist.
|
|
131
|
+
*/
|
|
132
|
+
export function place(data: EntryOptions[], options: EntryOptions, parent: string | null, position: number) {
|
|
133
|
+
let list = data
|
|
134
|
+
if (parent !== null) {
|
|
135
|
+
const group = flatten(data).get(parent)
|
|
136
|
+
if (!group?.options.group) return false
|
|
137
|
+
list = group.options.config ??= []
|
|
138
|
+
}
|
|
139
|
+
list.splice(Math.min(position, list.length), 0, options)
|
|
140
|
+
return true
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Overlay a journal onto `data` in place. `filter(id, key)` restricts which
|
|
145
|
+
* changes apply; `key` is `null` when asking about the entry as a whole
|
|
146
|
+
* (creation, removal, relocation).
|
|
147
|
+
*/
|
|
148
|
+
export function applyJournal(
|
|
149
|
+
data: EntryOptions[],
|
|
150
|
+
journal: Journal,
|
|
151
|
+
filter: (id: string, key: string | null) => boolean = () => true,
|
|
152
|
+
warn: (format: string, ...args: any[]) => void = () => {},
|
|
153
|
+
) {
|
|
154
|
+
for (const [id, record] of journal) {
|
|
155
|
+
if (!filter(id, null)) continue
|
|
156
|
+
if (record.kind === 'remove') {
|
|
157
|
+
detach(data, id)
|
|
158
|
+
continue
|
|
159
|
+
}
|
|
160
|
+
const flat = flatten(data).get(id)
|
|
161
|
+
if (!flat) {
|
|
162
|
+
const options = { id } as EntryOptions
|
|
163
|
+
applyChanges(options, record.changes, key => filter(id, key))
|
|
164
|
+
const parent = record.parent ?? null
|
|
165
|
+
if (!place(data, options, parent, record.position)) {
|
|
166
|
+
warn('cannot place entry %C: group %C not found', id, parent)
|
|
167
|
+
}
|
|
168
|
+
continue
|
|
169
|
+
}
|
|
170
|
+
applyChanges(flat.options, record.changes, key => filter(id, key))
|
|
171
|
+
if (record.parent !== undefined && flat.parent !== record.parent) {
|
|
172
|
+
detach(data, id)
|
|
173
|
+
if (!place(data, flat.options, record.parent, record.position)) {
|
|
174
|
+
warn('cannot move entry %C: group %C not found', id, record.parent)
|
|
175
|
+
place(data, flat.options, flat.parent, flat.position)
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return data
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export interface Conflict {
|
|
183
|
+
id: string
|
|
184
|
+
reason: string
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Three-way reconciliation of a journal against a new file state. The file
|
|
189
|
+
* wins every conflict: conflicting keys are dropped from the journal so that
|
|
190
|
+
* the tree follows the file, and each drop is reported.
|
|
191
|
+
*
|
|
192
|
+
* `fileOwned(id, key)` scopes the check to what the file can actually express
|
|
193
|
+
* (patch-owned keys and patch-inserted entries live elsewhere).
|
|
194
|
+
*/
|
|
195
|
+
export function reconcile(
|
|
196
|
+
journal: Journal,
|
|
197
|
+
base: Map<string, FlatEntry>,
|
|
198
|
+
theirs: Map<string, FlatEntry>,
|
|
199
|
+
fileOwned: (id: string, key: string | null) => boolean,
|
|
200
|
+
) {
|
|
201
|
+
const conflicts: Conflict[] = []
|
|
202
|
+
for (const [id, record] of journal) {
|
|
203
|
+
if (!fileOwned(id, null)) continue
|
|
204
|
+
const b = base.get(id)
|
|
205
|
+
const t = theirs.get(id)
|
|
206
|
+
if (record.kind === 'remove') {
|
|
207
|
+
if (!t) {
|
|
208
|
+
journal.delete(id)
|
|
209
|
+
} else if (!b || !deepEqual(t.options, b.options)) {
|
|
210
|
+
conflicts.push({ id, reason: b ? 'modified in file, removed at runtime' : 'added in file, removed at runtime' })
|
|
211
|
+
journal.delete(id)
|
|
212
|
+
}
|
|
213
|
+
continue
|
|
214
|
+
}
|
|
215
|
+
if (!t) {
|
|
216
|
+
if (b) {
|
|
217
|
+
conflicts.push({ id, reason: 'removed in file, modified at runtime' })
|
|
218
|
+
journal.delete(id)
|
|
219
|
+
}
|
|
220
|
+
// else: created at runtime, nothing in the file to conflict with
|
|
221
|
+
continue
|
|
222
|
+
}
|
|
223
|
+
if (record.created) record.created = false
|
|
224
|
+
for (const key of Object.keys(record.changes)) {
|
|
225
|
+
if (!fileOwned(id, key)) continue
|
|
226
|
+
const bv = b?.options[key]
|
|
227
|
+
const tv = t.options[key]
|
|
228
|
+
if (deepEqual(bv, tv) || deepEqual(tv, record.changes[key])) continue
|
|
229
|
+
conflicts.push({ id, reason: `key "${key}" modified both in file and at runtime` })
|
|
230
|
+
delete record.changes[key]
|
|
231
|
+
}
|
|
232
|
+
if (record.parent !== undefined && b && t.parent !== b.parent && t.parent !== record.parent) {
|
|
233
|
+
conflicts.push({ id, reason: 'moved both in file and at runtime' })
|
|
234
|
+
record.parent = undefined
|
|
235
|
+
}
|
|
236
|
+
const moved = record.parent !== undefined && record.parent !== t.parent
|
|
237
|
+
if (!Object.keys(record.changes).length && !moved) {
|
|
238
|
+
journal.delete(id)
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return conflicts
|
|
242
|
+
}
|
package/src/patch.ts
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import type { EntryOptions } from '@deepseek-ai/cordis-plugin-loader'
|
|
2
|
+
import { type Dict, omit } from '@deepseek-ai/cosmokit'
|
|
3
|
+
import { applyChanges, detach, flatten, type Journal, place } from './journal.ts'
|
|
4
|
+
|
|
5
|
+
export interface PatchOptions {
|
|
6
|
+
id?: string
|
|
7
|
+
insert?: EntryOptions[]
|
|
8
|
+
name?: string
|
|
9
|
+
config?: any
|
|
10
|
+
group?: boolean | null
|
|
11
|
+
disabled?: boolean | null
|
|
12
|
+
inject?: any
|
|
13
|
+
intercept?: any
|
|
14
|
+
isolate?: any
|
|
15
|
+
[key: string]: any
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type Warn = (format: string, ...args: any[]) => void
|
|
19
|
+
|
|
20
|
+
function randomId(used: (id: string) => boolean) {
|
|
21
|
+
let id: string
|
|
22
|
+
do {
|
|
23
|
+
id = Math.random().toString(16).slice(2, 10)
|
|
24
|
+
} while (used(id))
|
|
25
|
+
return id
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Give every entry (nested groups included) an id, in place. */
|
|
29
|
+
export function ensureIds(entries: EntryOptions[], used: (id: string) => boolean) {
|
|
30
|
+
for (const entry of entries) {
|
|
31
|
+
if (!entry.id) entry.id = randomId(used)
|
|
32
|
+
if (entry.group && Array.isArray(entry.config)) ensureIds(entry.config, used)
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Runtime changes are routed back to an inserted entry by its id, so every insert needs one. */
|
|
37
|
+
export function ensureInsertIds(patches: PatchOptions[] | undefined, used: (id: string) => boolean) {
|
|
38
|
+
for (const patch of patches ?? []) {
|
|
39
|
+
if (patch.insert) ensureIds(patch.insert, used)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Overlay `patches` onto a fresh clone of `data`. Inserted entries are cloned
|
|
45
|
+
* too, so the tree owns every object it mounts.
|
|
46
|
+
*/
|
|
47
|
+
export function applyPatches(data: EntryOptions[], patches: PatchOptions[] | undefined, warn: Warn): EntryOptions[] {
|
|
48
|
+
data = structuredClone(data)
|
|
49
|
+
if (!patches?.length) return data
|
|
50
|
+
|
|
51
|
+
const entryMap = new Map<string, EntryOptions>()
|
|
52
|
+
const buildMap = (entries: EntryOptions[]) => {
|
|
53
|
+
for (const entry of entries) {
|
|
54
|
+
if (entry.id) entryMap.set(entry.id, entry)
|
|
55
|
+
if (entry.group && Array.isArray(entry.config)) {
|
|
56
|
+
buildMap(entry.config)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
buildMap(data)
|
|
61
|
+
|
|
62
|
+
for (const patch of patches) {
|
|
63
|
+
const { id, insert, name, ...overrides } = patch
|
|
64
|
+
|
|
65
|
+
if (insert) {
|
|
66
|
+
const clone = structuredClone(insert)
|
|
67
|
+
if (id) {
|
|
68
|
+
const target = entryMap.get(id)
|
|
69
|
+
if (!target) {
|
|
70
|
+
warn('patch insert: entry %C not found', id)
|
|
71
|
+
continue
|
|
72
|
+
}
|
|
73
|
+
if (!target.group) {
|
|
74
|
+
warn('patch insert: entry %C is not a group', id)
|
|
75
|
+
continue
|
|
76
|
+
}
|
|
77
|
+
if (!Array.isArray(target.config)) target.config = []
|
|
78
|
+
target.config.push(...clone)
|
|
79
|
+
} else {
|
|
80
|
+
data.push(...clone)
|
|
81
|
+
}
|
|
82
|
+
buildMap(clone)
|
|
83
|
+
continue
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (!id) {
|
|
87
|
+
warn('patch: id is required for non-insert patches')
|
|
88
|
+
continue
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const target = entryMap.get(id)
|
|
92
|
+
if (!target) {
|
|
93
|
+
warn('patch: entry %C not found', id)
|
|
94
|
+
continue
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (name && name !== target.name) {
|
|
98
|
+
warn('patch: name mismatch for %C (expected %C, got %C), skipping', id, target.name, name)
|
|
99
|
+
continue
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
for (const [key, value] of Object.entries(overrides)) {
|
|
103
|
+
if (key === 'id') continue
|
|
104
|
+
target[key] = value
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return data
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Who owns an entry, or one key of it, in the patched tree: the file, a patch
|
|
113
|
+
* that overrides that key (the last one wins), or the patch that inserted the
|
|
114
|
+
* whole entry.
|
|
115
|
+
*/
|
|
116
|
+
export type EntryOwner = EntryOwner.File | EntryOwner.Patch | EntryOwner.Insert
|
|
117
|
+
|
|
118
|
+
export namespace EntryOwner {
|
|
119
|
+
export interface File {
|
|
120
|
+
type: 'file'
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export interface Patch {
|
|
124
|
+
type: 'patch'
|
|
125
|
+
/** Index into the patch list. */
|
|
126
|
+
index: number
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export interface Insert {
|
|
130
|
+
type: 'insert'
|
|
131
|
+
/** Index into the patch list. */
|
|
132
|
+
index: number
|
|
133
|
+
/** The insert list (or nested group config) holding the entry. */
|
|
134
|
+
list: EntryOptions[]
|
|
135
|
+
options: EntryOptions
|
|
136
|
+
/** The group the entry is inserted into (`null` for the root). */
|
|
137
|
+
parent: string | null
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export class PatchIndex {
|
|
142
|
+
private keys = new Map<string, Map<string, number>>()
|
|
143
|
+
private inserts = new Map<string, EntryOwner.Insert>()
|
|
144
|
+
|
|
145
|
+
constructor(public patches: PatchOptions[]) {
|
|
146
|
+
patches.forEach((patch, index) => {
|
|
147
|
+
const { id, insert } = patch
|
|
148
|
+
if (insert) {
|
|
149
|
+
const walk = (list: EntryOptions[], parent: string | null) => {
|
|
150
|
+
for (const options of list) {
|
|
151
|
+
this.inserts.set(options.id, { type: 'insert', index, list, options, parent })
|
|
152
|
+
if (options.group && Array.isArray(options.config)) walk(options.config, options.id)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
walk(insert, id ?? null)
|
|
156
|
+
return
|
|
157
|
+
}
|
|
158
|
+
if (!id) return
|
|
159
|
+
const keys = this.keys.get(id) ?? new Map<string, number>()
|
|
160
|
+
for (const key of Object.keys(omit(patch, ['id', 'insert', 'name']))) {
|
|
161
|
+
keys.set(key, index)
|
|
162
|
+
}
|
|
163
|
+
this.keys.set(id, keys)
|
|
164
|
+
})
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Owner of the entry as a whole. */
|
|
168
|
+
entry(id: string | null): EntryOwner {
|
|
169
|
+
if (id === null) return { type: 'file' }
|
|
170
|
+
return this.inserts.get(id) ?? { type: 'file' }
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Owner of one key of an entry. */
|
|
174
|
+
key(id: string, key: string): EntryOwner {
|
|
175
|
+
const insert = this.inserts.get(id)
|
|
176
|
+
if (insert) return insert
|
|
177
|
+
const index = this.keys.get(id)?.get(key)
|
|
178
|
+
return index === undefined ? { type: 'file' } : { type: 'patch', index }
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
fileOwned(id: string, key: string | null) {
|
|
182
|
+
return (key === null ? this.entry(id) : this.key(id, key)).type === 'file'
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Route a journal to its owners: file-owned changes are applied to `data`,
|
|
188
|
+
* patch-owned ones to `patches`. Both are mutated in place, so pass clones.
|
|
189
|
+
* Returns whether any patch changed.
|
|
190
|
+
*/
|
|
191
|
+
export function routeJournal(journal: Journal, data: EntryOptions[], patches: PatchOptions[], warn: Warn) {
|
|
192
|
+
const index = new PatchIndex(patches)
|
|
193
|
+
let patched = false
|
|
194
|
+
|
|
195
|
+
const insertList = (owner: EntryOwner.Insert) => owner.options.config ??= []
|
|
196
|
+
|
|
197
|
+
const placeAt = (options: EntryOptions, parent: string | null, position: number) => {
|
|
198
|
+
const owner = index.entry(parent)
|
|
199
|
+
if (owner.type === 'insert') {
|
|
200
|
+
const list = insertList(owner)
|
|
201
|
+
list.splice(Math.min(position, list.length), 0, options)
|
|
202
|
+
patched = true
|
|
203
|
+
} else if (!place(data, options, parent, position)) {
|
|
204
|
+
warn('cannot place entry %C: group %C not found', options.id, parent)
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
for (const [id, record] of journal) {
|
|
209
|
+
const owner = index.entry(id)
|
|
210
|
+
if (record.kind === 'remove') {
|
|
211
|
+
if (owner.type === 'insert') {
|
|
212
|
+
owner.list.splice(owner.list.indexOf(owner.options), 1)
|
|
213
|
+
patched = true
|
|
214
|
+
} else {
|
|
215
|
+
detach(data, id)
|
|
216
|
+
}
|
|
217
|
+
continue
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const current = owner.type === 'insert' ? owner : flatten(data).get(id)
|
|
221
|
+
if (!current) {
|
|
222
|
+
const options = { id } as EntryOptions
|
|
223
|
+
applyChanges(options, record.changes)
|
|
224
|
+
placeAt(options, record.parent ?? null, record.position)
|
|
225
|
+
continue
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (record.parent !== undefined && current.parent !== record.parent) {
|
|
229
|
+
// moving across owners: carry the full options over
|
|
230
|
+
if (owner.type === 'insert') {
|
|
231
|
+
owner.list.splice(owner.list.indexOf(owner.options), 1)
|
|
232
|
+
patched = true
|
|
233
|
+
} else {
|
|
234
|
+
detach(data, id)
|
|
235
|
+
}
|
|
236
|
+
applyChanges(current.options, record.changes)
|
|
237
|
+
placeAt(current.options, record.parent, record.position)
|
|
238
|
+
continue
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (owner.type === 'insert') {
|
|
242
|
+
applyChanges(current.options, record.changes)
|
|
243
|
+
patched = true
|
|
244
|
+
continue
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const patchChanges: Dict<Dict> = {}
|
|
248
|
+
applyChanges(current.options, record.changes, (key) => {
|
|
249
|
+
const keyOwner = index.key(id, key)
|
|
250
|
+
if (keyOwner.type !== 'patch') return true
|
|
251
|
+
;(patchChanges[keyOwner.index] ??= {})[key] = record.changes[key]
|
|
252
|
+
return false
|
|
253
|
+
})
|
|
254
|
+
for (const [i, changes] of Object.entries(patchChanges)) {
|
|
255
|
+
applyChanges(patches[i] as any, changes)
|
|
256
|
+
patched = true
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return patched
|
|
261
|
+
}
|