@cratis/arc 24.7.0 → 24.8.1

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 (121) hide show
  1. package/availability/Availability.ts +94 -19
  2. package/availability/AvailabilityReason.ts +7 -0
  3. package/availability/AvailableParts.ts +4 -0
  4. package/availability/ObserveAvailableParts.ts +22 -2
  5. package/availability/PartAvailability.ts +43 -0
  6. package/availability/UnavailablePart.ts +3 -0
  7. package/availability/askAvailability.ts +66 -0
  8. package/availability/for_Availability/when_asking_whether_a_part_is_available/and_the_proxy_carries_its_identity.ts +24 -0
  9. package/availability/for_Availability/when_asking_whether_a_part_is_available/and_the_server_lists_it_as_suspended.ts +20 -0
  10. package/availability/for_askAvailability/when_answering_for_a_part.ts +15 -0
  11. package/availability/index.ts +3 -0
  12. package/availability/isSuspended.ts +14 -0
  13. package/commands/CommandResult.ts +21 -0
  14. package/dist/cjs/availability/Availability.d.ts +18 -11
  15. package/dist/cjs/availability/Availability.d.ts.map +1 -1
  16. package/dist/cjs/availability/Availability.js +64 -12
  17. package/dist/cjs/availability/Availability.js.map +1 -1
  18. package/dist/cjs/availability/AvailabilityReason.d.ts +1 -0
  19. package/dist/cjs/availability/AvailabilityReason.d.ts.map +1 -1
  20. package/dist/cjs/availability/AvailabilityReason.js +6 -0
  21. package/dist/cjs/availability/AvailabilityReason.js.map +1 -1
  22. package/dist/cjs/availability/AvailableParts.d.ts +1 -0
  23. package/dist/cjs/availability/AvailableParts.d.ts.map +1 -1
  24. package/dist/cjs/availability/AvailableParts.js +5 -0
  25. package/dist/cjs/availability/AvailableParts.js.map +1 -1
  26. package/dist/cjs/availability/ObserveAvailableParts.d.ts +7 -1
  27. package/dist/cjs/availability/ObserveAvailableParts.d.ts.map +1 -1
  28. package/dist/cjs/availability/ObserveAvailableParts.js +10 -1
  29. package/dist/cjs/availability/ObserveAvailableParts.js.map +1 -1
  30. package/dist/cjs/availability/PartAvailability.d.ts +12 -0
  31. package/dist/cjs/availability/PartAvailability.d.ts.map +1 -0
  32. package/dist/cjs/availability/PartAvailability.js +41 -0
  33. package/dist/cjs/availability/PartAvailability.js.map +1 -0
  34. package/dist/cjs/availability/UnavailablePart.d.ts +1 -0
  35. package/dist/cjs/availability/UnavailablePart.d.ts.map +1 -1
  36. package/dist/cjs/availability/askAvailability.d.ts +6 -0
  37. package/dist/cjs/availability/askAvailability.d.ts.map +1 -0
  38. package/dist/cjs/availability/askAvailability.js +65 -0
  39. package/dist/cjs/availability/askAvailability.js.map +1 -0
  40. package/dist/cjs/availability/index.d.ts +3 -0
  41. package/dist/cjs/availability/index.d.ts.map +1 -1
  42. package/dist/cjs/availability/index.js +9 -0
  43. package/dist/cjs/availability/index.js.map +1 -1
  44. package/dist/cjs/availability/isSuspended.d.ts +5 -0
  45. package/dist/cjs/availability/isSuspended.d.ts.map +1 -0
  46. package/dist/cjs/availability/isSuspended.js +17 -0
  47. package/dist/cjs/availability/isSuspended.js.map +1 -0
  48. package/dist/cjs/commands/CommandResult.d.ts +2 -0
  49. package/dist/cjs/commands/CommandResult.d.ts.map +1 -1
  50. package/dist/cjs/commands/CommandResult.js +17 -0
  51. package/dist/cjs/commands/CommandResult.js.map +1 -1
  52. package/dist/cjs/reflection/PartIdentity.d.ts +8 -0
  53. package/dist/cjs/reflection/PartIdentity.d.ts.map +1 -0
  54. package/dist/cjs/reflection/PartIdentity.js +31 -0
  55. package/dist/cjs/reflection/PartIdentity.js.map +1 -0
  56. package/dist/cjs/reflection/index.d.ts +1 -0
  57. package/dist/cjs/reflection/index.d.ts.map +1 -1
  58. package/dist/cjs/reflection/index.js +2 -0
  59. package/dist/cjs/reflection/index.js.map +1 -1
  60. package/dist/esm/availability/Availability.d.ts +18 -11
  61. package/dist/esm/availability/Availability.d.ts.map +1 -1
  62. package/dist/esm/availability/Availability.js +64 -13
  63. package/dist/esm/availability/Availability.js.map +1 -1
  64. package/dist/esm/availability/AvailabilityReason.d.ts +1 -0
  65. package/dist/esm/availability/AvailabilityReason.d.ts.map +1 -1
  66. package/dist/esm/availability/AvailabilityReason.js +6 -1
  67. package/dist/esm/availability/AvailabilityReason.js.map +1 -1
  68. package/dist/esm/availability/AvailableParts.d.ts +1 -0
  69. package/dist/esm/availability/AvailableParts.d.ts.map +1 -1
  70. package/dist/esm/availability/AvailableParts.js +5 -0
  71. package/dist/esm/availability/AvailableParts.js.map +1 -1
  72. package/dist/esm/availability/ObserveAvailableParts.d.ts +7 -1
  73. package/dist/esm/availability/ObserveAvailableParts.d.ts.map +1 -1
  74. package/dist/esm/availability/ObserveAvailableParts.js +10 -1
  75. package/dist/esm/availability/ObserveAvailableParts.js.map +1 -1
  76. package/dist/esm/availability/PartAvailability.d.ts +12 -0
  77. package/dist/esm/availability/PartAvailability.d.ts.map +1 -0
  78. package/dist/esm/availability/PartAvailability.js +39 -0
  79. package/dist/esm/availability/PartAvailability.js.map +1 -0
  80. package/dist/esm/availability/UnavailablePart.d.ts +1 -0
  81. package/dist/esm/availability/UnavailablePart.d.ts.map +1 -1
  82. package/dist/esm/availability/askAvailability.d.ts +6 -0
  83. package/dist/esm/availability/askAvailability.d.ts.map +1 -0
  84. package/dist/esm/availability/askAvailability.js +62 -0
  85. package/dist/esm/availability/askAvailability.js.map +1 -0
  86. package/dist/esm/availability/for_Availability/when_asking_whether_a_part_is_available/and_the_proxy_carries_its_identity.d.ts +2 -0
  87. package/dist/esm/availability/for_Availability/when_asking_whether_a_part_is_available/and_the_proxy_carries_its_identity.d.ts.map +1 -0
  88. package/dist/esm/availability/for_Availability/when_asking_whether_a_part_is_available/and_the_proxy_carries_its_identity.js +20 -0
  89. package/dist/esm/availability/for_Availability/when_asking_whether_a_part_is_available/and_the_proxy_carries_its_identity.js.map +1 -0
  90. package/dist/esm/availability/for_Availability/when_asking_whether_a_part_is_available/and_the_server_lists_it_as_suspended.d.ts +2 -0
  91. package/dist/esm/availability/for_Availability/when_asking_whether_a_part_is_available/and_the_server_lists_it_as_suspended.d.ts.map +1 -0
  92. package/dist/esm/availability/for_Availability/when_asking_whether_a_part_is_available/and_the_server_lists_it_as_suspended.js +16 -0
  93. package/dist/esm/availability/for_Availability/when_asking_whether_a_part_is_available/and_the_server_lists_it_as_suspended.js.map +1 -0
  94. package/dist/esm/availability/for_askAvailability/when_answering_for_a_part.d.ts +2 -0
  95. package/dist/esm/availability/for_askAvailability/when_answering_for_a_part.d.ts.map +1 -0
  96. package/dist/esm/availability/for_askAvailability/when_answering_for_a_part.js +11 -0
  97. package/dist/esm/availability/for_askAvailability/when_answering_for_a_part.js.map +1 -0
  98. package/dist/esm/availability/index.d.ts +3 -0
  99. package/dist/esm/availability/index.d.ts.map +1 -1
  100. package/dist/esm/availability/index.js +5 -2
  101. package/dist/esm/availability/index.js.map +1 -1
  102. package/dist/esm/availability/isSuspended.d.ts +5 -0
  103. package/dist/esm/availability/isSuspended.d.ts.map +1 -0
  104. package/dist/esm/availability/isSuspended.js +15 -0
  105. package/dist/esm/availability/isSuspended.js.map +1 -0
  106. package/dist/esm/commands/CommandResult.d.ts +2 -0
  107. package/dist/esm/commands/CommandResult.d.ts.map +1 -1
  108. package/dist/esm/commands/CommandResult.js +17 -0
  109. package/dist/esm/commands/CommandResult.js.map +1 -1
  110. package/dist/esm/reflection/PartIdentity.d.ts +8 -0
  111. package/dist/esm/reflection/PartIdentity.d.ts.map +1 -0
  112. package/dist/esm/reflection/PartIdentity.js +29 -0
  113. package/dist/esm/reflection/PartIdentity.js.map +1 -0
  114. package/dist/esm/reflection/index.d.ts +1 -0
  115. package/dist/esm/reflection/index.d.ts.map +1 -1
  116. package/dist/esm/reflection/index.js +1 -0
  117. package/dist/esm/reflection/index.js.map +1 -1
  118. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  119. package/package.json +1 -1
  120. package/reflection/PartIdentity.ts +21 -0
  121. package/reflection/index.ts +1 -0
@@ -2,21 +2,49 @@
2
2
  // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
3
 
4
4
  import { UnavailablePart } from './UnavailablePart.js';
5
+ import { PartAvailability } from './PartAvailability.js';
6
+
7
+ interface Location {
8
+ readonly module: string;
9
+ readonly feature: string;
10
+ readonly slice: string;
11
+ }
12
+
13
+ interface Identity extends Location {
14
+ readonly name: string;
15
+ }
5
16
 
6
17
  /**
7
18
  * Something a client can ask about: the full name of a command or read model on the server, or a generated proxy
8
- * (class or instance) that carries the slice it belongs to.
19
+ * (class or instance) that carries the part it is and the slice it belongs to.
9
20
  */
10
21
  export type AvailabilityPart =
11
22
  | string
12
- | { readonly sliceLocation?: { readonly module: string; readonly feature: string; readonly slice: string } }
13
- | { readonly constructor: { readonly sliceLocation?: { readonly module: string; readonly feature: string; readonly slice: string } } };
23
+ | Identity
24
+ | { readonly part?: Identity; readonly sliceLocation?: Location }
25
+ | { readonly constructor: { readonly part?: Identity; readonly sliceLocation?: Location } };
26
+
27
+ /**
28
+ * Gets the full name of a part, when it is known.
29
+ * @param part The full name, or a generated proxy or part identity.
30
+ * @returns The name the server knows the part by; undefined when the proxy does not carry one.
31
+ */
32
+ export function partNameOf(part: AvailabilityPart): string | undefined {
33
+ if (typeof part === 'string') {
34
+ return part;
35
+ }
36
+
37
+ return identityOf(part)?.name;
38
+ }
14
39
 
15
40
  /**
16
41
  * Represents what is available to the current context, from the last decision the server reported.
17
42
  *
18
43
  * Everything the server does not list is available. Until the first decision has arrived nothing is known, and
19
44
  * nothing is reported as available, so a client never shows a part it has not been told it may show.
45
+ *
46
+ * This answers for the context alone, such as the tenant and the user. A gate that depends on the data a command or
47
+ * query is about, such as one vessel, is asked with `askAvailability` (or `useIsAvailable` in React), which sends the data.
20
48
  */
21
49
  export class Availability {
22
50
  /** An availability that has not heard from the server yet. */
@@ -35,25 +63,26 @@ export class Availability {
35
63
 
36
64
  /**
37
65
  * Determines whether a part is available.
38
- * @param part The full name of a command or read model, or a generated proxy. A proxy is matched by the slice it
39
- * belongs to, so every part of a slice is hidden together, which is how navigation and screens are decided.
66
+ * @param part The full name of a command or read model, or a generated proxy. A proxy is matched by the part it is
67
+ * and by the slice it belongs to, so every part of a slice is hidden together, which is how navigation and screens are decided.
40
68
  * @returns True when the part is available; false when it is not, or when nothing is known yet.
41
69
  */
42
70
  isAvailable(part: AvailabilityPart): boolean {
43
- if (!this.isKnown) {
44
- return false;
45
- }
46
-
47
- if (typeof part === 'string') {
48
- return !this.unavailable.some(_ => _.name === part);
49
- }
71
+ return this.availabilityOf(part).isAvailable;
72
+ }
50
73
 
51
- const location = sliceLocationOf(part);
52
- if (location === undefined || (location.module === '' && location.feature === '' && location.slice === '')) {
53
- return true;
74
+ /**
75
+ * Gets whether, and if not why, a part is available.
76
+ * @param part The full name of a command or read model, or a generated proxy.
77
+ * @returns The {@link PartAvailability}; `NotAvailable` is told apart from `Suspended` by its reason.
78
+ */
79
+ availabilityOf(part: AvailabilityPart): PartAvailability {
80
+ if (!this.isKnown) {
81
+ return PartAvailability.unknown;
54
82
  }
55
83
 
56
- return !this.unavailable.some(_ => _.module === location.module && _.feature === location.feature && _.slice === location.slice);
84
+ const listed = this.find(part);
85
+ return listed === undefined ? PartAvailability.available : PartAvailability.refused(listed.reason);
57
86
  }
58
87
 
59
88
  /**
@@ -68,13 +97,59 @@ export class Availability {
68
97
  return part === undefined || this.isAvailable(part);
69
98
  });
70
99
  }
100
+
101
+ private find(part: AvailabilityPart): UnavailablePart | undefined {
102
+ if (typeof part === 'string') {
103
+ return this.unavailable.find(_ => _.name === part);
104
+ }
105
+
106
+ const identity = identityOf(part);
107
+ if (identity !== undefined) {
108
+ const byName = this.unavailable.find(_ => _.name === identity.name);
109
+ if (byName !== undefined) {
110
+ return byName;
111
+ }
112
+ }
113
+
114
+ const location = locationOf(part, identity);
115
+ if (location === undefined || (location.module === '' && location.feature === '' && location.slice === '')) {
116
+ return undefined;
117
+ }
118
+
119
+ return this.unavailable.find(_ => _.module === location.module && _.feature === location.feature && _.slice === location.slice);
120
+ }
71
121
  }
72
122
 
73
- function sliceLocationOf(part: Exclude<AvailabilityPart, string>): { module: string; feature: string; slice: string } | undefined {
74
- const own = (part as { sliceLocation?: { module: string; feature: string; slice: string } }).sliceLocation;
123
+ function identityOf(part: Exclude<AvailabilityPart, string>): Identity | undefined {
124
+ // A proxy class declares its identity as the static `part`. An instance is read through its class only, because a
125
+ // command or query may itself have a property named `part`.
126
+ if (typeof part === 'function') {
127
+ return asIdentity((part as { part?: unknown }).part);
128
+ }
129
+
130
+ return asIdentity(part) ?? asIdentity((part as { constructor?: { part?: unknown } }).constructor?.part);
131
+ }
132
+
133
+ function asIdentity(value: unknown): Identity | undefined {
134
+ if (typeof value !== 'object' || value === null) {
135
+ return undefined;
136
+ }
137
+
138
+ const candidate = value as Record<string, unknown>;
139
+ return typeof candidate.name === 'string' && typeof candidate.module === 'string' && typeof candidate.feature === 'string' && typeof candidate.slice === 'string'
140
+ ? value as Identity
141
+ : undefined;
142
+ }
143
+
144
+ function locationOf(part: Exclude<AvailabilityPart, string>, identity: Identity | undefined): Location | undefined {
145
+ if (identity !== undefined) {
146
+ return identity;
147
+ }
148
+
149
+ const own = (part as { sliceLocation?: Location }).sliceLocation;
75
150
  if (own !== undefined) {
76
151
  return own;
77
152
  }
78
153
 
79
- return (part as { constructor?: { sliceLocation?: { module: string; feature: string; slice: string } } }).constructor?.sliceLocation;
154
+ return (part as { constructor?: { sliceLocation?: Location } }).constructor?.sliceLocation;
80
155
  }
@@ -7,3 +7,10 @@
7
7
  * all, authorization decides what the caller may do in it.
8
8
  */
9
9
  export const NotAvailable = 'NotAvailable';
10
+
11
+ /**
12
+ * The authorization failure reason of a command or query result, and the reason of a part the server lists, when an
13
+ * operator has suspended the part. It is distinct from {@link NotAvailable}: a suspended part comes back when the
14
+ * suspension is lifted, so a client can say so instead of treating the part as gone.
15
+ */
16
+ export const Suspended = 'Suspended';
@@ -2,6 +2,7 @@
2
2
  // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
3
 
4
4
  import { field } from '@cratis/fundamentals';
5
+ import { NotAvailable } from './AvailabilityReason.js';
5
6
  import { UnavailablePart } from './UnavailablePart.js';
6
7
 
7
8
  /**
@@ -19,6 +20,9 @@ export class UnavailablePartModel implements UnavailablePart {
19
20
 
20
21
  @field(String)
21
22
  slice = '';
23
+
24
+ @field(String)
25
+ reason = NotAvailable;
22
26
  }
23
27
 
24
28
  /**
@@ -5,14 +5,34 @@ import { ObservableQueryFor } from '../queries/ObservableQueryFor.js';
5
5
  import { ParameterDescriptor } from '../reflection/ParameterDescriptor.js';
6
6
  import { AvailableParts } from './AvailableParts.js';
7
7
 
8
+ /**
9
+ * The parameters of {@link ObserveAvailableParts}.
10
+ */
11
+ export interface ObserveAvailablePartsParameters {
12
+ /** The full name of one part to ask about. Every part is decided when it is not given. */
13
+ part?: string;
14
+
15
+ /** The arguments of that part as a JSON object, for a gate that reads `arguments.name`. */
16
+ arguments?: string;
17
+ }
18
+
8
19
  /**
9
20
  * Observes what is not available to the current context. It reports the current decision first and again every time
10
21
  * it changes, so an open session updates without a reload.
22
+ *
23
+ * With `part` (and `arguments`) it answers for that one part and the data it is about: the part is listed when it is
24
+ * not available for those arguments, and the answer is empty when it is available.
11
25
  */
12
- export class ObserveAvailableParts extends ObservableQueryFor<AvailableParts> {
26
+ export class ObserveAvailableParts extends ObservableQueryFor<AvailableParts, ObserveAvailablePartsParameters> {
13
27
  readonly route = '/.cratis/availability';
14
28
  readonly defaultValue = new AvailableParts();
15
- readonly parameterDescriptors: ParameterDescriptor[] = [];
29
+ readonly parameterDescriptors: ParameterDescriptor[] = [
30
+ new ParameterDescriptor('arguments', String, false),
31
+ new ParameterDescriptor('part', String, false),
32
+ ];
33
+
34
+ part?: string;
35
+ arguments?: string;
16
36
 
17
37
  get requiredRequestParameters(): string[] {
18
38
  return [];
@@ -0,0 +1,43 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { NotAvailable, Suspended } from './AvailabilityReason.js';
5
+
6
+ /**
7
+ * The answer to whether one part is available, optionally for the data it is about.
8
+ */
9
+ export class PartAvailability {
10
+ /** The answer for a part that is available. */
11
+ static readonly available: PartAvailability = new PartAvailability(true);
12
+
13
+ /** The answer when the server has not told yet, or could not decide. Nothing is reported as available. */
14
+ static readonly unknown: PartAvailability = new PartAvailability(false, undefined, false);
15
+
16
+ /**
17
+ * Initializes a new instance of {@link PartAvailability}.
18
+ * @param isAvailable Whether the part is available.
19
+ * @param reason Why it is not: `NotAvailable` or `Suspended`; undefined when it is available or not known.
20
+ * @param isKnown Whether the server has decided.
21
+ */
22
+ constructor(readonly isAvailable: boolean, readonly reason?: string, readonly isKnown: boolean = true) {
23
+ }
24
+
25
+ /** Gets whether the part is not available for a reason other than a suspension. */
26
+ get isNotAvailable(): boolean {
27
+ return this.isKnown && !this.isAvailable && this.reason !== Suspended;
28
+ }
29
+
30
+ /** Gets whether an operator has suspended the part. It comes back when the suspension is lifted. */
31
+ get isSuspended(): boolean {
32
+ return this.isKnown && !this.isAvailable && this.reason === Suspended;
33
+ }
34
+
35
+ /**
36
+ * Creates the answer for a reason the server gave.
37
+ * @param reason The reason; a missing reason is `NotAvailable`.
38
+ * @returns The answer.
39
+ */
40
+ static refused(reason?: string): PartAvailability {
41
+ return new PartAvailability(false, reason ?? NotAvailable);
42
+ }
43
+ }
@@ -16,4 +16,7 @@ export interface UnavailablePart {
16
16
 
17
17
  /** The slice the part belongs to; empty when it has none. */
18
18
  readonly slice: string;
19
+
20
+ /** Why it is refused: `NotAvailable` or `Suspended`. A server that does not tell means `NotAvailable`. */
21
+ readonly reason?: string;
19
22
  }
@@ -0,0 +1,66 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { ObservableQuerySubscription } from '../queries/ObservableQuerySubscription.js';
5
+ import { AvailabilityPart, partNameOf } from './Availability.js';
6
+ import { AvailableParts } from './AvailableParts.js';
7
+ import { ObserveAvailableParts } from './ObserveAvailableParts.js';
8
+ import { PartAvailability } from './PartAvailability.js';
9
+
10
+ /**
11
+ * Describes the answer to a question about one part from the server's list of what is not available.
12
+ * @param name The full name of the part that was asked about.
13
+ * @param parts What the server listed.
14
+ * @returns The {@link PartAvailability}.
15
+ */
16
+ export function answerFor(name: string, parts: AvailableParts): PartAvailability {
17
+ const listed = parts.unavailable.find(_ => _.name === name);
18
+ return listed === undefined ? PartAvailability.available : PartAvailability.refused(listed.reason);
19
+ }
20
+
21
+ /**
22
+ * Asks the server whether a part is available for the data it is about, for example one vessel.
23
+ *
24
+ * The server decides with the same gate it uses when the command or query runs, reading `arguments.name` from the
25
+ * arguments given here, so the client never holds a rule of its own. It answers once; use `useIsAvailable` in React
26
+ * to keep the answer current.
27
+ * @param part The full name of a command or read model, or a generated proxy.
28
+ * @param args The arguments of the command or query, by property name. Arguments are for deciding availability only;
29
+ * they never grant anything.
30
+ * @returns The {@link PartAvailability}. When the server could not decide, the answer is {@link PartAvailability.unknown},
31
+ * which is not a denial and not a grant: ask again.
32
+ * @throws When the proxy does not carry the part it is. Generate the proxies again.
33
+ */
34
+ export function askAvailability(part: AvailabilityPart, args?: Record<string, unknown>): Promise<PartAvailability> {
35
+ const name = partNameOf(part);
36
+ if (name === undefined) {
37
+ return Promise.reject(new Error('The proxy does not carry the part it is. Generate the proxies again with the current ProxyGenerator.'));
38
+ }
39
+
40
+ return new Promise<PartAvailability>(resolve => {
41
+ const held: { subscription?: ObservableQuerySubscription<AvailableParts> } = {};
42
+ let finished = false;
43
+ const finish = (answer: PartAvailability) => {
44
+ if (finished) {
45
+ return;
46
+ }
47
+
48
+ finished = true;
49
+ held.subscription?.unsubscribe();
50
+ resolve(answer);
51
+ };
52
+
53
+ const query = new ObserveAvailableParts();
54
+ held.subscription = query.subscribe(result => {
55
+ if (result.isSuccess && result.isAuthorized && result.data?.unavailable !== undefined) {
56
+ finish(answerFor(name, result.data));
57
+ } else if (!result.isSuccess) {
58
+ finish(PartAvailability.unknown);
59
+ }
60
+ }, { part: name, arguments: JSON.stringify(args ?? {}) });
61
+
62
+ if (finished) {
63
+ held.subscription.unsubscribe();
64
+ }
65
+ });
66
+ }
@@ -0,0 +1,24 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { Availability, AvailabilityPart, partNameOf } from '../../Availability.js';
5
+ import { PartIdentity } from '../../../reflection/PartIdentity.js';
6
+
7
+ describe('when asking whether a part is available and the proxy carries its identity', () => {
8
+ const availability = new Availability([{ name: 'Fleet.Billing.Invoice', module: 'Fleet', feature: 'Billing', slice: 'Invoices' }]);
9
+ class ListedProxy {
10
+ static readonly part = new PartIdentity('Fleet.Billing.Invoice', 'Fleet', 'Billing', 'Invoices');
11
+ }
12
+ class ProxyWithAPartProperty {
13
+ static readonly part = new PartIdentity('Fleet.Billing.Other', 'Fleet', 'Billing', 'Other');
14
+ part = 'a value of the command';
15
+ }
16
+
17
+ it('should report a proxy class that is listed by name as not available', () => availability.isAvailable(ListedProxy).should.be.false);
18
+ it('should report an instance of it as not available', () => availability.isAvailable(new ListedProxy()).should.be.false);
19
+ it('should report the identity itself as not available', () => availability.isAvailable(ListedProxy.part).should.be.false);
20
+ it('should not take a property of an instance for the identity', () => availability.isAvailable(new ProxyWithAPartProperty() as unknown as AvailabilityPart).should.be.true);
21
+ it('should give the full name of a proxy', () => partNameOf(ListedProxy)!.should.equal('Fleet.Billing.Invoice'));
22
+ it('should give the full name of an instance', () => partNameOf(new ListedProxy())!.should.equal('Fleet.Billing.Invoice'));
23
+ it('should give no name for a proxy that carries none', () => (partNameOf({ sliceLocation: { module: 'a', feature: 'b', slice: 'c' } }) === undefined).should.be.true);
24
+ });
@@ -0,0 +1,20 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { Availability } from '../../Availability.js';
5
+
6
+ describe('when asking whether a part is available and the server lists it as suspended', () => {
7
+ const availability = new Availability([
8
+ { name: 'Fleet.Quality.Scores', module: 'Fleet', feature: 'Quality', slice: 'Scores', reason: 'Suspended' },
9
+ { name: 'Fleet.Billing.Invoice', module: 'Fleet', feature: 'Billing', slice: 'Invoices', reason: 'NotAvailable' },
10
+ { name: 'Fleet.Billing.Legacy', module: 'Fleet', feature: 'Billing', slice: 'Legacy' }
11
+ ]);
12
+
13
+ it('should tell a suspended part by its reason', () => availability.availabilityOf('Fleet.Quality.Scores').isSuspended.should.be.true);
14
+ it('should not take a suspended part for one that is not available', () => availability.availabilityOf('Fleet.Quality.Scores').isNotAvailable.should.be.false);
15
+ it('should keep a suspended part unavailable', () => availability.isAvailable('Fleet.Quality.Scores').should.be.false);
16
+ it('should tell a part that is not available by its reason', () => availability.availabilityOf('Fleet.Billing.Invoice').isNotAvailable.should.be.true);
17
+ it('should take a missing reason for not available', () => availability.availabilityOf('Fleet.Billing.Legacy').isNotAvailable.should.be.true);
18
+ it('should give no reason for a part that is available', () => (availability.availabilityOf('Fleet.Other').reason === undefined).should.be.true);
19
+ it('should report nothing as available when nothing is known', () => Availability.unknown.availabilityOf('Fleet.Other').isKnown.should.be.false);
20
+ });
@@ -0,0 +1,15 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { answerFor } from '../askAvailability.js';
5
+ import { AvailableParts, UnavailablePartModel } from '../AvailableParts.js';
6
+
7
+ describe('when answering for a part', () => {
8
+ const listed = Object.assign(new UnavailablePartModel(), { name: 'Fleet.Quality.Scores', reason: 'Suspended' });
9
+ const server = Object.assign(new AvailableParts(), { unavailable: [listed] });
10
+
11
+ it('should answer that a part the server lists is not available', () => answerFor('Fleet.Quality.Scores', server).isAvailable.should.be.false);
12
+ it('should answer with the reason the server gave', () => answerFor('Fleet.Quality.Scores', server).isSuspended.should.be.true);
13
+ it('should answer that a part the server does not list is available', () => answerFor('Fleet.Billing.Invoice', server).isAvailable.should.be.true);
14
+ it('should answer that every part is available when the server lists none', () => answerFor('Fleet.Quality.Scores', new AvailableParts()).isAvailable.should.be.true);
15
+ });
@@ -3,7 +3,10 @@
3
3
 
4
4
  export * from './AvailabilityReason.js';
5
5
  export * from './isNotAvailable.js';
6
+ export * from './isSuspended.js';
6
7
  export * from './UnavailablePart.js';
7
8
  export * from './AvailableParts.js';
8
9
  export * from './ObserveAvailableParts.js';
10
+ export * from './PartAvailability.js';
9
11
  export * from './Availability.js';
12
+ export * from './askAvailability.js';
@@ -0,0 +1,14 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { Suspended } from './AvailabilityReason.js';
5
+
6
+ /**
7
+ * Determines whether a command or query result was refused because an operator has suspended its part of the
8
+ * application. It is not the same as {@link isNotAvailable}: a suspended part comes back when the suspension is lifted.
9
+ * @param result The result of a command or a query.
10
+ * @returns True when the part is suspended.
11
+ */
12
+ export function isSuspended(result: { readonly isAuthorized: boolean; readonly authorizationFailureReason?: string }): boolean {
13
+ return !result.isAuthorized && result.authorizationFailureReason === Suspended;
14
+ }
@@ -5,6 +5,7 @@ import { Guid } from '@cratis/fundamentals';
5
5
  import { ICommandResult } from './ICommandResult.js';
6
6
  import { ValidationResult } from '../validation/ValidationResult.js';
7
7
  import { isNotAvailable } from '../availability/isNotAvailable.js';
8
+ import { isSuspended } from '../availability/isSuspended.js';
8
9
  import { Constructor, JsonSerializer } from '@cratis/fundamentals';
9
10
 
10
11
  /**
@@ -250,6 +251,26 @@ export class CommandResult<TResponse = object> implements ICommandResult<TRespon
250
251
  return this;
251
252
  }
252
253
 
254
+ /**
255
+ * Gets whether the command was refused because an operator has suspended its part of the application.
256
+ * It is not the same as {@link isNotAvailable}: a suspended part comes back when the suspension is lifted.
257
+ */
258
+ get isSuspended(): boolean {
259
+ return isSuspended(this);
260
+ }
261
+
262
+ /**
263
+ * Set up a callback for when the command was refused because its part of the application is suspended.
264
+ * @param {OnNotAvailable} callback The callback to call when the part was suspended.
265
+ * @returns {CommandResult} The instance of the command result.
266
+ */
267
+ onSuspended(callback: OnNotAvailable): CommandResult<TResponse> {
268
+ if (this.isSuspended) {
269
+ callback();
270
+ }
271
+ return this;
272
+ }
273
+
253
274
  /**
254
275
  * Set up a callback for when the command had validation errors.
255
276
  * @param {OnSuccess} callback The callback to call when the command was invalid.
@@ -1,19 +1,23 @@
1
1
  import { UnavailablePart } from './UnavailablePart.js';
2
- export type AvailabilityPart = string | {
3
- readonly sliceLocation?: {
4
- readonly module: string;
5
- readonly feature: string;
6
- readonly slice: string;
7
- };
2
+ import { PartAvailability } from './PartAvailability.js';
3
+ interface Location {
4
+ readonly module: string;
5
+ readonly feature: string;
6
+ readonly slice: string;
7
+ }
8
+ interface Identity extends Location {
9
+ readonly name: string;
10
+ }
11
+ export type AvailabilityPart = string | Identity | {
12
+ readonly part?: Identity;
13
+ readonly sliceLocation?: Location;
8
14
  } | {
9
15
  readonly constructor: {
10
- readonly sliceLocation?: {
11
- readonly module: string;
12
- readonly feature: string;
13
- readonly slice: string;
14
- };
16
+ readonly part?: Identity;
17
+ readonly sliceLocation?: Location;
15
18
  };
16
19
  };
20
+ export declare function partNameOf(part: AvailabilityPart): string | undefined;
17
21
  export declare class Availability {
18
22
  readonly unavailable: readonly UnavailablePart[];
19
23
  readonly isKnown: boolean;
@@ -21,6 +25,9 @@ export declare class Availability {
21
25
  static readonly everything: Availability;
22
26
  constructor(unavailable: readonly UnavailablePart[], isKnown?: boolean);
23
27
  isAvailable(part: AvailabilityPart): boolean;
28
+ availabilityOf(part: AvailabilityPart): PartAvailability;
24
29
  filter<TItem>(items: readonly TItem[], partOf: (item: TItem) => AvailabilityPart | undefined): TItem[];
30
+ private find;
25
31
  }
32
+ export {};
26
33
  //# sourceMappingURL=Availability.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Availability.d.ts","sourceRoot":"","sources":["../../../availability/Availability.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,eAAe,EAAE,MAA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
2
 
3
+ var PartAvailability = require('./PartAvailability.js');
4
+
3
5
  // Copyright (c) Cratis. All rights reserved.
4
6
  // Licensed under the MIT license. See LICENSE file in the project root for full license information.
7
+ /**
8
+ * Gets the full name of a part, when it is known.
9
+ * @param part The full name, or a generated proxy or part identity.
10
+ * @returns The name the server knows the part by; undefined when the proxy does not carry one.
11
+ */ function partNameOf(part) {
12
+ if (typeof part === 'string') {
13
+ return part;
14
+ }
15
+ return identityOf(part)?.name;
16
+ }
5
17
  /**
6
18
  * Represents what is available to the current context, from the last decision the server reported.
7
19
  *
8
20
  * Everything the server does not list is available. Until the first decision has arrived nothing is known, and
9
21
  * nothing is reported as available, so a client never shows a part it has not been told it may show.
22
+ *
23
+ * This answers for the context alone, such as the tenant and the user. A gate that depends on the data a command or
24
+ * query is about, such as one vessel, is asked with `askAvailability` (or `useIsAvailable` in React), which sends the data.
10
25
  */ class Availability {
11
26
  unavailable;
12
27
  isKnown;
@@ -22,21 +37,22 @@
22
37
  }
23
38
  /**
24
39
  * Determines whether a part is available.
25
- * @param part The full name of a command or read model, or a generated proxy. A proxy is matched by the slice it
26
- * belongs to, so every part of a slice is hidden together, which is how navigation and screens are decided.
40
+ * @param part The full name of a command or read model, or a generated proxy. A proxy is matched by the part it is
41
+ * and by the slice it belongs to, so every part of a slice is hidden together, which is how navigation and screens are decided.
27
42
  * @returns True when the part is available; false when it is not, or when nothing is known yet.
28
43
  */ isAvailable(part) {
44
+ return this.availabilityOf(part).isAvailable;
45
+ }
46
+ /**
47
+ * Gets whether, and if not why, a part is available.
48
+ * @param part The full name of a command or read model, or a generated proxy.
49
+ * @returns The {@link PartAvailability}; `NotAvailable` is told apart from `Suspended` by its reason.
50
+ */ availabilityOf(part) {
29
51
  if (!this.isKnown) {
30
- return false;
31
- }
32
- if (typeof part === 'string') {
33
- return !this.unavailable.some((_)=>_.name === part);
34
- }
35
- const location = sliceLocationOf(part);
36
- if (location === undefined || location.module === '' && location.feature === '' && location.slice === '') {
37
- return true;
52
+ return PartAvailability.PartAvailability.unknown;
38
53
  }
39
- return !this.unavailable.some((_)=>_.module === location.module && _.feature === location.feature && _.slice === location.slice);
54
+ const listed = this.find(part);
55
+ return listed === undefined ? PartAvailability.PartAvailability.available : PartAvailability.PartAvailability.refused(listed.reason);
40
56
  }
41
57
  /**
42
58
  * Keeps the items whose part is available.
@@ -49,8 +65,43 @@
49
65
  return part === undefined || this.isAvailable(part);
50
66
  });
51
67
  }
68
+ find(part) {
69
+ if (typeof part === 'string') {
70
+ return this.unavailable.find((_)=>_.name === part);
71
+ }
72
+ const identity = identityOf(part);
73
+ if (identity !== undefined) {
74
+ const byName = this.unavailable.find((_)=>_.name === identity.name);
75
+ if (byName !== undefined) {
76
+ return byName;
77
+ }
78
+ }
79
+ const location = locationOf(part, identity);
80
+ if (location === undefined || location.module === '' && location.feature === '' && location.slice === '') {
81
+ return undefined;
82
+ }
83
+ return this.unavailable.find((_)=>_.module === location.module && _.feature === location.feature && _.slice === location.slice);
84
+ }
52
85
  }
53
- function sliceLocationOf(part) {
86
+ function identityOf(part) {
87
+ // A proxy class declares its identity as the static `part`. An instance is read through its class only, because a
88
+ // command or query may itself have a property named `part`.
89
+ if (typeof part === 'function') {
90
+ return asIdentity(part.part);
91
+ }
92
+ return asIdentity(part) ?? asIdentity(part.constructor?.part);
93
+ }
94
+ function asIdentity(value) {
95
+ if (typeof value !== 'object' || value === null) {
96
+ return undefined;
97
+ }
98
+ const candidate = value;
99
+ return typeof candidate.name === 'string' && typeof candidate.module === 'string' && typeof candidate.feature === 'string' && typeof candidate.slice === 'string' ? value : undefined;
100
+ }
101
+ function locationOf(part, identity) {
102
+ if (identity !== undefined) {
103
+ return identity;
104
+ }
54
105
  const own = part.sliceLocation;
55
106
  if (own !== undefined) {
56
107
  return own;
@@ -59,4 +110,5 @@ function sliceLocationOf(part) {
59
110
  }
60
111
 
61
112
  exports.Availability = Availability;
113
+ exports.partNameOf = partNameOf;
62
114
  //# sourceMappingURL=Availability.js.map