Skip to content

Commit a60cc71

Browse files
authored
refactor: remove DocImpl dependencies from runner (#1450)
- Replace DocImpl-based cancellation tracking with URI-based tracking - Update stop() method to work with Cell<T> instead of DocImpl<T> - Change resultRef type from DocImpl-based to SigilLink - Add URI type export to storage interface - Remove unused ReactivityLog import - Reorder imports for better organization
1 parent 9e9413e commit a60cc71

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

packages/runner/src/runner.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { refer } from "merkle-reference";
1+
import { refer } from "merkle-reference/json";
22
import { isObject, isRecord, type Mutable } from "@commontools/utils/types";
33
import {
44
isModule,
@@ -20,9 +20,8 @@ import {
2020
pushFrameFromCause,
2121
recipeFromFrame,
2222
} from "./builder/recipe.ts";
23-
import { type DocImpl, isDoc } from "./doc.ts";
2423
import { type Cell } from "./cell.ts";
25-
import { type Action, type ReactivityLog } from "./scheduler.ts";
24+
import { type Action } from "./scheduler.ts";
2625
import { diffAndUpdate } from "./data-updating.ts";
2726
import {
2827
findAllWriteRedirectCells,
@@ -40,15 +39,19 @@ import {
4039
} from "./link-utils.ts";
4140
import { sendValueToBinding } from "./recipe-binding.ts";
4241
import { type AddCancel, type Cancel, useCancelGroup } from "./cancel.ts";
43-
import "./builtins/index.ts";
4442
import { LINK_V1_TAG, SigilLink } from "./sigil-types.ts";
4543
import type { IRunner, IRuntime } from "./runtime.ts";
46-
import type { IExtendedStorageTransaction } from "./storage/interface.ts";
44+
import type {
45+
IExtendedStorageTransaction,
46+
MemorySpace,
47+
URI,
48+
} from "./storage/interface.ts";
4749
import { ignoreReadForScheduling } from "./scheduler.ts";
4850
import { FunctionCache } from "./function-cache.ts";
51+
import "./builtins/index.ts";
4952

5053
export class Runner implements IRunner {
51-
readonly cancels = new WeakMap<DocImpl<any>, Cancel>();
54+
readonly cancels = new Map<`${MemorySpace}/${URI}`, Cancel>();
5255
private allCancels = new Set<Cancel>();
5356
private functionCache = new FunctionCache();
5457

@@ -100,7 +103,7 @@ export class Runner implements IRunner {
100103
spell?: SigilLink;
101104
argument?: T;
102105
internal?: JSONValue;
103-
resultRef: { cell: DocImpl<R>; path: PropertyKey[] };
106+
resultRef: SigilLink;
104107
};
105108

106109
let processCell: Cell<ProcessCellData>;
@@ -167,7 +170,7 @@ export class Runner implements IRunner {
167170
recipe,
168171
}, tx);
169172

170-
if (this.cancels.has(resultCell.getDoc())) {
173+
if (this.cancels.has(this.getDocKey(resultCell))) {
171174
// If it's already running and no new recipe or argument are given,
172175
// we are just returning the result doc
173176
if (
@@ -187,7 +190,7 @@ export class Runner implements IRunner {
187190

188191
// Keep track of subscriptions to cancel them later
189192
const [cancel, addCancel] = useCancelGroup();
190-
this.cancels.set(resultCell.getDoc(), cancel);
193+
this.cancels.set(this.getDocKey(resultCell), cancel);
191194
this.allCancels.add(cancel);
192195

193196
// If the bindings are a cell, doc or doc link, convert them to an alias
@@ -330,6 +333,11 @@ export class Runner implements IRunner {
330333
: resultCell;
331334
}
332335

336+
private getDocKey(cell: Cell<any>): `${MemorySpace}/${URI}` {
337+
const { space, id } = cell.getAsNormalizedFullLink();
338+
return `${space}/${id}`;
339+
}
340+
333341
private async syncCellsForRunningRecipe(
334342
resultCell: Cell<any>,
335343
recipe: Module | Recipe,
@@ -405,12 +413,10 @@ export class Runner implements IRunner {
405413
*
406414
* @param resultCell - The result doc or cell to stop.
407415
*/
408-
stop<T>(resultCell: DocImpl<T> | Cell<T>): void {
409-
const doc = isDoc(resultCell)
410-
? resultCell
411-
: (resultCell as Cell<T>).getDoc();
412-
this.cancels.get(doc)?.();
413-
this.cancels.delete(doc);
416+
stop<T>(resultCell: Cell<T>): void {
417+
const key = this.getDocKey(resultCell);
418+
this.cancels.get(key)?.();
419+
this.cancels.delete(key);
414420
}
415421

416422
stopAll(): void {
@@ -477,7 +483,10 @@ export class Runner implements IRunner {
477483
);
478484
this.discoverAndCacheFunctionsFromModule(referencedModule);
479485
} catch (error) {
480-
console.warn(`Failed to resolve module reference for implementation "${module.implementation}":`, error);
486+
console.warn(
487+
`Failed to resolve module reference for implementation "${module.implementation}":`,
488+
error,
489+
);
481490
}
482491
break;
483492
}
@@ -946,7 +955,7 @@ export class Runner implements IRunner {
946955
// TODO(seefeld): Make sure to not cancel after a recipe is elevated to a
947956
// charm, e.g. via navigateTo. Nothing is cancelling right now, so leaving
948957
// this as TODO.
949-
addCancel(this.cancels.get(resultCell.getSourceCell()!.getDoc()));
958+
addCancel(this.cancels.get(this.getDocKey(resultCell.getSourceCell()!)));
950959
}
951960
}
952961

packages/runner/src/storage/interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export type {
3333
SchemaContext,
3434
State,
3535
Unit,
36+
URI,
3637
};
3738

3839
/**

0 commit comments

Comments
 (0)