ballerina-core 1.0.44 → 1.0.46

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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "ballerina-core",
3
3
  "author": "Dr. Giuseppe Maggiore",
4
4
  "private": false,
5
- "version": "1.0.44",
5
+ "version": "1.0.46",
6
6
  "main": "main.ts",
7
7
  "dependencies": {
8
8
  "immutable": "^5.0.0-beta.5",
@@ -18,16 +18,16 @@ export const HandleApiResponse = <
18
18
  return AsyncState.Operations.isLoading(asyncState)
19
19
  ? CheckerCo.Do(() => {})
20
20
  : CheckerCo.Seq([
21
- CheckerCo.Do(() => {
22
- return AsyncState.Operations.status(asyncState) === 'error'
23
- ? handlers.handleError?.(
24
- asyncState.kind === 'error' ? asyncState.error : undefined
25
- )
26
- : handlers.handleSuccess?.(_)
21
+ CheckerCo.Do(() => {
22
+ return AsyncState.Operations.status(asyncState) === "error"
23
+ ? handlers.handleError?.(
24
+ asyncState.kind === "error" ? asyncState.error : undefined
25
+ )
26
+ : handlers.handleSuccess?.(_);
27
27
  }),
28
28
  CheckerCo.SetState((_) => ({
29
29
  ..._,
30
- ...ApiResponseChecker.Updaters.toChecked()(_),
30
+ ...ApiResponseChecker.Updaters().toChecked()(_),
31
31
  })),
32
32
  ]);
33
33
  });
@@ -11,20 +11,16 @@ export const ApiResponseChecker = {
11
11
  Default: (_?: boolean): ApiResponseChecker => ({
12
12
  apiResponseChecked: _ ?? false,
13
13
  }),
14
- Updaters: {
15
- toChecked:
16
- <CheckedState extends ApiResponseChecker>(): BasicUpdater<CheckedState> =>
17
- (_) => ({
18
- ..._,
19
- apiResponseChecked: true,
20
- }),
21
- toUnchecked:
22
- <CheckedState extends ApiResponseChecker>(): BasicUpdater<CheckedState> =>
23
- (_) => ({
24
- ..._,
25
- apiResponseChecked: false,
26
- }),
27
- },
14
+ Updaters: <CheckedState extends ApiResponseChecker>() => ({
15
+ toChecked: (): BasicUpdater<CheckedState> => (_) => ({
16
+ ..._,
17
+ apiResponseChecked: true,
18
+ }),
19
+ toUnchecked: (): BasicUpdater<CheckedState> => (_) => ({
20
+ ..._,
21
+ apiResponseChecked: false,
22
+ }),
23
+ }),
28
24
  Operations: {
29
25
  checked: (_: ApiResponseChecker) => _.apiResponseChecked,
30
26
  },
@@ -1,9 +1,12 @@
1
- import { ApiErrors, AsyncState, Debounce, Debounced, CreateFormForeignMutationsExpected, CreateFormState, Synchronize, Synchronized, Unit, SimpleCallback, unit, replaceWith } from "../../../../../../../main"
1
+ import { ApiErrors, AsyncState, Debounce, Debounced, CreateFormForeignMutationsExpected, CreateFormState, Synchronize, Synchronized, Unit, HandleApiResponse, ApiResponseChecker } from "../../../../../../../main"
2
2
  import { CoTypedFactory } from "../../../../../../coroutines/builder"
3
3
  import { CreateFormContext, CreateFormWritableState } from "../state"
4
4
 
5
5
  export const createFormRunner = <E, FS>() => {
6
- const Co = CoTypedFactory<CreateFormContext<E, FS> & { onSubmitted:SimpleCallback<E> }, CreateFormWritableState<E, FS>>()
6
+ const Co = CoTypedFactory<
7
+ CreateFormContext<E, FS> & CreateFormForeignMutationsExpected<E, FS>,
8
+ CreateFormWritableState<E, FS>
9
+ >()
7
10
 
8
11
  const init =
9
12
  Co.GetState().then(current =>
@@ -16,8 +19,11 @@ export const createFormRunner = <E, FS>() => {
16
19
 
17
20
  const synchronize =
18
21
  Co.Repeat(
19
- Co.Seq([
20
22
  Co.GetState().then(current =>
23
+ Co.Seq([
24
+ Co.SetState(
25
+ CreateFormState<E, FS>().Updaters.Template.toUnchecked()
26
+ ),
21
27
  Debounce<Synchronized<Unit, Synchronized<E, ApiErrors>>, CreateFormContext<E, FS>>(
22
28
  Synchronize<E, ApiErrors>(e => current.api.create([e, current.formState]), _ => "transient failure", 5, 50)
23
29
  .embed(
@@ -28,21 +34,18 @@ export const createFormRunner = <E, FS>() => {
28
34
  ).embed(
29
35
  _ => ({ ..._, ..._.entity }),
30
36
  _ => CreateFormState<E, FS>().Updaters.Core.entity(_)
31
- )
37
+ ),
38
+ HandleApiResponse<
39
+ CreateFormWritableState<E, FS>,
40
+ CreateFormContext<E, FS>,
41
+ Synchronized<E, ApiErrors> | ApiErrors
42
+ >((_) => AsyncState.Operations.hasValue(_.entity.sync) ? _.entity.sync.value.sync : _.entity.sync, {
43
+ handleSuccess: current.apiHandlers?.success,
44
+ handleError: current.apiHandlers?.error,
45
+ }),
46
+ ]
32
47
  ),
33
- Co.GetState().then(current =>
34
- AsyncState.Operations.hasValue(current.entity.sync) && AsyncState.Operations.hasValue(current.entity.sync.value.sync) && current.notifySubmitAfterSync ?
35
- Co.Seq([
36
- Co.Do(() => {
37
- if (AsyncState.Operations.hasValue(current.entity.sync) && AsyncState.Operations.hasValue(current.entity.sync.value.sync))
38
- current.onSubmitted(current.entity.sync.value)
39
- }),
40
- Co.SetState(CreateFormState<E,FS>().Updaters.Core.notifySubmitAfterSync(replaceWith(false)))
41
- ])
42
- : Co.Return(unit)
43
- )
44
- ])
45
- )
48
+ ))
46
49
 
47
50
  return Co.Template<CreateFormForeignMutationsExpected<E, FS>>(
48
51
  init, {
@@ -53,7 +56,10 @@ export const createFormRunner = <E, FS>() => {
53
56
  Co.Template<CreateFormForeignMutationsExpected<E, FS>>(
54
57
  synchronize, {
55
58
  interval: 15,
56
- runFilter: props => Debounced.Operations.shouldCoroutineRun(props.context.entity) || props.context.notifySubmitAfterSync
59
+ runFilter: props =>
60
+ props.context.entity.sync.kind === "loaded" &&
61
+ (Debounced.Operations.shouldCoroutineRun(props.context.entity) ||
62
+ !ApiResponseChecker.Operations.checked(props.context))
57
63
  }
58
64
  )
59
65
  ])
@@ -1,4 +1,4 @@
1
- import { ApiErrors, AsyncState, BasicUpdater, Debounced, ForeignMutationsInput, id, replaceWith, SimpleCallback, simpleUpdater, Synchronized, Template, unit, Unit, Updater, Value } from "../../../../../../main"
1
+ import { ApiErrors, ApiResponseChecker, AsyncState, BasicUpdater, Debounced, ForeignMutationsInput, FormRefApiHandlers, id, replaceWith, SimpleCallback, simpleUpdater, Synchronized, Template, unit, Unit, Updater, Value } from "../../../../../../main"
2
2
  import { BasicFun } from "../../../../../fun/state"
3
3
 
4
4
  export type CreateFormContext<E,FS> = {
@@ -14,8 +14,7 @@ export type CreateFormState<E,FS> = {
14
14
  // first sync is GET (returns E), second is UPDATE (accepts E)
15
15
  entity:Debounced<Synchronized<Unit, Synchronized<E, ApiErrors>>>
16
16
  formState:FS,
17
- notifySubmitAfterSync:boolean
18
- }
17
+ } & ApiResponseChecker;
19
18
 
20
19
  export const CreateFormState = <E,FS>() => ({
21
20
  Default:(initialFormState:FS) : CreateFormState<E,FS> => ({
@@ -23,15 +22,15 @@ export const CreateFormState = <E,FS>() => ({
23
22
  Synchronized.Default(unit)
24
23
  ),
25
24
  formState:initialFormState,
26
- notifySubmitAfterSync:false
25
+ ...ApiResponseChecker.Default(true),
27
26
  }),
28
27
  Updaters:{
29
- Core:{
30
- ...simpleUpdater<CreateFormState<E,FS>>()("notifySubmitAfterSync"),
28
+ Core:{
31
29
  ...simpleUpdater<CreateFormState<E,FS>>()("entity"),
32
30
  ...simpleUpdater<CreateFormState<E,FS>>()("formState"),
33
31
  },
34
32
  Template:{
33
+ ...ApiResponseChecker.Updaters<CreateFormState<E, FS>>(),
35
34
  entity:(_:BasicUpdater<E>) : Updater<CreateFormState<E,FS>> =>
36
35
  CreateFormState<E,FS>().Updaters.Core.entity(
37
36
  Debounced.Updaters.Core.value(
@@ -55,8 +54,6 @@ export const CreateFormState = <E,FS>() => ({
55
54
  )
56
55
  )
57
56
  )
58
- ).then(
59
- CreateFormState<E,FS>().Updaters.Core.notifySubmitAfterSync(replaceWith(true))
60
57
  )
61
58
 
62
59
  }
@@ -68,4 +65,9 @@ export const CreateFormState = <E,FS>() => ({
68
65
 
69
66
  export type CreateFormWritableState<E,FS> = CreateFormState<E,FS>
70
67
  export type CreateFormForeignMutationsExposed<E,FS> = ReturnType<ReturnType<typeof CreateFormState<E,FS>>["ForeignMutations"]>
71
- export type CreateFormForeignMutationsExpected<E,FS> = { onSubmitted:SimpleCallback<E> }
68
+ export type CreateFormForeignMutationsExpected<E,FS> = {
69
+ apiHandlers?: {
70
+ success?: (_: CreateFormWritableState<E, FS> & CreateFormContext<E, FS> | undefined) => void;
71
+ error?: <ApiErrors>(_: ApiErrors | undefined) => void;
72
+ }
73
+ }
@@ -60,6 +60,9 @@ export const CreateFormTemplate = <E, FS>(): CreateFormTemplate<E, FS> =>
60
60
  }).any([
61
61
  createFormRunner<E, FS>().mapContextFromProps(props => ({
62
62
  ...props.context,
63
- onSubmitted:props.foreignMutations.onSubmitted
63
+ apiHandlers: {
64
+ success: props.foreignMutations.apiHandlers?.success,
65
+ error: props.foreignMutations.apiHandlers?.error
66
+ }
64
67
  }))
65
68
  ])
@@ -35,8 +35,7 @@ export const EditFormState = <E,FS>() => ({
35
35
  ...simpleUpdater<EditFormState<E,FS>>()("formState"),
36
36
  },
37
37
  Template:{
38
- toChecked: () => ApiResponseChecker.Updaters.toChecked<EditFormState<E, FS>>(),
39
- toUnchecked: () => ApiResponseChecker.Updaters.toUnchecked<EditFormState<E, FS>>(),
38
+ ...ApiResponseChecker.Updaters<EditFormState<E, FS>>(),
40
39
  entity:(_:BasicUpdater<E>) : Updater<EditFormState<E,FS>> =>
41
40
  EditFormState<E,FS>().Updaters.Core.entity(
42
41
  Synchronized.Updaters.sync(
@@ -2,23 +2,25 @@ import { BasicFun, Guid, Mapping, simpleUpdater, Sum, Unit } from "../../../../m
2
2
  import { FormParsingResult, FormsParserState } from "../parser/state"
3
3
  import { OnChange } from "../singleton/state"
4
4
 
5
+ export type FormRefApiHandlers<Arg> = {
6
+ success?: (_: Arg) => void,
7
+ error?: (_: Arg) => void,
8
+ }
9
+
5
10
  export type FormRef = {
6
11
  formName:string
7
12
  } & ({
8
13
  kind:"edit",
9
14
  submitButtonWrapper:any
10
15
  entityId:Guid,
11
- apiHandlers?: {
12
- success?: (_: any) => void,
13
- error?: (_: any) => void,
14
- }
16
+ apiHandlers?: FormRefApiHandlers<any>
15
17
  } | {
16
18
  kind:"map",
17
19
  onChange:OnChange<any>
18
20
  value:any
19
21
  } | {
20
22
  kind:"create",
21
- onSubmitted: (_: any) => void,
23
+ apiHandlers?: FormRefApiHandlers<any>,
22
24
  submitButtonWrapper:any
23
25
  })
24
26
 
@@ -49,17 +49,19 @@ export const FormRunnerTemplate =
49
49
  if (props.context.formRef.kind == "map")
50
50
  props.context.formRef.onChange(_, _path)
51
51
  },
52
- onSubmitted: (_: any) => {
53
- if (props.context.formRef.kind == "create")
54
- props.context.formRef.onSubmitted(_)
55
- },
56
52
  apiHandlers: {
57
53
  success: (_: any) => {
58
- if (props.context.formRef.kind === 'edit')
54
+ if (
55
+ props.context.formRef.kind === 'edit' ||
56
+ props.context.formRef.kind === 'create'
57
+ )
59
58
  props.context.formRef.apiHandlers?.success?.(_)
60
59
  },
61
60
  error: (_: any) => {
62
- if (props.context.formRef.kind === 'edit')
61
+ if (
62
+ props.context.formRef.kind === 'edit' ||
63
+ props.context.formRef.kind === 'create'
64
+ )
63
65
  props.context.formRef.apiHandlers?.error?.(_)
64
66
  }
65
67
  }
@@ -32,7 +32,7 @@ export type BuiltInApiConverters = {
32
32
  "maybeBoolean": ApiConverter<boolean | undefined>
33
33
  "base64File": ApiConverter<string>
34
34
  "secret": ApiConverter<string>,
35
- "Date": ApiConverter<Date>
35
+ "Date": ApiConverter<Maybe<Date>>
36
36
  "CollectionReference": ApiConverter<CollectionReference>
37
37
  "SingleSelection": ApiConverter<CollectionSelection<any>>
38
38
  "MultiSelection": ApiConverter<OrderedMap<string, any>>
@@ -17,7 +17,7 @@ export const DateForm = <Context extends FormLabel, ForeignMutationsExpected>(
17
17
  props.setState(DateFormState.Updaters.possiblyInvalidInput(Maybe.Updaters.value(replaceWith(_))))
18
18
  const newValue = _ == undefined ? _ : new Date(_)
19
19
  setTimeout(() => {
20
- props.foreignMutations.onChange(Maybe.Updaters.value(replaceWith(newValue)), List())
20
+ props.foreignMutations.onChange(replaceWith(newValue), List())
21
21
  }, 0)
22
22
  }
23
23
  }} />