Skip to content

Commit 0df5fa8

Browse files
authored
Clean up Cause objects to generally be strings. (#1904)
* Clean up Cause objects to generally be strings. - Change The and Entity types in many places to the more meaningful URI and MIME. - Remove broken support for queries with "is" but no cause. * Removed stray "The" instead of "MIME"
1 parent 43096d2 commit 0df5fa8

File tree

15 files changed

+247
-202
lines changed

15 files changed

+247
-202
lines changed

packages/memory/changes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
AssertFact,
33
Assertion,
4-
Cause,
4+
CauseString,
55
Changes,
66
ClaimFact,
77
Fact,
@@ -22,7 +22,7 @@ export const from = <T extends Statement>(statements: Iterable<T>) => {
2222
}
2323

2424
return changes as T extends Assertion<infer The, infer Of, infer Is>
25-
? { [of in Of]: { [the in The]: { [cause: Cause]: { is: Is } } } }
25+
? { [of in Of]: { [the in The]: { [cause in CauseString]: { is: Is } } } }
2626
: T extends Fact<infer The, infer Of, infer Is> ? Changes<The, Of, Is>
2727
: T extends Statement<infer The, infer Of, infer Is> ? Changes<The, Of, Is>
2828
: never;

packages/memory/consumer.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type {
33
Authorization,
44
AuthorizationError,
55
Await,
6-
Cause,
6+
CauseString,
77
Clock,
88
Command,
99
ConnectionError,
@@ -14,14 +14,14 @@ import type {
1414
ConsumerResultFor,
1515
DID,
1616
EnhancedCommit,
17-
Entity,
1817
Fact,
1918
FactSelection,
2019
InferOf,
2120
Invocation,
2221
InvocationURL,
2322
JSONValue,
2423
MemorySpace,
24+
MIME,
2525
OfTheCause,
2626
Proto,
2727
Protocol,
@@ -39,13 +39,14 @@ import type {
3939
SchemaSelector,
4040
Seconds,
4141
Select,
42+
SelectAll,
4243
Selection,
4344
Selector,
4445
Signer,
45-
The,
4646
Transaction,
4747
TransactionResult,
4848
UCAN,
49+
URI,
4950
UTCUnixTimestampInSeconds,
5051
} from "./interface.ts";
5152
import { fromJSON, refer } from "./reference.ts";
@@ -367,18 +368,22 @@ class MemorySpaceConsumerSession<Space extends MemorySpace>
367368
}
368369

369370
private static asSelectSchema(queryArg: QueryArgs): SchemaQueryArgs {
370-
const selectSchema: OfTheCause<SchemaPathSelector> = {};
371+
const selectSchema: Select<
372+
URI,
373+
Select<MIME, Select<CauseString, SchemaPathSelector>>
374+
> = {};
371375
for (const [of, attributes] of Object.entries(queryArg.select)) {
372-
const entityEntry: Select<The, Select<Cause, SchemaPathSelector>> = {};
373-
selectSchema[of as Entity] = entityEntry;
376+
const entityEntry: Select<MIME, Select<CauseString, SchemaPathSelector>> =
377+
{};
378+
selectSchema[of as URI | SelectAll] = entityEntry;
374379
let attrEntries = Object.entries(attributes);
375380
// A Selector may not have a "the", but SchemaSelector needs all three levels
376381
if (attrEntries.length === 0) {
377382
attrEntries = [["_", {}]];
378383
}
379384
for (const [the, causes] of attrEntries) {
380-
const attributeEntry: Select<Cause, SchemaPathSelector> = {};
381-
entityEntry[the] = attributeEntry;
385+
const attributeEntry: Select<CauseString, SchemaPathSelector> = {};
386+
entityEntry[the as MIME | SelectAll] = attributeEntry;
382387
// A Selector may not have a cause, but SchemaSelector needs all three levels
383388
let causeEntries = Object.entries(causes);
384389
if (causeEntries.length === 0) {
@@ -390,7 +395,7 @@ class MemorySpaceConsumerSession<Space extends MemorySpace>
390395
schemaContext: SchemaNone,
391396
...selector.is ? { is: selector.is } : {},
392397
};
393-
attributeEntry[cause] = causeEntry;
398+
attributeEntry[cause as CauseString | SelectAll] = causeEntry;
394399
}
395400
}
396401
}
@@ -412,7 +417,7 @@ interface Job<Ability, Protocol extends Proto> {
412417
perform(effect: ConsumerEffectFor<Ability, Protocol>): void;
413418
}
414419

415-
class ConsumerInvocation<Ability extends The, Protocol extends Proto> {
420+
class ConsumerInvocation<Ability extends string, Protocol extends Proto> {
416421
promise: Promise<ConsumerResultFor<Ability, Protocol>>;
417422

418423
return: (input: ConsumerResultFor<Ability, Protocol>) => boolean;
@@ -421,7 +426,7 @@ class ConsumerInvocation<Ability extends The, Protocol extends Proto> {
421426

422427
#reference: Reference<Invocation>;
423428

424-
static create<Ability extends The, Protocol extends Proto>(
429+
static create<Ability extends string, Protocol extends Proto>(
425430
as: DID,
426431
{ cmd, sub, args, nonce }: Command<Ability, InferOf<Protocol>>,
427432
time: UTCUnixTimestampInSeconds,
@@ -605,7 +610,7 @@ class QuerySubscriptionInvocation<
605610
controller:
606611
| undefined
607612
| ReadableStreamDefaultController<EnhancedCommit<Space>>;
608-
patterns: { the?: The; of?: Entity; cause?: Cause }[];
613+
patterns: { the?: MIME; of?: URI; cause?: CauseString }[];
609614

610615
selection: Selection<Space>;
611616
constructor(public query: QueryView<Space, MemoryProtocol>) {
@@ -654,7 +659,7 @@ class QuerySubscriptionInvocation<
654659
const fact = toRevision(commit.commit);
655660

656661
const { the, of, is } = fact;
657-
const cause = fact.cause.toString();
662+
const cause = fact.cause.toString() as CauseString;
658663
const { transaction, since } = is;
659664
const matchCommit = this.patterns.some((pattern) =>
660665
(!pattern.of || pattern.of === of) &&
@@ -666,8 +671,10 @@ class QuerySubscriptionInvocation<
666671
// Update the main application/commit+json record for the space
667672
setRevision(differential, of, the, cause, { is, since });
668673
}
669-
for (const [of, attributes] of Object.entries(transaction.args.changes)) {
670-
for (const [the, changes] of Object.entries(attributes)) {
674+
for (const [k1, attributes] of Object.entries(transaction.args.changes)) {
675+
const of = k1 as URI;
676+
for (const [k2, changes] of Object.entries(attributes)) {
677+
const the = k2 as MIME;
671678
const causeEntries = Object.entries(changes);
672679
if (causeEntries.length === 0) {
673680
// A classified object will not have a cause/change pair
@@ -676,13 +683,14 @@ class QuerySubscriptionInvocation<
676683
(!pattern.the || pattern.the === the) && !pattern.cause
677684
);
678685
if (matchDoc) {
679-
setEmptyObj(differential, of as Entity, the);
686+
setEmptyObj(differential, of, the);
680687
}
681688
} else {
682-
const [[cause, change]] = causeEntries;
689+
const [[k3, change]] = causeEntries;
690+
const cause = k3 as CauseString;
683691
if (change !== true) {
684692
const state = Object.entries(
685-
selection?.[of as Entity]?.[the] ?? {},
693+
selection?.[of]?.[the] ?? {},
686694
);
687695
const [current] = state.length > 0 ? state[0] : [];
688696
if (cause !== current) {
@@ -696,7 +704,7 @@ class QuerySubscriptionInvocation<
696704
const value = change.is
697705
? { is: change.is, since: since }
698706
: { since: since };
699-
setRevision(differential, of as Entity, the, cause, value);
707+
setRevision(differential, of, the, cause, value);
700708
}
701709
}
702710
}

packages/memory/fact.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
import { JSONValue } from "@commontools/runner";
1+
import { JSONValue, URI } from "@commontools/runner";
22
import {
33
Assertion,
4-
Entity,
54
Fact,
65
FactSelection,
76
Invariant,
7+
MIME,
88
Reference,
99
Retraction,
1010
Revision,
1111
State,
12-
The,
1312
Unclaimed,
1413
} from "./interface.ts";
1514
import {
@@ -23,13 +22,13 @@ import {
2322
* Creates an unclaimed fact.
2423
*/
2524
export const unclaimed = (
26-
{ the, of }: { the: The; of: Entity },
25+
{ the, of }: { the: MIME; of: URI },
2726
): Unclaimed => ({
2827
the,
2928
of,
3029
});
3130

32-
export const assert = <Is extends JSONValue, T extends The, Of extends Entity>({
31+
export const assert = <Is extends JSONValue, T extends MIME, Of extends URI>({
3332
the,
3433
of,
3534
is,
@@ -77,14 +76,14 @@ export const claimState = (state: State): Invariant => ({
7776
export const iterate = function* (
7877
selection: FactSelection,
7978
): Iterable<Revision<Fact>> {
80-
for (const [entity, attributes] of Object.entries(selection)) {
79+
for (const [of, attributes] of Object.entries(selection)) {
8180
for (const [the, changes] of Object.entries(attributes)) {
8281
const [change] = Object.entries(changes);
8382
if (change) {
8483
const [cause, { is, since }] = change;
8584
yield {
86-
the,
87-
of: entity as Entity,
85+
the: the as MIME,
86+
of: of as URI,
8887
cause: fromString(cause),
8988
since,
9089
...(is ? { is } : undefined),
@@ -98,8 +97,8 @@ export const iterate = function* (
9897
// conform), and convert it to a proper fact.
9998

10099
export function normalizeFact<
101-
T extends The,
102-
Of extends Entity,
100+
T extends MIME,
101+
Of extends URI,
103102
Is extends JSONValue,
104103
>(
105104
arg: {
@@ -116,8 +115,8 @@ export function normalizeFact<
116115
): Assertion<T, Of, Is>;
117116

118117
export function normalizeFact<
119-
T extends The,
120-
Of extends Entity,
118+
T extends MIME,
119+
Of extends URI,
121120
Is extends JSONValue,
122121
>(
123122
arg: {
@@ -133,8 +132,8 @@ export function normalizeFact<
133132
): Retraction<T, Of, Is>;
134133

135134
export function normalizeFact<
136-
T extends The,
137-
Of extends Entity,
135+
T extends MIME,
136+
Of extends URI,
138137
Is extends JSONValue,
139138
>(
140139
arg: {

0 commit comments

Comments
 (0)