@shivaedev/contract 0.50.0 → 0.52.0
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/CHANGELOG.md +16 -0
- package/README.md +114 -80
- package/dist/adapters/activityState.d.ts +1 -0
- package/dist/adapters/activityState.d.ts.map +1 -1
- package/dist/adapters/activityState.js +7 -1
- package/dist/adapters/activityState.js.map +1 -1
- package/dist/clientActivities.d.ts +2 -2
- package/dist/clientActivities.d.ts.map +1 -1
- package/dist/clientActivities.js +9 -6
- package/dist/clientActivities.js.map +1 -1
- package/dist/clientOperations.d.ts.map +1 -1
- package/dist/clientOperations.js +5 -1
- package/dist/clientOperations.js.map +1 -1
- package/dist/commandSignal.d.ts.map +1 -1
- package/dist/commandSignal.js +17 -13
- package/dist/commandSignal.js.map +1 -1
- package/dist/connectionReadiness.js +1 -1
- package/dist/connectionReadiness.js.map +1 -1
- package/dist/live.d.ts.map +1 -1
- package/dist/live.js +16 -6
- package/dist/live.js.map +1 -1
- package/dist/liveHintQueue.d.ts.map +1 -1
- package/dist/liveHintQueue.js +19 -14
- package/dist/liveHintQueue.js.map +1 -1
- package/dist/querySubscription.js +5 -1
- package/dist/querySubscription.js.map +1 -1
- package/dist/reflection.d.ts.map +1 -1
- package/dist/reflection.js +3 -3
- package/dist/reflection.js.map +1 -1
- package/dist/runProof.d.ts +2 -1
- package/dist/runProof.d.ts.map +1 -1
- package/dist/servedOperation.d.ts.map +1 -1
- package/dist/servedOperation.js +2 -1
- package/dist/servedOperation.js.map +1 -1
- package/dist/socketProtocol.js +1 -1
- package/dist/socketProtocol.js.map +1 -1
- package/dist/staleWrite.d.ts +10 -0
- package/dist/staleWrite.d.ts.map +1 -0
- package/dist/staleWrite.js +14 -0
- package/dist/staleWrite.js.map +1 -0
- package/dist/warmQueries.d.ts +3 -0
- package/dist/warmQueries.d.ts.map +1 -0
- package/dist/warmQueries.js +4 -0
- package/dist/warmQueries.js.map +1 -0
- package/package.json +6 -6
- package/src/adapters/activityState.ts +7 -1
- package/src/clientActivities.ts +9 -6
- package/src/clientOperations.ts +5 -1
- package/src/commandSignal.ts +16 -12
- package/src/connectionReadiness.ts +1 -1
- package/src/live.ts +19 -6
- package/src/liveHintQueue.ts +20 -14
- package/src/querySubscription.ts +7 -1
- package/src/reflection.ts +88 -86
- package/src/runProof.ts +3 -1
- package/src/servedOperation.ts +2 -1
- package/src/socketProtocol.ts +1 -1
- package/src/staleWrite.ts +23 -0
- package/src/warmQueries.ts +5 -0
package/src/clientActivities.ts
CHANGED
|
@@ -9,7 +9,7 @@ type ActivityTracker = ReturnType<typeof makeActivity>;
|
|
|
9
9
|
export const clientActivities = new WeakMap<object, ActivityTracker>();
|
|
10
10
|
|
|
11
11
|
export function makeActivity(factory: Atom.RuntimeFactory) {
|
|
12
|
-
const { activity, answered, change, hold, setConnection } = activityState();
|
|
12
|
+
const { activity, answered, change, hold, setConnection, warming } = activityState();
|
|
13
13
|
const connection = clientConnection(factory, setConnection, activity);
|
|
14
14
|
function begin(key: "rpcCalls" | "commands" | "unreflectedCommands") {
|
|
15
15
|
change(key, 1);
|
|
@@ -29,21 +29,22 @@ export function makeActivity(factory: Atom.RuntimeFactory) {
|
|
|
29
29
|
(end) => Effect.sync(end),
|
|
30
30
|
);
|
|
31
31
|
}
|
|
32
|
-
function counter(
|
|
32
|
+
function counter(apply: (delta: number) => void) {
|
|
33
33
|
let pending = false;
|
|
34
34
|
function set(next: boolean) {
|
|
35
35
|
if (pending !== next) {
|
|
36
36
|
pending = next;
|
|
37
|
-
|
|
37
|
+
apply(next ? 1 : -1);
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
return { release: () => set(false), set };
|
|
41
41
|
}
|
|
42
|
-
function watch<A, E>(atom: Atom.Atom<AsyncResult.AsyncResult<A, E
|
|
42
|
+
function watch<A, E>(atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>, warm: (get: Atom.AtomContext) => boolean) {
|
|
43
43
|
const watched = wrapAtom(atom, (get) => {
|
|
44
44
|
get.mount(connection.status);
|
|
45
|
-
const refreshing = counter("refreshingAtoms");
|
|
46
|
-
const initial = counter("initialQueries");
|
|
45
|
+
const refreshing = counter((delta) => change("refreshingAtoms", delta));
|
|
46
|
+
const initial = counter((delta) => change("initialQueries", delta));
|
|
47
|
+
const warmInitial = counter(warming);
|
|
47
48
|
let received = false;
|
|
48
49
|
let active = true;
|
|
49
50
|
let published = false;
|
|
@@ -54,6 +55,7 @@ export function makeActivity(factory: Atom.RuntimeFactory) {
|
|
|
54
55
|
const first = !(received || AsyncResult.isInitial(result));
|
|
55
56
|
received ||= first;
|
|
56
57
|
initial.set(!received);
|
|
58
|
+
warmInitial.set(!received && warm(get));
|
|
57
59
|
refreshing.set(result.waiting || AsyncResult.isInitial(result));
|
|
58
60
|
if (first) {
|
|
59
61
|
answered();
|
|
@@ -62,6 +64,7 @@ export function makeActivity(factory: Atom.RuntimeFactory) {
|
|
|
62
64
|
get.addFinalizer(() => {
|
|
63
65
|
active = false;
|
|
64
66
|
refreshing.release();
|
|
67
|
+
warmInitial.release();
|
|
65
68
|
initial.release();
|
|
66
69
|
});
|
|
67
70
|
function publish(result: AsyncResult.AsyncResult<A, E>) {
|
package/src/clientOperations.ts
CHANGED
|
@@ -21,6 +21,7 @@ import { shareQuerySignals } from "#querySignals.ts";
|
|
|
21
21
|
import { type CommandObserver, reflectCommand } from "#reflectCommand.ts";
|
|
22
22
|
import { retainQuery } from "#retainQuery.ts";
|
|
23
23
|
import { subjectSerializationKey, withSubject } from "#subjectHeader.ts";
|
|
24
|
+
import { warmQueries } from "#warmQueries.ts";
|
|
24
25
|
|
|
25
26
|
// `runtime.fn(api.save.run)` passes its function context as the second argument, so only an options object selects a subject.
|
|
26
27
|
function runSubject(options: unknown): string | undefined {
|
|
@@ -71,7 +72,10 @@ export function clientOperations(
|
|
|
71
72
|
(source) => ordinaryClient.runtime.factory.withReactivity(clientReadKeys(dependencies, owner?.context))(source),
|
|
72
73
|
headers,
|
|
73
74
|
);
|
|
74
|
-
|
|
75
|
+
function warm(get: Atom.AtomContext): boolean {
|
|
76
|
+
return get.once(warmQueries).has(tracked);
|
|
77
|
+
}
|
|
78
|
+
const tracked = retainQuery(ordinaryClient.runtime, clientActivities.get(ordinaryClient)?.watch(result, warm) ?? result);
|
|
75
79
|
shareQuerySignals(tracked, result);
|
|
76
80
|
cached.set(atom, tracked);
|
|
77
81
|
return tracked;
|
package/src/commandSignal.ts
CHANGED
|
@@ -8,20 +8,24 @@ export const CommandSignal = Context.Reference<(state: CommandState) => void>("@
|
|
|
8
8
|
|
|
9
9
|
export const untilReflected = Effect.fnUntraced(function* <A, E, R>(command: Effect.Effect<A, E, R>) {
|
|
10
10
|
const outer = yield* CommandSignal;
|
|
11
|
-
const reflected = Latch.
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
if (
|
|
21
|
-
|
|
11
|
+
const reflected = yield* Latch.make(true);
|
|
12
|
+
// The command reports its states synchronously, so the signal that counts them is built as a side effect.
|
|
13
|
+
const signal = Effect.sync(() => {
|
|
14
|
+
let accepted = 0;
|
|
15
|
+
return (state: CommandState) => {
|
|
16
|
+
outer(state);
|
|
17
|
+
if (state === "accepted") {
|
|
18
|
+
accepted += 1;
|
|
19
|
+
reflected.closeUnsafe();
|
|
20
|
+
} else if (state === "reflected") {
|
|
21
|
+
accepted -= 1;
|
|
22
|
+
if (accepted === 0) {
|
|
23
|
+
reflected.openUnsafe();
|
|
24
|
+
}
|
|
22
25
|
}
|
|
23
|
-
}
|
|
26
|
+
};
|
|
24
27
|
});
|
|
28
|
+
const result = yield* Effect.provideServiceEffect(command, CommandSignal, signal);
|
|
25
29
|
yield* reflected.await;
|
|
26
30
|
return result;
|
|
27
31
|
});
|
|
@@ -4,7 +4,7 @@ import { SocketOpenError } from "effect/socket/Socket";
|
|
|
4
4
|
|
|
5
5
|
export const connectionReadiness = Effect.gen(function* () {
|
|
6
6
|
const clock = yield* Clock.Clock;
|
|
7
|
-
const ready = Latch.
|
|
7
|
+
const ready = yield* Latch.make(true);
|
|
8
8
|
return {
|
|
9
9
|
close: Effect.asVoid(ready.close),
|
|
10
10
|
isReady: () => ready.isOpen(),
|
package/src/live.ts
CHANGED
|
@@ -33,6 +33,17 @@ export function isLiveConnection(atom: Atom.Atom<unknown>): boolean {
|
|
|
33
33
|
return connections.has(atom);
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
// The registry calls its subscribers synchronously, so subscribing is a side effect.
|
|
37
|
+
function wakeOnChange(get: Atom.AtomContext, atom: Atom.Atom<unknown>, wakes: () => boolean, woken: Queue.Queue<void>) {
|
|
38
|
+
return Effect.sync(() =>
|
|
39
|
+
get.subscribe(atom, () => {
|
|
40
|
+
if (wakes()) {
|
|
41
|
+
Queue.offerUnsafe(woken, undefined);
|
|
42
|
+
}
|
|
43
|
+
}),
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
36
47
|
export function live<R, E, TRuntimeError>(runtime: Atom.AtomRuntime<R, TRuntimeError>, options: LiveOptions<E, R>) {
|
|
37
48
|
const paused = Atom.make(false);
|
|
38
49
|
const status = Atom.make<LiveStatus<E>>({ connection: "connecting", failure: Option.none(), needsResync: true, pending: 0 });
|
|
@@ -51,14 +62,16 @@ export function live<R, E, TRuntimeError>(runtime: Atom.AtomRuntime<R, TRuntimeE
|
|
|
51
62
|
update({ connection: "connecting", failure: Option.none(), needsResync: true, pending: 0 });
|
|
52
63
|
}
|
|
53
64
|
});
|
|
65
|
+
function resyncsOnResume() {
|
|
66
|
+
if (get.once(paused) || get.once(status).connection === "failed") {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
update({ needsResync: true });
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
54
72
|
if (options.resume !== undefined) {
|
|
55
73
|
get.once(options.resume);
|
|
56
|
-
get
|
|
57
|
-
if (!get.once(paused) && get.once(status).connection !== "failed") {
|
|
58
|
-
update({ needsResync: true });
|
|
59
|
-
Queue.offerUnsafe(resumed, undefined);
|
|
60
|
-
}
|
|
61
|
-
});
|
|
74
|
+
yield* wakeOnChange(get, options.resume, resyncsOnResume, resumed);
|
|
62
75
|
}
|
|
63
76
|
const attempt = liveAttempt(options.stream, {
|
|
64
77
|
delivered: () => update({ connection: "live", failure: Option.none(), needsResync: false, pending: 0 }),
|
package/src/liveHintQueue.ts
CHANGED
|
@@ -4,21 +4,27 @@ import type { LiveHint } from "#live.ts";
|
|
|
4
4
|
// A subscription's hint also carries the sync tokens that arrived behind its keys; they never reach a hint stream's wire.
|
|
5
5
|
export type SubscriptionHint = LiveHint & { readonly synced?: readonly number[] };
|
|
6
6
|
|
|
7
|
+
function folded(pending: SubscriptionHint | undefined, hint: SubscriptionHint, count: number): SubscriptionHint {
|
|
8
|
+
const ids = [...new Set([...(pending?.synced ?? []), ...(hint.synced ?? [])])];
|
|
9
|
+
const synced = ids.length === 0 ? {} : { synced: ids };
|
|
10
|
+
if (pending?._tag === "Resync" || hint._tag === "Resync" || count > 256) {
|
|
11
|
+
return { _tag: "Resync", ...synced };
|
|
12
|
+
}
|
|
13
|
+
const keys = new Map([...(pending?.keys ?? []), ...hint.keys].map((key) => [JSON.stringify(key), key]));
|
|
14
|
+
return { _tag: "Changed", keys: [...keys.values()], ...synced };
|
|
15
|
+
}
|
|
16
|
+
|
|
7
17
|
export const liveHintQueue = Effect.gen(function* () {
|
|
8
18
|
const queue = yield* Queue.bounded<SubscriptionHint>(1);
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
const keys = new Map([...(pending?.keys ?? []), ...hint.keys].map((key) => [JSON.stringify(key), key]));
|
|
21
|
-
Queue.offerUnsafe(queue, { _tag: "Changed", keys: [...keys.values()], ...synced });
|
|
22
|
-
}
|
|
19
|
+
// The dispatcher offers synchronously, so a hint folds into the queued one before any fiber can take it.
|
|
20
|
+
const offer = yield* Effect.sync(() => {
|
|
21
|
+
let count = 0;
|
|
22
|
+
return (hint: SubscriptionHint) => {
|
|
23
|
+
const queued = Queue.takeUnsafe(queue);
|
|
24
|
+
const pending = queued?._tag === "Success" ? queued.value : undefined;
|
|
25
|
+
count = pending === undefined ? 1 : count + 1;
|
|
26
|
+
Queue.offerUnsafe(queue, folded(pending, hint, count));
|
|
27
|
+
};
|
|
28
|
+
});
|
|
23
29
|
return { offer, queue };
|
|
24
30
|
});
|
package/src/querySubscription.ts
CHANGED
|
@@ -31,7 +31,13 @@ export function querySubscription<TRpc extends Rpc.Any>(
|
|
|
31
31
|
>;
|
|
32
32
|
export function querySubscription(rpc: Rpc.Any, references: readonly QueryReference[], viewer: Option.Option<Viewer>) {
|
|
33
33
|
return Effect.fnUntraced(function* () {
|
|
34
|
-
const
|
|
34
|
+
const changes = Context.getOption(rpc.annotations, ChangesQueries);
|
|
35
|
+
if (Option.isNone(changes)) {
|
|
36
|
+
return yield* Effect.die(
|
|
37
|
+
new Error(`${rpc._tag} carries no changes queries.\nhelp: Subscribe through the $changes RPC that changesRpc declares.`),
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
const queries = yield* changes.value.resolve(references);
|
|
35
41
|
const prefix = rpc._tag.slice(0, -"$changes".length);
|
|
36
42
|
const policies = yield* Effect.forEach(
|
|
37
43
|
queries,
|
package/src/reflection.ts
CHANGED
|
@@ -14,96 +14,98 @@ interface Wait {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
// One per connection: every command and live stream that shares it folds into the same tokens.
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
17
|
+
// The record is mutable state that commands and streams drive synchronously, so building it is a side effect.
|
|
18
|
+
export const makeReflection = Effect.flatMap(Queue.sliding<void>(1), (catchUps) =>
|
|
19
|
+
Effect.sync(() => {
|
|
20
|
+
const client = clientId();
|
|
21
|
+
let issued = 0;
|
|
22
|
+
const streams = new Set<FollowedStream>();
|
|
23
|
+
const flights = new Map<number, Set<FollowedStream>>();
|
|
24
|
+
const waits = new Set<Wait>();
|
|
24
25
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
function release(covers: (wait: Wait, stream: FollowedStream) => boolean, stream?: FollowedStream) {
|
|
30
|
-
for (const wait of [...waits]) {
|
|
31
|
-
const covered = [...wait.streams].filter((waited) => (stream === undefined || waited === stream) && covers(wait, waited));
|
|
32
|
-
for (const waited of covered) {
|
|
33
|
-
wait.streams.delete(waited);
|
|
34
|
-
}
|
|
35
|
-
if (wait.streams.size === 0 && waits.delete(wait)) {
|
|
36
|
-
wait.reflected();
|
|
37
|
-
}
|
|
26
|
+
// A stream armed after the command's token was issued may have registered behind it, so only a later token proves it caught up.
|
|
27
|
+
function uncertain(wait: Wait, stream: FollowedStream) {
|
|
28
|
+
return stream.armedAt !== undefined && (wait.own === undefined || stream.armedAt >= wait.own);
|
|
38
29
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
return {
|
|
50
|
-
abandon: () => release(uncertain),
|
|
51
|
-
accepted: (own: number | undefined, reflected: () => void) => {
|
|
52
|
-
const confirmed = own === undefined ? undefined : flights.get(own);
|
|
53
|
-
if (own !== undefined) {
|
|
54
|
-
flights.delete(own);
|
|
55
|
-
}
|
|
56
|
-
const waited = [...streams].filter((stream) => stream.supported !== false && confirmed?.has(stream) !== true);
|
|
57
|
-
if (waited.length === 0) {
|
|
58
|
-
reflected();
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
const wait: Wait = { after: issued, own, reflected, streams: new Set(waited) };
|
|
62
|
-
waits.add(wait);
|
|
63
|
-
if (waited.some((stream) => uncertain(wait, stream))) {
|
|
64
|
-
Queue.offerUnsafe(catchUps, undefined);
|
|
65
|
-
}
|
|
66
|
-
},
|
|
67
|
-
armed: (stream: FollowedStream) => {
|
|
68
|
-
stream.supported = true;
|
|
69
|
-
stream.armedAt = issued;
|
|
70
|
-
if ([...waits].some((wait) => wait.streams.has(stream))) {
|
|
71
|
-
Queue.offerUnsafe(catchUps, undefined);
|
|
72
|
-
}
|
|
73
|
-
},
|
|
74
|
-
catchUps,
|
|
75
|
-
client,
|
|
76
|
-
confirmed: (stream: FollowedStream, ids: readonly number[]) => {
|
|
77
|
-
for (const id of ids) {
|
|
78
|
-
flights.get(id)?.add(stream);
|
|
30
|
+
function release(covers: (wait: Wait, stream: FollowedStream) => boolean, stream?: FollowedStream) {
|
|
31
|
+
for (const wait of [...waits]) {
|
|
32
|
+
const covered = [...wait.streams].filter((waited) => (stream === undefined || waited === stream) && covers(wait, waited));
|
|
33
|
+
for (const waited of covered) {
|
|
34
|
+
wait.streams.delete(waited);
|
|
35
|
+
}
|
|
36
|
+
if (wait.streams.size === 0 && waits.delete(wait)) {
|
|
37
|
+
wait.reflected();
|
|
38
|
+
}
|
|
79
39
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
40
|
+
}
|
|
41
|
+
function token() {
|
|
42
|
+
issued += 1;
|
|
43
|
+
return issued;
|
|
44
|
+
}
|
|
45
|
+
function flight() {
|
|
46
|
+
const own = token();
|
|
47
|
+
flights.set(own, new Set());
|
|
48
|
+
return own;
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
abandon: () => release(uncertain),
|
|
52
|
+
accepted: (own: number | undefined, reflected: () => void) => {
|
|
53
|
+
const confirmed = own === undefined ? undefined : flights.get(own);
|
|
54
|
+
if (own !== undefined) {
|
|
55
|
+
flights.delete(own);
|
|
56
|
+
}
|
|
57
|
+
const waited = [...streams].filter((stream) => stream.supported !== false && confirmed?.has(stream) !== true);
|
|
58
|
+
if (waited.length === 0) {
|
|
59
|
+
reflected();
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const wait: Wait = { after: issued, own, reflected, streams: new Set(waited) };
|
|
63
|
+
waits.add(wait);
|
|
64
|
+
if (waited.some((stream) => uncertain(wait, stream))) {
|
|
65
|
+
Queue.offerUnsafe(catchUps, undefined);
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
armed: (stream: FollowedStream) => {
|
|
69
|
+
stream.supported = true;
|
|
70
|
+
stream.armedAt = issued;
|
|
71
|
+
if ([...waits].some((wait) => wait.streams.has(stream))) {
|
|
72
|
+
Queue.offerUnsafe(catchUps, undefined);
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
catchUps,
|
|
76
|
+
client,
|
|
77
|
+
confirmed: (stream: FollowedStream, ids: readonly number[]) => {
|
|
78
|
+
for (const id of ids) {
|
|
79
|
+
flights.get(id)?.add(stream);
|
|
80
|
+
}
|
|
81
|
+
release((wait) => ids.some((id) => id === wait.own || id > wait.after), stream);
|
|
82
|
+
},
|
|
83
|
+
data: (stream: FollowedStream) => {
|
|
84
|
+
if (stream.supported === undefined) {
|
|
85
|
+
stream.supported = false;
|
|
86
|
+
release(() => true, stream);
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
detach: (stream: FollowedStream) => {
|
|
90
|
+
streams.delete(stream);
|
|
85
91
|
release(() => true, stream);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
send: (piggyback: boolean) => (piggyback && [...streams].some((stream) => stream.supported !== false) ? flight() : undefined),
|
|
104
|
-
token,
|
|
105
|
-
};
|
|
106
|
-
});
|
|
92
|
+
},
|
|
93
|
+
failed: (own: number | undefined) => {
|
|
94
|
+
if (own !== undefined) {
|
|
95
|
+
flights.delete(own);
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
flight,
|
|
99
|
+
opening: (stream: FollowedStream) => {
|
|
100
|
+
stream.armedAt = undefined;
|
|
101
|
+
stream.supported = undefined;
|
|
102
|
+
streams.add(stream);
|
|
103
|
+
},
|
|
104
|
+
send: (piggyback: boolean) => (piggyback && [...streams].some((stream) => stream.supported !== false) ? flight() : undefined),
|
|
105
|
+
token,
|
|
106
|
+
};
|
|
107
|
+
}),
|
|
108
|
+
);
|
|
107
109
|
|
|
108
110
|
export type Reflection = Effect.Success<typeof makeReflection>;
|
|
109
111
|
|
package/src/runProof.ts
CHANGED
|
@@ -3,12 +3,14 @@ import type { InfrastructureFailure } from "@shivaedev/errors/infrastructure.ts"
|
|
|
3
3
|
import type { InternalServerError, ServiceUnavailable } from "@shivaedev/errors/taxonomy.ts";
|
|
4
4
|
import type { QueryReadDenied } from "#readPolicyErrors.ts";
|
|
5
5
|
import type { TaggedRejection } from "#rejection.ts";
|
|
6
|
+
import type { Answered } from "#staleWrite.ts";
|
|
6
7
|
|
|
7
8
|
export type RunBody<TPayload, TYielded, A> = (payload: TPayload) => Generator<TYielded, A, never>;
|
|
8
9
|
|
|
9
10
|
export type ErrorOf<TYielded> = [TYielded] extends [never] ? never : [TYielded] extends [Effect.Effect<unknown, infer E, unknown>] ? E : never;
|
|
10
11
|
export type ServicesOf<TYielded> = [TYielded] extends [never] ? never : [TYielded] extends [Effect.Effect<unknown, unknown, infer R>] ? R : never;
|
|
11
|
-
|
|
12
|
+
// Run answers a stale versioned write with Conflict, so its callers and its wire see that rejection.
|
|
13
|
+
export type RunEffect<TYielded, A> = [A] extends [unknown] ? Effect.Effect<A, Answered<ErrorOf<TYielded>>, ServicesOf<TYielded>> : never;
|
|
12
14
|
|
|
13
15
|
export interface ClassRejections {
|
|
14
16
|
readonly [tag: string]: TaggedRejection;
|
package/src/servedOperation.ts
CHANGED
|
@@ -8,6 +8,7 @@ import type { QueryServerReads } from "#QueryServerReads.ts";
|
|
|
8
8
|
import type { QueryReadPolicy, Viewer } from "#readPolicy.ts";
|
|
9
9
|
import type { RejectionSpecs } from "#rejection.ts";
|
|
10
10
|
import type { ErrorOf, RunBody, RunCheck, RunEffect, ServicesOf, SpecsOf } from "#runProof.ts";
|
|
11
|
+
import { answerStaleWrite } from "#staleWrite.ts";
|
|
11
12
|
|
|
12
13
|
export interface ServedQuery<
|
|
13
14
|
TName extends string,
|
|
@@ -141,5 +142,5 @@ export function invalidatesOf(declaration: LooseCommand): Invalidates {
|
|
|
141
142
|
}
|
|
142
143
|
|
|
143
144
|
export function runOf({ run }: { readonly run?: RunGenerator | undefined }) {
|
|
144
|
-
return run === undefined ? {} : { run: Effect.fnUntraced(run) };
|
|
145
|
+
return run === undefined ? {} : { run: Effect.fnUntraced(run, answerStaleWrite) };
|
|
145
146
|
}
|
package/src/socketProtocol.ts
CHANGED
|
@@ -62,7 +62,7 @@ const protocols = Atom.family(
|
|
|
62
62
|
const clock = yield* SocketClock;
|
|
63
63
|
let memo = socketMemoMaps.get(clock);
|
|
64
64
|
if (memo === undefined) {
|
|
65
|
-
memo = Layer.
|
|
65
|
+
memo = yield* Layer.makeMemoMap;
|
|
66
66
|
socketMemoMaps.set(clock, memo);
|
|
67
67
|
}
|
|
68
68
|
const protocol = managed.pipe(Layer.provide(Layer.succeed(Clock.Clock, clock)));
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Effect, Predicate } from "effect";
|
|
2
|
+
import { Conflict } from "@shivaedev/errors/taxonomy.ts";
|
|
3
|
+
|
|
4
|
+
// The OptimisticConcurrencyError of @shivaedev/sql repositories, known by its tag as catchTag knows it, since this
|
|
5
|
+
// browser-safe package reaches no server package.
|
|
6
|
+
const STALE_WRITE = "OptimisticConcurrencyError";
|
|
7
|
+
|
|
8
|
+
export interface StaleWrite {
|
|
9
|
+
readonly _tag: typeof STALE_WRITE;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// A versioned write that lost its race means the caller acted on an old read, which the shared Conflict tells every client.
|
|
13
|
+
export type Answered<E> = E extends StaleWrite ? Conflict : E;
|
|
14
|
+
|
|
15
|
+
function staleConflict(cause: unknown) {
|
|
16
|
+
const rejection = new Conflict({ message: "This was changed or deleted since you loaded it. Refresh and try again." });
|
|
17
|
+
Object.defineProperty(rejection, "cause", { configurable: true, enumerable: false, value: cause });
|
|
18
|
+
return Effect.fail(rejection);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function answerStaleWrite(effect: Effect.Effect<unknown, unknown, unknown>) {
|
|
22
|
+
return Effect.catchIf(effect, (error) => Predicate.isTagged(error, STALE_WRITE), staleConflict);
|
|
23
|
+
}
|