@chipmobilesdk/rn-notification 0.1.2 → 0.2.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 (69) hide show
  1. package/CHANGELOG.md +96 -42
  2. package/README.md +399 -306
  3. package/package.json +2 -1
  4. package/skills/sdk-rn-notification/SKILL.md +68 -3
  5. package/src/channel/migrate.ts +86 -12
  6. package/src/channel/tone-map.ts +130 -90
  7. package/src/channel/validate-tone.ts +61 -0
  8. package/src/contract/channel.ts +188 -89
  9. package/src/contract/diagnostics.ts +102 -102
  10. package/src/contract/errors.ts +130 -123
  11. package/src/contract/index.ts +10 -10
  12. package/src/contract/interaction.ts +62 -62
  13. package/src/contract/notification.ts +118 -118
  14. package/src/contract/permission.ts +60 -60
  15. package/src/contract/reconcile.ts +88 -88
  16. package/src/contract/recurrence.ts +123 -123
  17. package/src/diagnostics/burst.ts +101 -101
  18. package/src/diagnostics/events.ts +72 -72
  19. package/src/diagnostics/redact.ts +72 -72
  20. package/src/diagnostics/snapshot.ts +64 -64
  21. package/src/engine/adapter.ts +172 -79
  22. package/src/engine/notify-kit-engine.ts +761 -535
  23. package/src/index.ts +24 -0
  24. package/src/initialize.ts +174 -174
  25. package/src/interaction/index.ts +146 -146
  26. package/src/maintenance/cycle.ts +147 -147
  27. package/src/maintenance/permission-refresh.ts +41 -41
  28. package/src/maintenance/reanchor.ts +95 -95
  29. package/src/maintenance/toggle.ts +60 -60
  30. package/src/maintenance/verify.ts +83 -83
  31. package/src/maintenance/window-refresh.ts +61 -61
  32. package/src/permission/index.ts +183 -183
  33. package/src/permission/state.ts +77 -77
  34. package/src/push/envelope.ts +165 -165
  35. package/src/push/fcm-provider.ts +146 -146
  36. package/src/push/gate.ts +190 -190
  37. package/src/push/instructions.ts +109 -109
  38. package/src/push/provider-adapter.ts +54 -54
  39. package/src/push/signout.ts +65 -65
  40. package/src/reconcile/chunk.ts +53 -53
  41. package/src/reconcile/diff.ts +145 -145
  42. package/src/reconcile/duplicates.ts +73 -73
  43. package/src/reconcile/index.ts +14 -1
  44. package/src/reconcile/truncate.ts +56 -56
  45. package/src/recurrence/clock-guard.ts +71 -71
  46. package/src/recurrence/expand.ts +292 -292
  47. package/src/repeat/expiry.ts +55 -0
  48. package/src/repeat/index.ts +54 -0
  49. package/src/repeat/marker.ts +72 -0
  50. package/src/repeat/place.ts +73 -0
  51. package/src/schedule/anchor.ts +267 -267
  52. package/src/schedule/index.ts +184 -173
  53. package/src/schedule/partition.ts +85 -85
  54. package/src/schedule/resolve-moment.ts +80 -80
  55. package/src/schedule/validate-content.ts +111 -111
  56. package/src/schedule/validate-identifier.ts +126 -114
  57. package/src/state/caps.ts +71 -71
  58. package/src/state/interaction-buffer.ts +73 -73
  59. package/src/state/mmkv-store.ts +75 -71
  60. package/src/state/permission-history.ts +41 -41
  61. package/src/state/record.ts +258 -246
  62. package/src/state/reset.ts +41 -41
  63. package/src/state/series-ledger.ts +84 -84
  64. package/src/state/store.ts +40 -40
  65. package/src/testing/conformance/index.ts +248 -194
  66. package/src/testing/fake-engine.ts +469 -284
  67. package/src/testing/fake-provider.ts +129 -129
  68. package/src/testing/faults.ts +72 -72
  69. package/src/testing/measure.ts +337 -334
@@ -1,146 +1,146 @@
1
- /**
2
- * Interaction routing.
3
- *
4
- * One handler covers foreground, background, and cold start. The hard case is
5
- * the last one: the tap happens before any JavaScript exists to receive it, so
6
- * the event has to wait — durably — until a handler registers.
7
- *
8
- * Events are delivered in occurrence order and never dropped for want of a
9
- * listener. After delivery the notification leaves the tray, because a
10
- * notification the user has already acted on sitting there is noise.
11
- */
12
-
13
- import type {
14
- InteractionEvent,
15
- InteractionHandler,
16
- TargetMissingSignal,
17
- Unsubscribe,
18
- } from '../contract/interaction';
19
- import type { NotificationEngineAdapter } from '../engine/adapter';
20
- import type { StateManager } from '../state/record';
21
- import { DIAGNOSTIC_CODES } from '../contract/errors';
22
- import { buffer, bufferedCount, drain } from '../state/interaction-buffer';
23
-
24
- export interface InteractionDependencies {
25
- readonly engine: NotificationEngineAdapter;
26
- readonly state: StateManager;
27
- readonly now: () => number;
28
- readonly emit?: (code: string, message: string, context?: Record<string, unknown>) => void;
29
- }
30
-
31
- export function createInteractions(deps: InteractionDependencies) {
32
- const emit = deps.emit ?? (() => {});
33
- let handlers: InteractionHandler[] = [];
34
- let engineSubscription: Unsubscribe | undefined;
35
- /** Serializes delivery so rapid interactions arrive in order, never interleaved. */
36
- let deliveryChain: Promise<void> = Promise.resolve();
37
-
38
- const deliver = (event: InteractionEvent): void => {
39
- deliveryChain = deliveryChain.then(async () => {
40
- for (const handler of handlers) {
41
- try {
42
- await handler(event);
43
- } catch {
44
- // A consumer's handler failing must not stop the next event, or one
45
- // bad tap would silently swallow every tap after it.
46
- }
47
- }
48
-
49
- // Acted on, so it leaves the tray and stops making noise.
50
- if (event.kind !== 'dismiss') {
51
- await deps.engine.cancel(event.notificationId).catch(() => undefined);
52
- }
53
- });
54
- };
55
-
56
- const route = (event: InteractionEvent): void => {
57
- if (handlers.length === 0) {
58
- buffer(deps.state, event);
59
- return;
60
- }
61
- deliver(event);
62
- };
63
-
64
- const ensureSubscribed = (): void => {
65
- engineSubscription ??= deps.engine.subscribeToInteractions(route);
66
- };
67
-
68
- // Subscribing immediately rather than on first handler registration: an
69
- // interaction arriving in the gap would otherwise be lost entirely, which is
70
- // the exact failure the buffer exists to prevent.
71
- ensureSubscribed();
72
-
73
- return {
74
- /**
75
- * Registers a handler and immediately drains anything that was waiting.
76
- *
77
- * The drain is what makes a cold-start tap work: the event was persisted
78
- * before this handler existed, possibly before this process existed.
79
- */
80
- onInteraction(handler: InteractionHandler): Unsubscribe {
81
- handlers.push(handler);
82
-
83
- for (const event of drain(deps.state)) {
84
- deliver(event);
85
- }
86
-
87
- return () => {
88
- handlers = handlers.filter(h => h !== handler);
89
- };
90
- },
91
-
92
- /**
93
- * Collects the interaction that launched the app, if any.
94
- *
95
- * Called once during startup. If no handler exists yet the event is
96
- * buffered rather than discarded.
97
- */
98
- async collectInitial(): Promise<void> {
99
- const initial = await deps.engine.takeInitialInteraction();
100
- if (initial === undefined) return;
101
- route(initial);
102
- },
103
-
104
- /**
105
- * Signals that a routing target no longer exists.
106
- *
107
- * The consumer chooses its own fallback screen. No platform error is ever
108
- * shown to the user for this — a notification pointing at deleted data is
109
- * an ordinary condition, not a crash.
110
- */
111
- reportTargetMissing(signal: TargetMissingSignal): void {
112
- emit(
113
- DIAGNOSTIC_CODES.TARGET_MISSING,
114
- 'The routing target no longer exists. The consumer chooses a fallback; no platform error is shown to the user.',
115
- { notificationId: signal.notificationId },
116
- );
117
- },
118
-
119
- /** Reports that a background action exceeded the platform's execution window. */
120
- reportBackgroundTimeout(notificationId: string, windowMs: number): void {
121
- emit(
122
- DIAGNOSTIC_CODES.BACKGROUND_WINDOW_EXCEEDED,
123
- `A background action exceeded the platform's ${windowMs} ms execution window and was cut short.`,
124
- { notificationId, windowMs },
125
- );
126
- },
127
-
128
- /** How many interactions are waiting for a handler. */
129
- pendingCount(): number {
130
- return bufferedCount(deps.state);
131
- },
132
-
133
- /** Settles outstanding deliveries. Exists for tests and orderly shutdown. */
134
- async flush(): Promise<void> {
135
- await deliveryChain;
136
- },
137
-
138
- dispose(): void {
139
- engineSubscription?.();
140
- engineSubscription = undefined;
141
- handlers = [];
142
- },
143
- };
144
- }
145
-
146
- export type Interactions = ReturnType<typeof createInteractions>;
1
+ /**
2
+ * Interaction routing.
3
+ *
4
+ * One handler covers foreground, background, and cold start. The hard case is
5
+ * the last one: the tap happens before any JavaScript exists to receive it, so
6
+ * the event has to wait — durably — until a handler registers.
7
+ *
8
+ * Events are delivered in occurrence order and never dropped for want of a
9
+ * listener. After delivery the notification leaves the tray, because a
10
+ * notification the user has already acted on sitting there is noise.
11
+ */
12
+
13
+ import type {
14
+ InteractionEvent,
15
+ InteractionHandler,
16
+ TargetMissingSignal,
17
+ Unsubscribe,
18
+ } from '../contract/interaction';
19
+ import type { NotificationEngineAdapter } from '../engine/adapter';
20
+ import type { StateManager } from '../state/record';
21
+ import { DIAGNOSTIC_CODES } from '../contract/errors';
22
+ import { buffer, bufferedCount, drain } from '../state/interaction-buffer';
23
+
24
+ export interface InteractionDependencies {
25
+ readonly engine: NotificationEngineAdapter;
26
+ readonly state: StateManager;
27
+ readonly now: () => number;
28
+ readonly emit?: (code: string, message: string, context?: Record<string, unknown>) => void;
29
+ }
30
+
31
+ export function createInteractions(deps: InteractionDependencies) {
32
+ const emit = deps.emit ?? (() => {});
33
+ let handlers: InteractionHandler[] = [];
34
+ let engineSubscription: Unsubscribe | undefined;
35
+ /** Serializes delivery so rapid interactions arrive in order, never interleaved. */
36
+ let deliveryChain: Promise<void> = Promise.resolve();
37
+
38
+ const deliver = (event: InteractionEvent): void => {
39
+ deliveryChain = deliveryChain.then(async () => {
40
+ for (const handler of handlers) {
41
+ try {
42
+ await handler(event);
43
+ } catch {
44
+ // A consumer's handler failing must not stop the next event, or one
45
+ // bad tap would silently swallow every tap after it.
46
+ }
47
+ }
48
+
49
+ // Acted on, so it leaves the tray and stops making noise.
50
+ if (event.kind !== 'dismiss') {
51
+ await deps.engine.cancel(event.notificationId).catch(() => undefined);
52
+ }
53
+ });
54
+ };
55
+
56
+ const route = (event: InteractionEvent): void => {
57
+ if (handlers.length === 0) {
58
+ buffer(deps.state, event);
59
+ return;
60
+ }
61
+ deliver(event);
62
+ };
63
+
64
+ const ensureSubscribed = (): void => {
65
+ engineSubscription ??= deps.engine.subscribeToInteractions(route);
66
+ };
67
+
68
+ // Subscribing immediately rather than on first handler registration: an
69
+ // interaction arriving in the gap would otherwise be lost entirely, which is
70
+ // the exact failure the buffer exists to prevent.
71
+ ensureSubscribed();
72
+
73
+ return {
74
+ /**
75
+ * Registers a handler and immediately drains anything that was waiting.
76
+ *
77
+ * The drain is what makes a cold-start tap work: the event was persisted
78
+ * before this handler existed, possibly before this process existed.
79
+ */
80
+ onInteraction(handler: InteractionHandler): Unsubscribe {
81
+ handlers.push(handler);
82
+
83
+ for (const event of drain(deps.state)) {
84
+ deliver(event);
85
+ }
86
+
87
+ return () => {
88
+ handlers = handlers.filter(h => h !== handler);
89
+ };
90
+ },
91
+
92
+ /**
93
+ * Collects the interaction that launched the app, if any.
94
+ *
95
+ * Called once during startup. If no handler exists yet the event is
96
+ * buffered rather than discarded.
97
+ */
98
+ async collectInitial(): Promise<void> {
99
+ const initial = await deps.engine.takeInitialInteraction();
100
+ if (initial === undefined) return;
101
+ route(initial);
102
+ },
103
+
104
+ /**
105
+ * Signals that a routing target no longer exists.
106
+ *
107
+ * The consumer chooses its own fallback screen. No platform error is ever
108
+ * shown to the user for this — a notification pointing at deleted data is
109
+ * an ordinary condition, not a crash.
110
+ */
111
+ reportTargetMissing(signal: TargetMissingSignal): void {
112
+ emit(
113
+ DIAGNOSTIC_CODES.TARGET_MISSING,
114
+ 'The routing target no longer exists. The consumer chooses a fallback; no platform error is shown to the user.',
115
+ { notificationId: signal.notificationId },
116
+ );
117
+ },
118
+
119
+ /** Reports that a background action exceeded the platform's execution window. */
120
+ reportBackgroundTimeout(notificationId: string, windowMs: number): void {
121
+ emit(
122
+ DIAGNOSTIC_CODES.BACKGROUND_WINDOW_EXCEEDED,
123
+ `A background action exceeded the platform's ${windowMs} ms execution window and was cut short.`,
124
+ { notificationId, windowMs },
125
+ );
126
+ },
127
+
128
+ /** How many interactions are waiting for a handler. */
129
+ pendingCount(): number {
130
+ return bufferedCount(deps.state);
131
+ },
132
+
133
+ /** Settles outstanding deliveries. Exists for tests and orderly shutdown. */
134
+ async flush(): Promise<void> {
135
+ await deliveryChain;
136
+ },
137
+
138
+ dispose(): void {
139
+ engineSubscription?.();
140
+ engineSubscription = undefined;
141
+ handlers = [];
142
+ },
143
+ };
144
+ }
145
+
146
+ export type Interactions = ReturnType<typeof createInteractions>;
@@ -1,147 +1,147 @@
1
- /**
2
- * The automatic maintenance cycle.
3
- *
4
- * Covers exactly what only the package can know — window refresh, recurrence
5
- * expansion, wall-clock re-anchoring, pending-set verification, permission
6
- * re-reads. Full desired-set reconciliation is never automatic, because only
7
- * the consumer knows what its data implies and a package that guessed would
8
- * cancel notifications it does not understand.
9
- *
10
- * Steps register themselves here rather than being wired in a fixed order. That
11
- * is what lets scheduling, reconciliation, recurrence, and permissions each
12
- * contribute a step without importing one another.
13
- *
14
- * The rule that makes automatic work safe: a step may act only on notifications
15
- * the package owns, and may never cancel anything else. It is enforced here
16
- * rather than trusted to each step.
17
- */
18
-
19
- import type { MaintenanceResult, PendingDiscrepancy } from '../contract/reconcile';
20
-
21
- export type MaintenanceTrigger =
22
- | 'foreground'
23
- | 'delivery'
24
- | 'timeZoneChange'
25
- | 'permissionChange'
26
- | 'explicit';
27
-
28
- export interface MaintenanceContext {
29
- readonly trigger: MaintenanceTrigger;
30
- readonly now: number;
31
- /**
32
- * Identifiers the package owns — series occurrences and entries it created.
33
- * A step may cancel only within this set.
34
- */
35
- readonly ownedIds: ReadonlySet<string>;
36
- }
37
-
38
- export interface StepOutcome {
39
- readonly occurrencesScheduled?: number;
40
- readonly reAnchored?: number;
41
- readonly windowRefreshed?: boolean;
42
- readonly timeZoneChanged?: boolean;
43
- readonly permissionChanged?: boolean;
44
- readonly discrepancy?: PendingDiscrepancy;
45
- /** Platform operations issued. Zero on a no-change pass. */
46
- readonly operationCount?: number;
47
- /** Identifiers this step cancelled, checked against `ownedIds`. */
48
- readonly cancelledIds?: readonly string[];
49
- }
50
-
51
- export interface MaintenanceStep {
52
- readonly name: string;
53
- /** Lower runs first. Re-anchoring must precede verification, or verification sees stale instants. */
54
- readonly order: number;
55
- run(context: MaintenanceContext): Promise<StepOutcome>;
56
- }
57
-
58
- export class MaintenanceScopeViolation extends Error {
59
- readonly step: string;
60
- readonly offendingIds: readonly string[];
61
-
62
- constructor(step: string, offendingIds: readonly string[]) {
63
- super(
64
- `Maintenance step "${step}" attempted to cancel ${offendingIds.length} notification(s) it does not own. ` +
65
- 'Automatic maintenance may never cancel a directly-scheduled notification.',
66
- );
67
- this.name = 'MaintenanceScopeViolation';
68
- this.step = step;
69
- this.offendingIds = offendingIds;
70
- Object.setPrototypeOf(this, MaintenanceScopeViolation.prototype);
71
- }
72
- }
73
-
74
- export class MaintenanceRegistry {
75
- private steps: MaintenanceStep[] = [];
76
- private running = false;
77
-
78
- register(step: MaintenanceStep): () => void {
79
- this.steps.push(step);
80
- this.steps.sort((a, b) => a.order - b.order);
81
- return () => {
82
- this.steps = this.steps.filter(s => s !== step);
83
- };
84
- }
85
-
86
- get registered(): readonly string[] {
87
- return this.steps.map(s => s.name);
88
- }
89
-
90
- /**
91
- * Runs every registered step in order.
92
- *
93
- * Overlapping cycles are collapsed rather than queued: a second foreground
94
- * event arriving mid-cycle wants the same work the running cycle is already
95
- * doing.
96
- */
97
- async run(context: MaintenanceContext): Promise<MaintenanceResult> {
98
- const started = context.now;
99
-
100
- if (this.running) {
101
- return {
102
- ranAt: started,
103
- windowRefreshed: false,
104
- occurrencesScheduled: 0,
105
- reAnchored: 0,
106
- timeZoneChanged: false,
107
- permissionChanged: false,
108
- operationCount: 0,
109
- durationMs: 0,
110
- };
111
- }
112
-
113
- this.running = true;
114
- const aggregate = {
115
- windowRefreshed: false,
116
- occurrencesScheduled: 0,
117
- reAnchored: 0,
118
- timeZoneChanged: false,
119
- permissionChanged: false,
120
- operationCount: 0,
121
- };
122
- let discrepancy: PendingDiscrepancy | undefined;
123
-
124
- try {
125
- for (const step of this.steps) {
126
- const outcome = await step.run(context);
127
-
128
- const offending = (outcome.cancelledIds ?? []).filter(id => !context.ownedIds.has(id));
129
- if (offending.length > 0) {
130
- throw new MaintenanceScopeViolation(step.name, offending);
131
- }
132
-
133
- aggregate.windowRefreshed ||= outcome.windowRefreshed ?? false;
134
- aggregate.occurrencesScheduled += outcome.occurrencesScheduled ?? 0;
135
- aggregate.reAnchored += outcome.reAnchored ?? 0;
136
- aggregate.timeZoneChanged ||= outcome.timeZoneChanged ?? false;
137
- aggregate.permissionChanged ||= outcome.permissionChanged ?? false;
138
- aggregate.operationCount += outcome.operationCount ?? 0;
139
- discrepancy ??= outcome.discrepancy;
140
- }
141
- } finally {
142
- this.running = false;
143
- }
144
-
145
- return { ranAt: started, ...aggregate, discrepancy, durationMs: 0 };
146
- }
147
- }
1
+ /**
2
+ * The automatic maintenance cycle.
3
+ *
4
+ * Covers exactly what only the package can know — window refresh, recurrence
5
+ * expansion, wall-clock re-anchoring, pending-set verification, permission
6
+ * re-reads. Full desired-set reconciliation is never automatic, because only
7
+ * the consumer knows what its data implies and a package that guessed would
8
+ * cancel notifications it does not understand.
9
+ *
10
+ * Steps register themselves here rather than being wired in a fixed order. That
11
+ * is what lets scheduling, reconciliation, recurrence, and permissions each
12
+ * contribute a step without importing one another.
13
+ *
14
+ * The rule that makes automatic work safe: a step may act only on notifications
15
+ * the package owns, and may never cancel anything else. It is enforced here
16
+ * rather than trusted to each step.
17
+ */
18
+
19
+ import type { MaintenanceResult, PendingDiscrepancy } from '../contract/reconcile';
20
+
21
+ export type MaintenanceTrigger =
22
+ | 'foreground'
23
+ | 'delivery'
24
+ | 'timeZoneChange'
25
+ | 'permissionChange'
26
+ | 'explicit';
27
+
28
+ export interface MaintenanceContext {
29
+ readonly trigger: MaintenanceTrigger;
30
+ readonly now: number;
31
+ /**
32
+ * Identifiers the package owns — series occurrences and entries it created.
33
+ * A step may cancel only within this set.
34
+ */
35
+ readonly ownedIds: ReadonlySet<string>;
36
+ }
37
+
38
+ export interface StepOutcome {
39
+ readonly occurrencesScheduled?: number;
40
+ readonly reAnchored?: number;
41
+ readonly windowRefreshed?: boolean;
42
+ readonly timeZoneChanged?: boolean;
43
+ readonly permissionChanged?: boolean;
44
+ readonly discrepancy?: PendingDiscrepancy;
45
+ /** Platform operations issued. Zero on a no-change pass. */
46
+ readonly operationCount?: number;
47
+ /** Identifiers this step cancelled, checked against `ownedIds`. */
48
+ readonly cancelledIds?: readonly string[];
49
+ }
50
+
51
+ export interface MaintenanceStep {
52
+ readonly name: string;
53
+ /** Lower runs first. Re-anchoring must precede verification, or verification sees stale instants. */
54
+ readonly order: number;
55
+ run(context: MaintenanceContext): Promise<StepOutcome>;
56
+ }
57
+
58
+ export class MaintenanceScopeViolation extends Error {
59
+ readonly step: string;
60
+ readonly offendingIds: readonly string[];
61
+
62
+ constructor(step: string, offendingIds: readonly string[]) {
63
+ super(
64
+ `Maintenance step "${step}" attempted to cancel ${offendingIds.length} notification(s) it does not own. ` +
65
+ 'Automatic maintenance may never cancel a directly-scheduled notification.',
66
+ );
67
+ this.name = 'MaintenanceScopeViolation';
68
+ this.step = step;
69
+ this.offendingIds = offendingIds;
70
+ Object.setPrototypeOf(this, MaintenanceScopeViolation.prototype);
71
+ }
72
+ }
73
+
74
+ export class MaintenanceRegistry {
75
+ private steps: MaintenanceStep[] = [];
76
+ private running = false;
77
+
78
+ register(step: MaintenanceStep): () => void {
79
+ this.steps.push(step);
80
+ this.steps.sort((a, b) => a.order - b.order);
81
+ return () => {
82
+ this.steps = this.steps.filter(s => s !== step);
83
+ };
84
+ }
85
+
86
+ get registered(): readonly string[] {
87
+ return this.steps.map(s => s.name);
88
+ }
89
+
90
+ /**
91
+ * Runs every registered step in order.
92
+ *
93
+ * Overlapping cycles are collapsed rather than queued: a second foreground
94
+ * event arriving mid-cycle wants the same work the running cycle is already
95
+ * doing.
96
+ */
97
+ async run(context: MaintenanceContext): Promise<MaintenanceResult> {
98
+ const started = context.now;
99
+
100
+ if (this.running) {
101
+ return {
102
+ ranAt: started,
103
+ windowRefreshed: false,
104
+ occurrencesScheduled: 0,
105
+ reAnchored: 0,
106
+ timeZoneChanged: false,
107
+ permissionChanged: false,
108
+ operationCount: 0,
109
+ durationMs: 0,
110
+ };
111
+ }
112
+
113
+ this.running = true;
114
+ const aggregate = {
115
+ windowRefreshed: false,
116
+ occurrencesScheduled: 0,
117
+ reAnchored: 0,
118
+ timeZoneChanged: false,
119
+ permissionChanged: false,
120
+ operationCount: 0,
121
+ };
122
+ let discrepancy: PendingDiscrepancy | undefined;
123
+
124
+ try {
125
+ for (const step of this.steps) {
126
+ const outcome = await step.run(context);
127
+
128
+ const offending = (outcome.cancelledIds ?? []).filter(id => !context.ownedIds.has(id));
129
+ if (offending.length > 0) {
130
+ throw new MaintenanceScopeViolation(step.name, offending);
131
+ }
132
+
133
+ aggregate.windowRefreshed ||= outcome.windowRefreshed ?? false;
134
+ aggregate.occurrencesScheduled += outcome.occurrencesScheduled ?? 0;
135
+ aggregate.reAnchored += outcome.reAnchored ?? 0;
136
+ aggregate.timeZoneChanged ||= outcome.timeZoneChanged ?? false;
137
+ aggregate.permissionChanged ||= outcome.permissionChanged ?? false;
138
+ aggregate.operationCount += outcome.operationCount ?? 0;
139
+ discrepancy ??= outcome.discrepancy;
140
+ }
141
+ } finally {
142
+ this.running = false;
143
+ }
144
+
145
+ return { ranAt: started, ...aggregate, discrepancy, durationMs: 0 };
146
+ }
147
+ }
@@ -1,41 +1,41 @@
1
- /**
2
- * The permission re-read maintenance step.
3
- *
4
- * The user can change permission in system settings while the app is
5
- * backgrounded, and the platform does not tell anyone. Re-reading on foreground
6
- * is the only way a consumer's interface avoids showing a stale answer.
7
- *
8
- * Runs last (order 30): permission state does not affect what the earlier steps
9
- * expand or re-anchor, and reading it after they have run means the report
10
- * reflects the device as the cycle leaves it.
11
- */
12
-
13
- import type { MaintenanceStep, MaintenanceContext, StepOutcome } from './cycle';
14
- import type { Permissions } from '../permission';
15
-
16
- export const PERMISSION_REFRESH_ORDER = 30;
17
-
18
- export interface PermissionRefreshDependencies {
19
- readonly permissions: Permissions;
20
- }
21
-
22
- export function createPermissionRefreshStep(
23
- deps: PermissionRefreshDependencies,
24
- ): MaintenanceStep {
25
- return {
26
- name: 'permission-refresh',
27
- order: PERMISSION_REFRESH_ORDER,
28
-
29
- async run(_context: MaintenanceContext): Promise<StepOutcome> {
30
- const { changed } = await deps.permissions.refresh();
31
-
32
- return {
33
- // Reading permission is not a platform mutation, so it never counts
34
- // toward the zero-operation guarantee.
35
- operationCount: 0,
36
- permissionChanged: changed,
37
- cancelledIds: [],
38
- };
39
- },
40
- };
41
- }
1
+ /**
2
+ * The permission re-read maintenance step.
3
+ *
4
+ * The user can change permission in system settings while the app is
5
+ * backgrounded, and the platform does not tell anyone. Re-reading on foreground
6
+ * is the only way a consumer's interface avoids showing a stale answer.
7
+ *
8
+ * Runs last (order 30): permission state does not affect what the earlier steps
9
+ * expand or re-anchor, and reading it after they have run means the report
10
+ * reflects the device as the cycle leaves it.
11
+ */
12
+
13
+ import type { MaintenanceStep, MaintenanceContext, StepOutcome } from './cycle';
14
+ import type { Permissions } from '../permission';
15
+
16
+ export const PERMISSION_REFRESH_ORDER = 30;
17
+
18
+ export interface PermissionRefreshDependencies {
19
+ readonly permissions: Permissions;
20
+ }
21
+
22
+ export function createPermissionRefreshStep(
23
+ deps: PermissionRefreshDependencies,
24
+ ): MaintenanceStep {
25
+ return {
26
+ name: 'permission-refresh',
27
+ order: PERMISSION_REFRESH_ORDER,
28
+
29
+ async run(_context: MaintenanceContext): Promise<StepOutcome> {
30
+ const { changed } = await deps.permissions.refresh();
31
+
32
+ return {
33
+ // Reading permission is not a platform mutation, so it never counts
34
+ // toward the zero-operation guarantee.
35
+ operationCount: 0,
36
+ permissionChanged: changed,
37
+ cancelledIds: [],
38
+ };
39
+ },
40
+ };
41
+ }