@contember/binding 1.1.0-alpha.1 → 1.1.0-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/dist/development/accessorPropagation/useEntityListSubTree.js +4 -2
- package/dist/development/accessorPropagation/useEntityListSubTree.js.map +1 -1
- package/dist/development/accessorPropagation/useEntitySubTree.js +4 -2
- package/dist/development/accessorPropagation/useEntitySubTree.js.map +1 -1
- package/dist/development/accessorPropagation/useExtendTree.js +25 -0
- package/dist/development/accessorPropagation/useExtendTree.js.map +1 -0
- package/dist/development/accessorTree/AccessorTree.js +9 -4
- package/dist/development/accessorTree/AccessorTree.js.map +1 -1
- package/dist/development/accessorTree/AccessorTreeStateContext.js +6 -0
- package/dist/development/accessorTree/AccessorTreeStateContext.js.map +1 -0
- package/dist/development/accessorTree/useAccessorTreeState.js +5 -0
- package/dist/development/accessorTree/useAccessorTreeState.js.map +1 -0
- package/dist/development/core/DataBinding.js +1 -1
- package/dist/development/core/DataBinding.js.map +1 -1
- package/dist/development/core/DirtinessTracker.js +4 -13
- package/dist/development/core/DirtinessTracker.js.map +1 -1
- package/dist/development/core/EventManager.js +12 -7
- package/dist/development/core/EventManager.js.map +1 -1
- package/dist/development/core/MutationGenerator.js +95 -89
- package/dist/development/core/MutationGenerator.js.map +1 -1
- package/dist/development/core/StateInitializer.js +15 -18
- package/dist/development/core/StateInitializer.js.map +1 -1
- package/dist/development/core/operations/EntityOperations.js +8 -7
- package/dist/development/core/operations/EntityOperations.js.map +1 -1
- package/dist/development/core/operations/ListOperations.js +1 -1
- package/dist/development/core/operations/ListOperations.js.map +1 -1
- package/dist/development/core/schema/SchemaPreprocessor.js +1 -1
- package/dist/development/core/schema/SchemaPreprocessor.js.map +1 -1
- package/dist/development/dao/TreeNodeEnvironmentFactory.js +2 -2
- package/dist/development/dao/TreeNodeEnvironmentFactory.js.map +1 -1
- package/dist/development/helperComponents/If.js +39 -0
- package/dist/development/helperComponents/If.js.map +1 -0
- package/dist/development/helperComponents/SugaredField.js +1 -1
- package/dist/development/helperComponents/helpers/FilterEvaluator.js +38 -0
- package/dist/development/helperComponents/helpers/FilterEvaluator.js.map +1 -0
- package/dist/development/helperComponents/helpers/FilterFieldsCollector.js +41 -0
- package/dist/development/helperComponents/helpers/FilterFieldsCollector.js.map +1 -0
- package/dist/development/helperComponents/helpers/evaluateCondition.js +46 -0
- package/dist/development/helperComponents/helpers/evaluateCondition.js.map +1 -0
- package/dist/development/index.js +3 -0
- package/dist/development/index.js.map +1 -1
- package/dist/production/accessorPropagation/useEntityListSubTree.js +4 -2
- package/dist/production/accessorPropagation/useEntityListSubTree.js.map +1 -1
- package/dist/production/accessorPropagation/useEntitySubTree.js +4 -2
- package/dist/production/accessorPropagation/useEntitySubTree.js.map +1 -1
- package/dist/production/accessorPropagation/useExtendTree.js +25 -0
- package/dist/production/accessorPropagation/useExtendTree.js.map +1 -0
- package/dist/production/accessorTree/AccessorTree.js +9 -4
- package/dist/production/accessorTree/AccessorTree.js.map +1 -1
- package/dist/production/accessorTree/AccessorTreeStateContext.js +6 -0
- package/dist/production/accessorTree/AccessorTreeStateContext.js.map +1 -0
- package/dist/production/accessorTree/useAccessorTreeState.js +5 -0
- package/dist/production/accessorTree/useAccessorTreeState.js.map +1 -0
- package/dist/production/core/DataBinding.js +1 -1
- package/dist/production/core/DataBinding.js.map +1 -1
- package/dist/production/core/DirtinessTracker.js +4 -13
- package/dist/production/core/DirtinessTracker.js.map +1 -1
- package/dist/production/core/EventManager.js +11 -6
- package/dist/production/core/EventManager.js.map +1 -1
- package/dist/production/core/MutationGenerator.js +95 -89
- package/dist/production/core/MutationGenerator.js.map +1 -1
- package/dist/production/core/StateInitializer.js +15 -18
- package/dist/production/core/StateInitializer.js.map +1 -1
- package/dist/production/core/operations/EntityOperations.js +8 -7
- package/dist/production/core/operations/EntityOperations.js.map +1 -1
- package/dist/production/core/operations/ListOperations.js +1 -1
- package/dist/production/core/operations/ListOperations.js.map +1 -1
- package/dist/production/core/schema/SchemaPreprocessor.js +1 -1
- package/dist/production/core/schema/SchemaPreprocessor.js.map +1 -1
- package/dist/production/dao/TreeNodeEnvironmentFactory.js +2 -2
- package/dist/production/dao/TreeNodeEnvironmentFactory.js.map +1 -1
- package/dist/production/helperComponents/If.js +39 -0
- package/dist/production/helperComponents/If.js.map +1 -0
- package/dist/production/helperComponents/SugaredField.js +1 -1
- package/dist/production/helperComponents/helpers/FilterEvaluator.js +38 -0
- package/dist/production/helperComponents/helpers/FilterEvaluator.js.map +1 -0
- package/dist/production/helperComponents/helpers/FilterFieldsCollector.js +41 -0
- package/dist/production/helperComponents/helpers/FilterFieldsCollector.js.map +1 -0
- package/dist/production/helperComponents/helpers/evaluateCondition.js +46 -0
- package/dist/production/helperComponents/helpers/evaluateCondition.js.map +1 -0
- package/dist/production/index.js +3 -0
- package/dist/production/index.js.map +1 -1
- package/dist/types/accessorPropagation/index.d.ts +1 -0
- package/dist/types/accessorPropagation/index.d.ts.map +1 -1
- package/dist/types/accessorPropagation/useEntityListSubTree.d.ts +2 -1
- package/dist/types/accessorPropagation/useEntityListSubTree.d.ts.map +1 -1
- package/dist/types/accessorPropagation/useEntitySubTree.d.ts +2 -1
- package/dist/types/accessorPropagation/useEntitySubTree.d.ts.map +1 -1
- package/dist/types/accessorPropagation/useExtendTree.d.ts +5 -0
- package/dist/types/accessorPropagation/useExtendTree.d.ts.map +1 -0
- package/dist/types/accessorTree/AccessorTree.d.ts.map +1 -1
- package/dist/types/accessorTree/AccessorTreeStateContext.d.ts +4 -0
- package/dist/types/accessorTree/AccessorTreeStateContext.d.ts.map +1 -0
- package/dist/types/accessorTree/index.d.ts +1 -0
- package/dist/types/accessorTree/index.d.ts.map +1 -1
- package/dist/types/accessorTree/useAccessorTreeState.d.ts +2 -0
- package/dist/types/accessorTree/useAccessorTreeState.d.ts.map +1 -0
- package/dist/types/core/DataBinding.d.ts.map +1 -1
- package/dist/types/core/DirtinessTracker.d.ts +2 -5
- package/dist/types/core/DirtinessTracker.d.ts.map +1 -1
- package/dist/types/core/EventManager.d.ts +1 -1
- package/dist/types/core/EventManager.d.ts.map +1 -1
- package/dist/types/core/MutationGenerator.d.ts.map +1 -1
- package/dist/types/core/StateInitializer.d.ts +1 -5
- package/dist/types/core/StateInitializer.d.ts.map +1 -1
- package/dist/types/core/operations/EntityOperations.d.ts.map +1 -1
- package/dist/types/core/operations/ListOperations.d.ts.map +1 -1
- package/dist/types/core/schema/RawSchemaEntity.d.ts +3 -2
- package/dist/types/core/schema/RawSchemaEntity.d.ts.map +1 -1
- package/dist/types/core/schema/SchemaEntity.d.ts +1 -1
- package/dist/types/core/schema/SchemaEntity.d.ts.map +1 -1
- package/dist/types/helperComponents/If.d.ts +14 -0
- package/dist/types/helperComponents/If.d.ts.map +1 -0
- package/dist/types/helperComponents/helpers/FilterEvaluator.d.ts +9 -0
- package/dist/types/helperComponents/helpers/FilterEvaluator.d.ts.map +1 -0
- package/dist/types/helperComponents/helpers/FilterFieldsCollector.d.ts +10 -0
- package/dist/types/helperComponents/helpers/FilterFieldsCollector.d.ts.map +1 -0
- package/dist/types/helperComponents/helpers/evaluateCondition.d.ts +4 -0
- package/dist/types/helperComponents/helpers/evaluateCondition.d.ts.map +1 -0
- package/dist/types/helperComponents/index.d.ts +1 -0
- package/dist/types/helperComponents/index.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +5 -5
- package/src/accessorPropagation/index.ts +1 -0
- package/src/accessorPropagation/useEntityListSubTree.ts +8 -1
- package/src/accessorPropagation/useEntitySubTree.ts +8 -1
- package/src/accessorPropagation/useExtendTree.ts +28 -0
- package/src/accessorTree/AccessorTree.tsx +18 -12
- package/src/accessorTree/AccessorTreeStateContext.ts +6 -0
- package/src/accessorTree/index.ts +1 -0
- package/src/accessorTree/useAccessorTreeState.ts +4 -0
- package/src/core/DataBinding.ts +0 -1
- package/src/core/DirtinessTracker.ts +4 -16
- package/src/core/EventManager.ts +14 -8
- package/src/core/MutationGenerator.ts +132 -125
- package/src/core/StateInitializer.ts +22 -29
- package/src/core/operations/EntityOperations.ts +8 -9
- package/src/core/operations/ListOperations.ts +2 -1
- package/src/core/schema/RawSchemaEntity.ts +1 -2
- package/src/core/schema/SchemaEntity.ts +1 -1
- package/src/core/schema/SchemaPreprocessor.ts +1 -1
- package/src/dao/TreeNodeEnvironmentFactory.ts +2 -2
- package/src/helperComponents/If.tsx +71 -0
- package/src/helperComponents/helpers/FilterEvaluator.ts +48 -0
- package/src/helperComponents/helpers/FilterFieldsCollector.ts +49 -0
- package/src/helperComponents/helpers/evaluateCondition.ts +52 -0
- package/src/helperComponents/index.ts +1 -0
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { ReactNode } from 'react'
|
|
2
2
|
import { BindingOperationsProvider } from '../accessorPropagation'
|
|
3
3
|
import type { AccessorTreeState } from './AccessorTreeState'
|
|
4
|
+
import { AccessorTreeStateContext } from './AccessorTreeStateContext'
|
|
4
5
|
import { DirtinessContext } from './DirtinessContext'
|
|
5
6
|
import { MutationStateContext } from './MutationStateContext'
|
|
6
7
|
|
|
@@ -14,21 +15,26 @@ export function AccessorTree({ state, children }: AccessorTreeProps) {
|
|
|
14
15
|
// remounts.
|
|
15
16
|
if (state.name === 'initialized') {
|
|
16
17
|
return (
|
|
17
|
-
<
|
|
18
|
-
<
|
|
19
|
-
<
|
|
20
|
-
{
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
18
|
+
<AccessorTreeStateContext.Provider value={state}>
|
|
19
|
+
<DirtinessContext.Provider value={state.data.hasUnpersistedChanges}>
|
|
20
|
+
<MutationStateContext.Provider value={state.data.isMutating}>
|
|
21
|
+
<BindingOperationsProvider bindingOperations={state.data.bindingOperations}>
|
|
22
|
+
{children}
|
|
23
|
+
</BindingOperationsProvider>
|
|
24
|
+
</MutationStateContext.Provider>
|
|
25
|
+
</DirtinessContext.Provider>
|
|
26
|
+
</AccessorTreeStateContext.Provider>
|
|
24
27
|
)
|
|
25
28
|
}
|
|
26
29
|
return (
|
|
27
|
-
<
|
|
28
|
-
<
|
|
29
|
-
<
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
<AccessorTreeStateContext.Provider value={state}>
|
|
31
|
+
<DirtinessContext.Provider value={false}>
|
|
32
|
+
<MutationStateContext.Provider value={false}>
|
|
33
|
+
<BindingOperationsProvider bindingOperations={undefined}>{children}</BindingOperationsProvider>
|
|
34
|
+
</MutationStateContext.Provider>
|
|
35
|
+
</DirtinessContext.Provider>
|
|
36
|
+
</AccessorTreeStateContext.Provider>
|
|
32
37
|
)
|
|
33
38
|
}
|
|
39
|
+
|
|
34
40
|
AccessorTree.displayName = 'AccessorTree'
|
|
@@ -13,6 +13,7 @@ export * from './PersistResult'
|
|
|
13
13
|
export * from './QueryRequestResponse'
|
|
14
14
|
export * from './RequestError'
|
|
15
15
|
export * from './RuntimeId'
|
|
16
|
+
export * from './useAccessorTreeState'
|
|
16
17
|
export * from './useDataBinding'
|
|
17
18
|
export * from './useDirtinessState'
|
|
18
19
|
export * from './useMutationState'
|
package/src/core/DataBinding.ts
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
export class DirtinessTracker {
|
|
2
2
|
private changesCount = 0
|
|
3
|
-
private
|
|
3
|
+
private touchedCount = 0
|
|
4
4
|
|
|
5
|
-
public
|
|
6
|
-
return this.
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
public getTotalChangesCount() {
|
|
10
|
-
return this.totalChangesCount
|
|
5
|
+
public getTotalTouchCount() {
|
|
6
|
+
return this.touchedCount
|
|
11
7
|
}
|
|
12
8
|
|
|
13
9
|
public hasChanges() {
|
|
@@ -18,16 +14,8 @@ export class DirtinessTracker {
|
|
|
18
14
|
this.changesCount = 0
|
|
19
15
|
}
|
|
20
16
|
|
|
21
|
-
public increment() {
|
|
22
|
-
this.totalChangesCount++
|
|
23
|
-
this.changesCount++
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
public decrement() {
|
|
27
|
-
this.changesCount--
|
|
28
|
-
}
|
|
29
|
-
|
|
30
17
|
public increaseBy(delta: number) {
|
|
31
18
|
this.changesCount += delta
|
|
19
|
+
this.touchedCount++
|
|
32
20
|
}
|
|
33
21
|
}
|
package/src/core/EventManager.ts
CHANGED
|
@@ -346,9 +346,12 @@ export class EventManager {
|
|
|
346
346
|
|
|
347
347
|
for (const [state, callback] of callbackQueue) {
|
|
348
348
|
correspondingStates.push(state)
|
|
349
|
+
const changesCountBefore = this.dirtinessTracker.getTotalTouchCount()
|
|
350
|
+
let validateChanges = false
|
|
349
351
|
try {
|
|
350
352
|
const result = callback(state.getAccessor as any, options as any) // TS can't quite handle this but this is sound.
|
|
351
353
|
if (result instanceof Promise) {
|
|
354
|
+
validateChanges = true
|
|
352
355
|
promiseReturns.push(result)
|
|
353
356
|
} else {
|
|
354
357
|
promiseReturns.push(Promise.resolve(result))
|
|
@@ -356,10 +359,15 @@ export class EventManager {
|
|
|
356
359
|
} catch (e) {
|
|
357
360
|
promiseReturns.push(Promise.reject(e))
|
|
358
361
|
}
|
|
362
|
+
if (validateChanges) {
|
|
363
|
+
this.validateWithoutChanges(changesCountBefore, eventType)
|
|
364
|
+
}
|
|
359
365
|
}
|
|
360
366
|
// TODO timeout
|
|
361
367
|
|
|
362
|
-
const
|
|
368
|
+
const changesCountBefore = this.dirtinessTracker.getTotalTouchCount()
|
|
369
|
+
const newCallbacks = await Promise.allSettled(promiseReturns)
|
|
370
|
+
this.validateWithoutChanges(changesCountBefore, eventType)
|
|
363
371
|
|
|
364
372
|
callbackQueue.length = 0 // Empties the queue
|
|
365
373
|
|
|
@@ -561,20 +569,18 @@ export class EventManager {
|
|
|
561
569
|
}
|
|
562
570
|
}
|
|
563
571
|
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
const
|
|
567
|
-
const changesCountAfter = this.dirtinessTracker.getTotalChangesCount()
|
|
572
|
+
|
|
573
|
+
private validateWithoutChanges(initialCount: number, eventType: string): void {
|
|
574
|
+
const changesCountAfter = this.dirtinessTracker.getTotalTouchCount()
|
|
568
575
|
if (import.meta.env.DEV) {
|
|
569
|
-
if (
|
|
576
|
+
if (initialCount !== changesCountAfter) {
|
|
570
577
|
// This isn't bulletproof. They could e.g. undo a change and make another one which would
|
|
571
578
|
// slip through this detection. But for most cases, it should be good enough and not too expensive.
|
|
572
579
|
throw new BindingError(
|
|
573
580
|
`A ${eventType} event handler cannot be asynchronous and alter the accessor tree at the same time. ` +
|
|
574
|
-
|
|
581
|
+
`To achieve this, prepare your data asynchronously but only touch the tree from a returned callback.`,
|
|
575
582
|
)
|
|
576
583
|
}
|
|
577
584
|
}
|
|
578
|
-
return result
|
|
579
585
|
}
|
|
580
586
|
}
|
|
@@ -15,7 +15,7 @@ import {
|
|
|
15
15
|
EntityRealmStateStub,
|
|
16
16
|
EntityState,
|
|
17
17
|
FieldState,
|
|
18
|
-
getEntityMarker,
|
|
18
|
+
getEntityMarker, RootStateNode, StateIterator,
|
|
19
19
|
} from './state'
|
|
20
20
|
import type { TreeStore } from './TreeStore'
|
|
21
21
|
|
|
@@ -25,15 +25,20 @@ type ProcessedPlaceholdersByEntity = Map<EntityState, Set<PlaceholderName>>
|
|
|
25
25
|
|
|
26
26
|
// TODO enforce correct expected mutations in dev mode.
|
|
27
27
|
export class MutationGenerator {
|
|
28
|
-
public constructor(private readonly treeStore: TreeStore) {
|
|
28
|
+
public constructor(private readonly treeStore: TreeStore) {
|
|
29
|
+
}
|
|
29
30
|
|
|
30
31
|
public getPersistMutation(): string | undefined {
|
|
31
32
|
try {
|
|
32
33
|
let builder: QueryBuilder = new CrudQueryBuilder.CrudQueryBuilder()
|
|
33
34
|
const processedPlaceholdersByEntity: ProcessedPlaceholdersByEntity = new Map()
|
|
34
35
|
|
|
35
|
-
|
|
36
|
-
|
|
36
|
+
|
|
37
|
+
for (const [treeRootId, rootStates] of this.treeStore.subTreeStatesByRoot.entries()) {
|
|
38
|
+
for (const [placeholderName, subTreeState] of Array.from(rootStates.entries()).reverse()) {
|
|
39
|
+
if (subTreeState.unpersistedChangesCount <= 0) {
|
|
40
|
+
continue
|
|
41
|
+
}
|
|
37
42
|
if (subTreeState.type === 'entityRealm') {
|
|
38
43
|
builder = this.addSubMutation(
|
|
39
44
|
processedPlaceholdersByEntity,
|
|
@@ -88,6 +93,7 @@ export class MutationGenerator {
|
|
|
88
93
|
}
|
|
89
94
|
}
|
|
90
95
|
}
|
|
96
|
+
|
|
91
97
|
return builder.inTransaction(undefined, { deferForeignKeyConstraints: true }).getGql()
|
|
92
98
|
} catch (e) {
|
|
93
99
|
return undefined
|
|
@@ -287,142 +293,143 @@ export class MutationGenerator {
|
|
|
287
293
|
return builder
|
|
288
294
|
}
|
|
289
295
|
|
|
290
|
-
const
|
|
296
|
+
for (const siblingState of StateIterator.eachSiblingRealm(currentState)) {
|
|
291
297
|
|
|
292
|
-
|
|
293
|
-
| { type: 'field'; marker: FieldMarker; fieldState: FieldState }
|
|
294
|
-
| { type: 'hasOne'; marker: HasOneRelationMarker; fieldState: EntityRealmState | EntityRealmStateStub }
|
|
295
|
-
| { type: 'hasMany'; marker: HasManyRelationMarker; fieldState: EntityListState }
|
|
296
|
-
> = []
|
|
297
|
-
|
|
298
|
-
for (const [placeholderName, fieldState] of currentState.children) {
|
|
299
|
-
if (processedPlaceholders.has(placeholderName)) {
|
|
300
|
-
continue
|
|
301
|
-
}
|
|
302
|
-
processedPlaceholders.add(placeholderName)
|
|
298
|
+
const pathBack = this.treeStore.getPathBackToParent(siblingState)
|
|
303
299
|
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
const placeholderName = marker.placeholderName
|
|
300
|
+
const nonbearingFields: Array<| { type: 'field'; marker: FieldMarker; fieldState: FieldState }
|
|
301
|
+
| { type: 'hasOne'; marker: HasOneRelationMarker; fieldState: EntityRealmState | EntityRealmStateStub }
|
|
302
|
+
| { type: 'hasMany'; marker: HasManyRelationMarker; fieldState: EntityListState }> = []
|
|
308
303
|
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
) {
|
|
313
|
-
continue
|
|
314
|
-
}
|
|
315
|
-
if (marker.isNonbearing) {
|
|
316
|
-
nonbearingFields.push({
|
|
317
|
-
type: 'field',
|
|
318
|
-
marker,
|
|
319
|
-
fieldState,
|
|
320
|
-
})
|
|
321
|
-
continue
|
|
322
|
-
}
|
|
323
|
-
builder = this.registerCreateFieldPart(fieldState, marker, builder)
|
|
324
|
-
break
|
|
325
|
-
}
|
|
326
|
-
case 'entityRealmStub':
|
|
327
|
-
case 'entityRealm': {
|
|
328
|
-
const marker = getEntityMarker(fieldState)
|
|
329
|
-
if (!(marker instanceof HasOneRelationMarker)) {
|
|
330
|
-
throw new BindingError()
|
|
331
|
-
}
|
|
332
|
-
if (pathBack?.fieldBackToParent === marker.parameters.field) {
|
|
333
|
-
continue
|
|
334
|
-
}
|
|
335
|
-
if (marker.parameters.isNonbearing) {
|
|
336
|
-
nonbearingFields.push({
|
|
337
|
-
type: 'hasOne',
|
|
338
|
-
marker,
|
|
339
|
-
fieldState,
|
|
340
|
-
})
|
|
341
|
-
continue
|
|
342
|
-
}
|
|
343
|
-
builder = this.registerCreateEntityPart(processedPlaceholdersByEntity, fieldState, marker, builder)
|
|
344
|
-
break
|
|
345
|
-
}
|
|
346
|
-
case 'entityList': {
|
|
347
|
-
const marker = fieldState.blueprint.marker
|
|
348
|
-
if (!(marker instanceof HasManyRelationMarker)) {
|
|
349
|
-
throw new BindingError()
|
|
350
|
-
}
|
|
351
|
-
if (pathBack?.fieldBackToParent === marker.parameters.field) {
|
|
352
|
-
continue
|
|
353
|
-
}
|
|
354
|
-
if (marker.parameters.isNonbearing) {
|
|
355
|
-
nonbearingFields.push({
|
|
356
|
-
type: 'hasMany',
|
|
357
|
-
marker,
|
|
358
|
-
fieldState,
|
|
359
|
-
})
|
|
360
|
-
continue
|
|
361
|
-
}
|
|
362
|
-
builder = this.registerCreateEntityListPart(processedPlaceholdersByEntity, fieldState, marker, builder)
|
|
363
|
-
break
|
|
304
|
+
for (const [placeholderName, fieldState] of siblingState.children) {
|
|
305
|
+
if (processedPlaceholders.has(placeholderName)) {
|
|
306
|
+
continue
|
|
364
307
|
}
|
|
365
|
-
|
|
366
|
-
return assertNever(fieldState)
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
}
|
|
308
|
+
processedPlaceholders.add(placeholderName)
|
|
370
309
|
|
|
371
|
-
|
|
372
|
-
// currentState.blueprint.creationParameters.forceCreation &&
|
|
373
|
-
// (builder.data === undefined || isEmptyObject(builder.data))
|
|
374
|
-
// ) {
|
|
375
|
-
// builder = builder.set('_dummy_field_', true)
|
|
376
|
-
// }
|
|
377
|
-
|
|
378
|
-
if (
|
|
379
|
-
(builder.data !== undefined && !isEmptyObject(builder.data)) ||
|
|
380
|
-
!getEntityMarker(currentState).fields.hasAtLeastOneBearingField
|
|
381
|
-
) {
|
|
382
|
-
for (const field of nonbearingFields) {
|
|
383
|
-
switch (field.type) {
|
|
310
|
+
switch (fieldState.type) {
|
|
384
311
|
case 'field': {
|
|
385
|
-
|
|
312
|
+
const marker = fieldState.fieldMarker
|
|
313
|
+
const placeholderName = marker.placeholderName
|
|
314
|
+
|
|
315
|
+
if (
|
|
316
|
+
placeholderName === TYPENAME_KEY_NAME ||
|
|
317
|
+
(placeholderName === PRIMARY_KEY_NAME && !(siblingState.entity.id instanceof ClientGeneratedUuid))
|
|
318
|
+
) {
|
|
319
|
+
continue
|
|
320
|
+
}
|
|
321
|
+
if (marker.isNonbearing) {
|
|
322
|
+
nonbearingFields.push({
|
|
323
|
+
type: 'field',
|
|
324
|
+
marker,
|
|
325
|
+
fieldState,
|
|
326
|
+
})
|
|
327
|
+
continue
|
|
328
|
+
}
|
|
329
|
+
builder = this.registerCreateFieldPart(fieldState, marker, builder)
|
|
386
330
|
break
|
|
387
331
|
}
|
|
388
|
-
case '
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
)
|
|
332
|
+
case 'entityRealmStub':
|
|
333
|
+
case 'entityRealm': {
|
|
334
|
+
const marker = getEntityMarker(fieldState)
|
|
335
|
+
if (!(marker instanceof HasOneRelationMarker)) {
|
|
336
|
+
throw new BindingError()
|
|
337
|
+
}
|
|
338
|
+
if (pathBack?.fieldBackToParent === marker.parameters.field) {
|
|
339
|
+
continue
|
|
340
|
+
}
|
|
341
|
+
if (marker.parameters.isNonbearing) {
|
|
342
|
+
nonbearingFields.push({
|
|
343
|
+
type: 'hasOne',
|
|
344
|
+
marker,
|
|
345
|
+
fieldState,
|
|
346
|
+
})
|
|
347
|
+
continue
|
|
348
|
+
}
|
|
349
|
+
builder = this.registerCreateEntityPart(processedPlaceholdersByEntity, fieldState, marker, builder)
|
|
395
350
|
break
|
|
396
351
|
}
|
|
397
|
-
case '
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
352
|
+
case 'entityList': {
|
|
353
|
+
const marker = fieldState.blueprint.marker
|
|
354
|
+
if (!(marker instanceof HasManyRelationMarker)) {
|
|
355
|
+
throw new BindingError()
|
|
356
|
+
}
|
|
357
|
+
if (pathBack?.fieldBackToParent === marker.parameters.field) {
|
|
358
|
+
continue
|
|
359
|
+
}
|
|
360
|
+
if (marker.parameters.isNonbearing) {
|
|
361
|
+
nonbearingFields.push({
|
|
362
|
+
type: 'hasMany',
|
|
363
|
+
marker,
|
|
364
|
+
fieldState,
|
|
365
|
+
})
|
|
366
|
+
continue
|
|
367
|
+
}
|
|
368
|
+
builder = this.registerCreateEntityListPart(processedPlaceholdersByEntity, fieldState, marker, builder)
|
|
404
369
|
break
|
|
405
370
|
}
|
|
406
|
-
default:
|
|
407
|
-
assertNever(
|
|
371
|
+
default: {
|
|
372
|
+
return assertNever(fieldState)
|
|
373
|
+
}
|
|
408
374
|
}
|
|
409
375
|
}
|
|
410
|
-
}
|
|
411
376
|
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
377
|
+
// if (
|
|
378
|
+
// currentState.blueprint.creationParameters.forceCreation &&
|
|
379
|
+
// (builder.data === undefined || isEmptyObject(builder.data))
|
|
380
|
+
// ) {
|
|
381
|
+
// builder = builder.set('_dummy_field_', true)
|
|
382
|
+
// }
|
|
383
|
+
|
|
384
|
+
if (
|
|
385
|
+
(builder.data !== undefined && !isEmptyObject(builder.data)) ||
|
|
386
|
+
!getEntityMarker(siblingState).fields.hasAtLeastOneBearingField
|
|
387
|
+
) {
|
|
388
|
+
for (const field of nonbearingFields) {
|
|
389
|
+
switch (field.type) {
|
|
390
|
+
case 'field': {
|
|
391
|
+
builder = this.registerCreateFieldPart(field.fieldState, field.marker, builder)
|
|
392
|
+
break
|
|
393
|
+
}
|
|
394
|
+
case 'hasOne': {
|
|
395
|
+
builder = this.registerCreateEntityPart(
|
|
396
|
+
processedPlaceholdersByEntity,
|
|
397
|
+
field.fieldState,
|
|
398
|
+
field.marker,
|
|
399
|
+
builder,
|
|
400
|
+
)
|
|
401
|
+
break
|
|
402
|
+
}
|
|
403
|
+
case 'hasMany': {
|
|
404
|
+
builder = this.registerCreateEntityListPart(
|
|
405
|
+
processedPlaceholdersByEntity,
|
|
406
|
+
field.fieldState,
|
|
407
|
+
field.marker,
|
|
408
|
+
builder,
|
|
409
|
+
)
|
|
410
|
+
break
|
|
411
|
+
}
|
|
412
|
+
default:
|
|
413
|
+
assertNever(field)
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
const setOnCreate = getEntityMarker(siblingState).parameters.setOnCreate
|
|
419
|
+
if (setOnCreate && builder.data !== undefined && !isEmptyObject(builder.data)) {
|
|
420
|
+
for (const key in setOnCreate) {
|
|
421
|
+
const field = setOnCreate[key]
|
|
422
|
+
|
|
423
|
+
if (
|
|
424
|
+
typeof field === 'string' ||
|
|
425
|
+
typeof field === 'number' ||
|
|
426
|
+
field === null ||
|
|
427
|
+
field instanceof GraphQlBuilder.GraphQlLiteral
|
|
428
|
+
) {
|
|
429
|
+
builder = builder.set(key, field)
|
|
430
|
+
} else {
|
|
431
|
+
builder = builder.one(key, builder => builder.connect(field))
|
|
432
|
+
}
|
|
426
433
|
}
|
|
427
434
|
}
|
|
428
435
|
}
|
|
@@ -19,7 +19,6 @@ import type {
|
|
|
19
19
|
} from '../treeParameters'
|
|
20
20
|
import { assertNever } from '../utils'
|
|
21
21
|
import type { AccessorErrorManager } from './AccessorErrorManager'
|
|
22
|
-
import type { Config } from './Config'
|
|
23
22
|
import type { EventManager } from './EventManager'
|
|
24
23
|
import { EntityOperations, FieldOperations, ListOperations } from './operations'
|
|
25
24
|
import { RealmKeyGenerator } from './RealmKeyGenerator'
|
|
@@ -44,27 +43,14 @@ export class StateInitializer {
|
|
|
44
43
|
private readonly listOperations: ListOperations
|
|
45
44
|
|
|
46
45
|
public constructor(
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
private readonly config: Config,
|
|
46
|
+
accessorErrorManager: AccessorErrorManager,
|
|
47
|
+
batchUpdatesOptions: BatchUpdatesOptions,
|
|
50
48
|
private readonly eventManager: EventManager,
|
|
51
49
|
private readonly treeStore: TreeStore,
|
|
52
50
|
) {
|
|
53
|
-
this.fieldOperations = new FieldOperations(
|
|
54
|
-
this.entityOperations = new EntityOperations(
|
|
55
|
-
|
|
56
|
-
this.batchUpdatesOptions,
|
|
57
|
-
this.eventManager,
|
|
58
|
-
this,
|
|
59
|
-
this.treeStore,
|
|
60
|
-
)
|
|
61
|
-
this.listOperations = new ListOperations(
|
|
62
|
-
this.accessorErrorManager,
|
|
63
|
-
this.batchUpdatesOptions,
|
|
64
|
-
this.eventManager,
|
|
65
|
-
this,
|
|
66
|
-
this.treeStore,
|
|
67
|
-
)
|
|
51
|
+
this.fieldOperations = new FieldOperations(accessorErrorManager, this.eventManager, this, this.treeStore)
|
|
52
|
+
this.entityOperations = new EntityOperations(accessorErrorManager, batchUpdatesOptions, this.eventManager, this, this.treeStore)
|
|
53
|
+
this.listOperations = new ListOperations(accessorErrorManager, batchUpdatesOptions, this.eventManager, this, this.treeStore)
|
|
68
54
|
}
|
|
69
55
|
|
|
70
56
|
public initializeSubTree(tree: EntitySubTreeMarker | EntityListSubTreeMarker): RootStateNode {
|
|
@@ -126,7 +112,7 @@ export class StateInitializer {
|
|
|
126
112
|
|
|
127
113
|
return stub
|
|
128
114
|
}
|
|
129
|
-
const realm = this.createEntityRealm(blueprint, realmKey, entity)
|
|
115
|
+
const realm = this.createEntityRealm(blueprint, realmKey, entity, copyFrom)
|
|
130
116
|
|
|
131
117
|
this.registerEntityRealm(realm)
|
|
132
118
|
|
|
@@ -139,7 +125,7 @@ export class StateInitializer {
|
|
|
139
125
|
if (!value || value.type !== 'field') {
|
|
140
126
|
throw new BindingError()
|
|
141
127
|
}
|
|
142
|
-
this.initializeFromFieldMarker(realm, field, value
|
|
128
|
+
this.initializeFromFieldMarker(realm, field, undefined, value)
|
|
143
129
|
} else if (field instanceof HasManyRelationMarker) {
|
|
144
130
|
// todo: get from copyFrom
|
|
145
131
|
this.initializeFromHasManyRelationMarker(realm, field, new Set())
|
|
@@ -168,7 +154,12 @@ export class StateInitializer {
|
|
|
168
154
|
return realm
|
|
169
155
|
}
|
|
170
156
|
|
|
171
|
-
private createEntityRealm(
|
|
157
|
+
private createEntityRealm(
|
|
158
|
+
blueprint: EntityRealmBlueprint,
|
|
159
|
+
realmKey: string,
|
|
160
|
+
entity: EntityState,
|
|
161
|
+
copyFrom?: EntityRealmState,
|
|
162
|
+
): EntityRealmState {
|
|
172
163
|
const entityRealm: EntityRealmState = {
|
|
173
164
|
type: 'entityRealm',
|
|
174
165
|
|
|
@@ -183,7 +174,7 @@ export class StateInitializer {
|
|
|
183
174
|
eventListeners: this.initializeEntityEventListenerStore(blueprint),
|
|
184
175
|
fieldsWithPendingConnectionUpdates: undefined,
|
|
185
176
|
plannedHasOneDeletions: undefined,
|
|
186
|
-
unpersistedChangesCount: 0,
|
|
177
|
+
unpersistedChangesCount: copyFrom?.unpersistedChangesCount ?? 0,
|
|
187
178
|
getAccessor: () => {
|
|
188
179
|
if (entityRealm.accessor === undefined) {
|
|
189
180
|
const entity = entityRealm.entity
|
|
@@ -359,8 +350,9 @@ export class StateInitializer {
|
|
|
359
350
|
placeholderName: FieldName,
|
|
360
351
|
fieldMarker: FieldMarker,
|
|
361
352
|
persistedValue: Scalar | undefined,
|
|
353
|
+
copyFromState: FieldState | undefined,
|
|
362
354
|
): FieldState {
|
|
363
|
-
const resolvedFieldValue = persistedValue ?? fieldMarker.defaultValue ?? null
|
|
355
|
+
const resolvedFieldValue = copyFromState ? copyFromState.value : (persistedValue ?? fieldMarker.defaultValue ?? null)
|
|
364
356
|
|
|
365
357
|
const fieldState: FieldState = {
|
|
366
358
|
type: 'field',
|
|
@@ -373,7 +365,7 @@ export class StateInitializer {
|
|
|
373
365
|
eventListeners: this.initializeFieldEventListenerStore(fieldMarker),
|
|
374
366
|
errors: undefined,
|
|
375
367
|
touchLog: undefined,
|
|
376
|
-
hasUnpersistedChanges: false,
|
|
368
|
+
hasUnpersistedChanges: copyFromState?.hasUnpersistedChanges ?? false,
|
|
377
369
|
|
|
378
370
|
getAccessor: () => {
|
|
379
371
|
if (fieldState.accessor === undefined) {
|
|
@@ -409,15 +401,16 @@ export class StateInitializer {
|
|
|
409
401
|
private initializeFromFieldMarker(
|
|
410
402
|
parent: EntityRealmState,
|
|
411
403
|
field: FieldMarker,
|
|
412
|
-
|
|
404
|
+
persistedValue: EntityFieldPersistedData | undefined,
|
|
405
|
+
copyFromState?: FieldState,
|
|
413
406
|
) {
|
|
414
|
-
if (
|
|
407
|
+
if (persistedValue instanceof Set) {
|
|
415
408
|
throw new BindingError(
|
|
416
409
|
`Received a collection of referenced entities where a single '${field.fieldName}' field was expected. ` +
|
|
417
410
|
`Perhaps you wanted to use a <Repeater />?`,
|
|
418
411
|
)
|
|
419
412
|
}
|
|
420
|
-
if (
|
|
413
|
+
if (persistedValue instanceof ServerId) {
|
|
421
414
|
throw new BindingError(
|
|
422
415
|
`Received a referenced entity where a single '${field.fieldName}' field was expected. ` +
|
|
423
416
|
`Perhaps you wanted to use <HasOne />?`,
|
|
@@ -425,7 +418,7 @@ export class StateInitializer {
|
|
|
425
418
|
}
|
|
426
419
|
parent.children.set(
|
|
427
420
|
field.placeholderName,
|
|
428
|
-
this.initializeFieldState(parent, field.placeholderName, field,
|
|
421
|
+
this.initializeFieldState(parent, field.placeholderName, field, persistedValue, copyFromState),
|
|
429
422
|
)
|
|
430
423
|
}
|
|
431
424
|
|
|
@@ -127,16 +127,11 @@ export class EntityOperations {
|
|
|
127
127
|
}
|
|
128
128
|
|
|
129
129
|
const persistedId = persistedData?.get(targetHasOneMarker.placeholderName)
|
|
130
|
-
if (persistedId instanceof ServerId) {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
changesDelta++ // We're changing it from the persisted id.
|
|
135
|
-
}
|
|
130
|
+
if (persistedId instanceof ServerId && persistedId.value === stateToConnect.entity.id.value) {
|
|
131
|
+
changesDelta-- // It was removed from the list but now we're adding it back.
|
|
132
|
+
} else if (persistedId instanceof ServerId && persistedId.value === previouslyConnectedState.entity.id.value) {
|
|
133
|
+
changesDelta++ // We're changing it from the persisted id.
|
|
136
134
|
} else if (!previouslyConnectedState.entity.id.existsOnServer) {
|
|
137
|
-
// This assumes the invariant enforced above that we cannot connect unpersisted entities.
|
|
138
|
-
// Hence the previouslyConnectedState still refers to the entity created initially.
|
|
139
|
-
|
|
140
135
|
if (
|
|
141
136
|
persistedId === null || // We're updating.
|
|
142
137
|
(persistedId === undefined && // We're creating.
|
|
@@ -145,6 +140,9 @@ export class EntityOperations {
|
|
|
145
140
|
changesDelta++
|
|
146
141
|
}
|
|
147
142
|
}
|
|
143
|
+
if (stateToConnect.type === 'entityRealm') {
|
|
144
|
+
changesDelta += stateToConnect.unpersistedChangesCount
|
|
145
|
+
}
|
|
148
146
|
|
|
149
147
|
// TODO do something about the existing state…
|
|
150
148
|
|
|
@@ -152,6 +150,7 @@ export class EntityOperations {
|
|
|
152
150
|
stateToConnect.entity.id,
|
|
153
151
|
stateToConnect.entity.entityName,
|
|
154
152
|
previouslyConnectedState.blueprint,
|
|
153
|
+
stateToConnect,
|
|
155
154
|
)
|
|
156
155
|
|
|
157
156
|
this.eventManager.registerUpdatedConnection(state, targetHasOneMarker.placeholderName)
|
|
@@ -106,7 +106,8 @@ export class ListOperations {
|
|
|
106
106
|
if (persistedEntityIds.has(idToConnect.value)) {
|
|
107
107
|
// It was removed from the list but now we're adding it back.
|
|
108
108
|
changesDelta--
|
|
109
|
-
} else {
|
|
109
|
+
} else if (idToConnect.existsOnServer) {
|
|
110
|
+
// ignoring unpersisted entities
|
|
110
111
|
changesDelta++
|
|
111
112
|
}
|
|
112
113
|
if (stateToConnect.type === 'entityRealm') {
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { EntityName } from '../../treeParameters'
|
|
2
2
|
import type { SchemaColumn } from './SchemaColumn'
|
|
3
3
|
import type { SchemaRelation } from './SchemaRelation'
|
|
4
|
-
import type { SchemaUniqueConstraint } from './SchemaUniqueConstraint'
|
|
5
4
|
|
|
6
5
|
export type RawSchemaFields = Array<SchemaRelation | SchemaColumn>
|
|
7
6
|
|
|
@@ -9,5 +8,5 @@ export interface RawSchemaEntity {
|
|
|
9
8
|
name: EntityName
|
|
10
9
|
customPrimaryAllowed: boolean
|
|
11
10
|
fields: RawSchemaFields
|
|
12
|
-
unique:
|
|
11
|
+
unique: { fields: string[] }[]
|
|
13
12
|
}
|