@contember/binding 1.1.0-alpha.8 → 1.1.0-alpha.9

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.
Files changed (40) hide show
  1. package/dist/development/accessorTree/AccessorTreeStateContext.js +1 -3
  2. package/dist/development/accessorTree/AccessorTreeStateContext.js.map +1 -1
  3. package/dist/development/accessorTree/accessorTreeStateReducer.js +7 -1
  4. package/dist/development/accessorTree/accessorTreeStateReducer.js.map +1 -1
  5. package/dist/development/accessorTree/useAccessorTreeState.js +8 -1
  6. package/dist/development/accessorTree/useAccessorTreeState.js.map +1 -1
  7. package/dist/development/accessorTree/useDataBinding.js +18 -16
  8. package/dist/development/accessorTree/useDataBinding.js.map +1 -1
  9. package/dist/development/core/DataBinding.js +6 -6
  10. package/dist/development/core/DataBinding.js.map +1 -1
  11. package/dist/production/accessorTree/AccessorTreeStateContext.js +1 -3
  12. package/dist/production/accessorTree/AccessorTreeStateContext.js.map +1 -1
  13. package/dist/production/accessorTree/accessorTreeStateReducer.js +7 -1
  14. package/dist/production/accessorTree/accessorTreeStateReducer.js.map +1 -1
  15. package/dist/production/accessorTree/useAccessorTreeState.js +8 -1
  16. package/dist/production/accessorTree/useAccessorTreeState.js.map +1 -1
  17. package/dist/production/accessorTree/useDataBinding.js +18 -16
  18. package/dist/production/accessorTree/useDataBinding.js.map +1 -1
  19. package/dist/production/core/DataBinding.js +6 -6
  20. package/dist/production/core/DataBinding.js.map +1 -1
  21. package/dist/types/accessorTree/AccessorTreeState.d.ts +4 -0
  22. package/dist/types/accessorTree/AccessorTreeState.d.ts.map +1 -1
  23. package/dist/types/accessorTree/AccessorTreeStateAction.d.ts +4 -0
  24. package/dist/types/accessorTree/AccessorTreeStateAction.d.ts.map +1 -1
  25. package/dist/types/accessorTree/AccessorTreeStateContext.d.ts +1 -1
  26. package/dist/types/accessorTree/AccessorTreeStateContext.d.ts.map +1 -1
  27. package/dist/types/accessorTree/accessorTreeStateReducer.d.ts.map +1 -1
  28. package/dist/types/accessorTree/useAccessorTreeState.d.ts.map +1 -1
  29. package/dist/types/accessorTree/useDataBinding.d.ts.map +1 -1
  30. package/dist/types/core/DataBinding.d.ts +1 -1
  31. package/dist/types/core/DataBinding.d.ts.map +1 -1
  32. package/dist/types/tsconfig.tsbuildinfo +1 -1
  33. package/package.json +5 -5
  34. package/src/accessorTree/AccessorTreeState.ts +4 -0
  35. package/src/accessorTree/AccessorTreeStateAction.ts +4 -0
  36. package/src/accessorTree/AccessorTreeStateContext.ts +1 -3
  37. package/src/accessorTree/accessorTreeStateReducer.ts +7 -1
  38. package/src/accessorTree/useAccessorTreeState.ts +8 -1
  39. package/src/accessorTree/useDataBinding.ts +15 -15
  40. package/src/core/DataBinding.ts +9 -9
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@contember/binding",
3
3
  "license": "Apache-2.0",
4
- "version": "1.1.0-alpha.8",
4
+ "version": "1.1.0-alpha.9",
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
7
  "main": "./dist/production/index.js",
@@ -19,10 +19,10 @@
19
19
  ],
20
20
  "typings": "./dist/types/index.d.ts",
21
21
  "dependencies": {
22
- "@contember/client": "1.1.0-alpha.8",
23
- "@contember/react-client": "1.1.0-alpha.8",
24
- "@contember/react-multipass-rendering": "1.1.0-alpha.8",
25
- "@contember/react-utils": "1.1.0-alpha.8",
22
+ "@contember/client": "1.1.0-alpha.9",
23
+ "@contember/react-client": "1.1.0-alpha.9",
24
+ "@contember/react-multipass-rendering": "1.1.0-alpha.9",
25
+ "@contember/react-utils": "1.1.0-alpha.9",
26
26
  "chevrotain": "6.5.0",
27
27
  "fast-deep-equal": "^3.1.3",
28
28
  "js-levenshtein": "1.1.6",
@@ -1,16 +1,20 @@
1
1
  import type { TreeRootAccessor } from '../accessors'
2
2
  import type { RequestError } from './RequestError'
3
+ import { DataBinding } from '../core'
3
4
 
4
5
  export interface InitializingAccessorTreeState {
6
+ binding: DataBinding
5
7
  name: 'initializing'
6
8
  }
7
9
 
8
10
  export interface InitializedAccessorTreeState {
11
+ binding: DataBinding
9
12
  name: 'initialized'
10
13
  data: TreeRootAccessor
11
14
  }
12
15
 
13
16
  export interface ErrorAccessorTreeState {
17
+ binding: DataBinding
14
18
  name: 'error'
15
19
  error: RequestError
16
20
  }
@@ -1,15 +1,19 @@
1
1
  import type { TreeRootAccessor } from '../accessors'
2
2
  import type { RequestError } from './RequestError'
3
+ import { DataBinding } from '../core'
3
4
 
4
5
  export type AccessorTreeStateAction =
5
6
  | {
6
7
  type: 'setData'
7
8
  data: TreeRootAccessor
9
+ binding: DataBinding
8
10
  }
9
11
  | {
10
12
  type: 'failWithError'
11
13
  error: RequestError
14
+ binding: DataBinding
12
15
  }
13
16
  | {
14
17
  type: 'reset'
18
+ binding: DataBinding
15
19
  }
@@ -1,6 +1,4 @@
1
1
  import { createContext } from 'react'
2
2
  import { AccessorTreeState } from './AccessorTreeState'
3
3
 
4
- export const AccessorTreeStateContext = createContext<AccessorTreeState>({
5
- name: 'initializing',
6
- })
4
+ export const AccessorTreeStateContext = createContext<AccessorTreeState | undefined>(undefined)
@@ -7,20 +7,26 @@ export const accessorTreeStateReducer = (
7
7
  ): AccessorTreeState => {
8
8
  switch (action.type) {
9
9
  case 'setData':
10
- if (previousState.name === 'error') {
10
+ if (previousState.name === 'error' || previousState.binding !== action.binding) {
11
11
  return previousState
12
12
  }
13
13
  return {
14
+ binding: previousState.binding,
14
15
  name: 'initialized',
15
16
  data: action.data,
16
17
  }
17
18
  case 'failWithError':
19
+ if (previousState.binding !== action.binding) {
20
+ return previousState
21
+ }
18
22
  return {
23
+ binding: previousState.binding,
19
24
  name: 'error',
20
25
  error: action.error,
21
26
  }
22
27
  case 'reset':
23
28
  return {
29
+ binding: action.binding,
24
30
  name: 'initializing',
25
31
  }
26
32
  }
@@ -1,4 +1,11 @@
1
1
  import { useContext } from 'react'
2
2
  import { AccessorTreeStateContext } from './AccessorTreeStateContext'
3
+ import { BindingError } from '../BindingError'
3
4
 
4
- export const useAccessorTreeState = () => useContext(AccessorTreeStateContext)
5
+ export const useAccessorTreeState = () => {
6
+ const state = useContext(AccessorTreeStateContext)
7
+ if (!state) {
8
+ throw new BindingError()
9
+ }
10
+ return state
11
+ }
@@ -12,10 +12,6 @@ import type { AccessorTreeStateOptions } from './AccessorTreeStateOptions'
12
12
  import { accessorTreeStateReducer } from './accessorTreeStateReducer'
13
13
  import type { RequestError } from './RequestError'
14
14
 
15
- const initialState: AccessorTreeState = {
16
- name: 'initializing',
17
- }
18
-
19
15
  export const useDataBinding = ({
20
16
  nodeTree,
21
17
  refreshOnEnvironmentChange = true,
@@ -26,27 +22,28 @@ export const useDataBinding = ({
26
22
  const tenantClient = useTenantGraphQlClient()
27
23
  const environment = useEnvironment()
28
24
 
29
- const [state, dispatch] = useReducer(accessorTreeStateReducer, initialState)
30
25
 
31
26
  const isFirstRenderRef = useRef(true)
32
27
  const isMountedRef = useRef(true)
33
28
 
34
- const onUpdate = useCallback((accessor: TreeRootAccessor) => {
29
+ const onUpdate = useCallback((accessor: TreeRootAccessor, binding: DataBinding) => {
35
30
  if (!isMountedRef.current) {
36
31
  return
37
32
  }
38
33
  dispatch({
39
34
  type: 'setData',
40
35
  data: accessor,
36
+ binding,
41
37
  })
42
38
  }, [])
43
- const onError = useCallback((error: RequestError) => {
39
+ const onError = useCallback((error: RequestError, binding: DataBinding) => {
44
40
  if (!isMountedRef.current) {
45
41
  return
46
42
  }
47
43
  dispatch({
48
44
  type: 'failWithError',
49
45
  error,
46
+ binding,
50
47
  })
51
48
  }, [])
52
49
 
@@ -56,15 +53,18 @@ export const useDataBinding = ({
56
53
  if (!isMountedRef.current || !refreshOnPersist) {
57
54
  return
58
55
  }
59
- dispatch({ type: 'reset' })
60
- setDataBinding(create())
56
+ const binding = create()
57
+ dispatch({ type: 'reset', binding })
61
58
  })
62
59
  return create()
63
60
  },
64
61
  [contentClient, systemClient, tenantClient, environment, onUpdate, onError, refreshOnPersist],
65
62
  )
66
-
67
- const [dataBinding, setDataBinding] = useState(() => createDataBinding())
63
+ const [initialState] = useState(() => ({
64
+ binding: createDataBinding(),
65
+ name: 'initializing' as const,
66
+ }))
67
+ const [state, dispatch] = useReducer(accessorTreeStateReducer, initialState)
68
68
 
69
69
  useEffect(() => {
70
70
  if (state.name !== 'initializing') {
@@ -74,16 +74,16 @@ export const useDataBinding = ({
74
74
  // To make their lives a bit easier, we do this.
75
75
  return
76
76
  }
77
- dataBinding.extendTree(nodeTree)
78
- }, [nodeTree, dataBinding, state.name])
77
+ state.binding.extendTree(nodeTree)
78
+ }, [nodeTree, state.binding, state.name])
79
79
 
80
80
  useEffect(() => {
81
81
  if (isFirstRenderRef.current || !refreshOnEnvironmentChange) {
82
82
  return
83
83
  }
84
- dispatch({ type: 'reset' })
85
84
  // This essentially just reacts to new environments.
86
- setDataBinding(createDataBinding())
85
+ const binding = createDataBinding()
86
+ dispatch({ type: 'reset', binding })
87
87
  }, [createDataBinding, refreshOnEnvironmentChange])
88
88
 
89
89
  useEffect(() => {
@@ -63,9 +63,9 @@ export class DataBinding {
63
63
  private readonly systemApiClient: GraphQlClient,
64
64
  private readonly tenantApiClient: GraphQlClient,
65
65
  private readonly environment: Environment,
66
- private readonly onUpdate: (newData: TreeRootAccessor) => void,
67
- private readonly onError: (error: RequestError) => void,
68
- private readonly onPersistSuccess: (result: SuccessfulPersistResult) => void,
66
+ private readonly onUpdate: (newData: TreeRootAccessor, binding: DataBinding) => void,
67
+ private readonly onError: (error: RequestError, binding: DataBinding) => void,
68
+ private readonly onPersistSuccess: (result: SuccessfulPersistResult, binding: DataBinding) => void,
69
69
  ) {
70
70
  this.config = new Config()
71
71
  this.treeStore = new TreeStore()
@@ -169,7 +169,7 @@ export class DataBinding {
169
169
  type: 'gqlError',
170
170
  query,
171
171
  errors: mutationResponse.errors,
172
- })
172
+ }, this)
173
173
  }
174
174
  if (!mutationResponse.data?.transaction) {
175
175
  throw {
@@ -213,10 +213,10 @@ export class DataBinding {
213
213
  ...result,
214
214
  afterPersistError: e,
215
215
  }
216
- this.onPersistSuccess(resultWithError)
216
+ this.onPersistSuccess(resultWithError, this)
217
217
  return resultWithError
218
218
  }
219
- this.onPersistSuccess(result)
219
+ this.onPersistSuccess(result, this)
220
220
  return result
221
221
  } else {
222
222
  this.eventManager.syncTransaction(() => this.accessorErrorManager.replaceErrors(mutationData, operations))
@@ -250,7 +250,7 @@ export class DataBinding {
250
250
  }
251
251
 
252
252
  private resolvedOnUpdate = ({ isMutating }: UpdateMetadata) => {
253
- this.onUpdate(new TreeRootAccessor(this.dirtinessTracker.hasChanges(), isMutating, this.bindingOperations))
253
+ this.onUpdate(new TreeRootAccessor(this.dirtinessTracker.hasChanges(), isMutating, this.bindingOperations), this)
254
254
  }
255
255
 
256
256
  // This is currently useless but potentially future-compatible
@@ -370,7 +370,7 @@ export class DataBinding {
370
370
  if (metadata.name === 'AbortError') {
371
371
  return
372
372
  }
373
- this.onError(metadataToRequestError(metadata as GraphQlClientFailedRequestMetadata))
373
+ this.onError(metadataToRequestError(metadata as GraphQlClientFailedRequestMetadata), this)
374
374
  }
375
375
 
376
376
  if (queryResponse && queryResponse.errors !== undefined && queryResponse.errors.length > 0) {
@@ -378,7 +378,7 @@ export class DataBinding {
378
378
  type: 'gqlError',
379
379
  query: query ?? 'unknown query',
380
380
  errors: queryResponse.errors,
381
- })
381
+ }, this)
382
382
  }
383
383
 
384
384
  return queryResponse