@marimo-team/islands 0.25.3-dev438 → 0.25.3-dev439

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.
@@ -51,15 +51,9 @@ const NO_CLEARED: ReadonlyMap<Protocol.CellId, ClearedOutput> = new Map();
51
51
  interface ShownNotebook {
52
52
  readonly replica: NotebookReplica;
53
53
  readonly id: Protocol.NotebookId;
54
+ readonly generation: number;
54
55
  }
55
56
 
56
- /**
57
- * What the editor marks about cells of one replica's notebook.
58
- *
59
- * The marks refer to cells by id, which another notebook, or the same one
60
- * after a reconnect, reuses with different executions behind it. They
61
- * therefore count only for the replica and host notebook they belong to.
62
- */
63
57
  type Marks<T> = {
64
58
  readonly notebook: ShownNotebook;
65
59
  readonly value: T;
@@ -96,55 +90,44 @@ const requestsAtom = atom<Marks<ReadonlySet<RunRequest>>>(null);
96
90
  */
97
91
  const blockedCellsAtom = atom<Marks<ReadonlySet<Protocol.CellId>>>(null);
98
92
 
99
- function current<T>(
100
- marks: Marks<T>,
101
- replica: NotebookReplica | null,
102
- notebookId: Protocol.NotebookId | null,
103
- empty: T,
104
- ): T {
105
- return marks !== null &&
106
- marks.notebook.replica === replica &&
107
- marks.notebook.id === notebookId
93
+ function sameNotebook(
94
+ marked: ShownNotebook,
95
+ shown: ShownNotebook | null,
96
+ ): boolean {
97
+ return (
98
+ shown !== null &&
99
+ marked.replica === shown.replica &&
100
+ marked.id === shown.id &&
101
+ marked.generation === shown.generation
102
+ );
103
+ }
104
+
105
+ function current<T>(marks: Marks<T>, shown: ShownNotebook | null, empty: T): T {
106
+ return marks !== null && sameNotebook(marks.notebook, shown)
108
107
  ? marks.value
109
108
  : empty;
110
109
  }
111
110
 
112
- function shownNotebook(store: Pick<Store, "get">): ShownNotebook | null {
113
- const replica = store.get(activeNotebookReplicaAtom);
114
- const notebook = replica === null ? null : store.get(replica.notebookAtom);
111
+ const shownNotebookAtom = atom((get): ShownNotebook | null => {
112
+ const replica = get(activeNotebookReplicaAtom);
113
+ const notebook = replica === null ? null : get(replica.notebookAtom);
115
114
  return replica === null || notebook === null
116
115
  ? null
117
- : { replica, id: notebook.id };
118
- }
119
-
120
- function mark<T>(store: Pick<Store, "get">, value: T): Marks<T> {
121
- const notebook = shownNotebook(store);
122
- return notebook === null ? null : { notebook, value };
123
- }
116
+ : { replica, id: notebook.id, generation: get(replica.generationAtom) };
117
+ });
124
118
 
125
- /**
126
- * Whether `request` still belongs to the notebook being shown.
127
- *
128
- * Between a reset and the next snapshot the replica has no notebook for a
129
- * moment. That says nothing about where the request belongs, so it does
130
- * not count against it. Another replica, or another notebook on the same
131
- * replica, does.
132
- */
119
+ /** Whether the request still belongs to the current stream. */
133
120
  export function isCurrent(store: Store, request: RunRequest): boolean {
134
- const replica = store.get(activeNotebookReplicaAtom);
135
- if (replica !== request.notebook.replica) {
136
- return false;
137
- }
138
- const notebook = store.get(replica.notebookAtom);
139
- return notebook === null || notebook.id === request.notebook.id;
121
+ return sameNotebook(request.notebook, store.get(shownNotebookAtom));
140
122
  }
141
123
 
142
124
  /** Cells named by pending requests made in the notebook now shown. */
143
125
  const requestedCellsAtom = atom((get) => {
144
- const replica = get(activeNotebookReplicaAtom);
145
- const notebookId =
146
- replica === null ? null : (get(replica.notebookAtom)?.id ?? null);
147
- const requests = current(get(requestsAtom), replica, notebookId, NO_REQUESTS);
126
+ const requests = current(
127
+ get(requestsAtom),
128
+ get(shownNotebookAtom),
129
+ NO_REQUESTS,
130
+ );
148
131
  return requests.size === 0
149
132
  ? NO_REQUESTED
150
133
  : new Set([...requests].flatMap((request) => request.cells));
@@ -153,15 +136,13 @@ const requestedCellsAtom = atom((get) => {
153
136
  /** What every cell's view derives from. Pair with `cellExecutionView`. */
154
137
  export const cellExecutionInputsAtom = atom((get): CellExecutionInputs => {
155
138
  const replica = get(activeNotebookReplicaAtom);
156
- const notebookId =
157
- replica === null ? null : (get(replica.notebookAtom)?.id ?? null);
158
139
  const document = replica === null ? null : get(replica.shownDocumentAtom);
159
140
  return {
160
141
  executions: replica === null ? NO_EXECUTIONS : get(replica.executionsAtom),
161
142
  results: replica === null ? NO_RESULTS : get(replica.resultsAtom),
162
143
  cells: document?.cells ?? NO_CELLS,
163
144
  requested: get(requestedCellsAtom),
164
- blocked: current(get(blockedCellsAtom), replica, notebookId, NO_BLOCKED),
145
+ blocked: current(get(blockedCellsAtom), get(shownNotebookAtom), NO_BLOCKED),
165
146
  runtimeId: replica === null ? null : (get(replica.runtimeAtom)?.id ?? null),
166
147
  };
167
148
  });
@@ -195,17 +176,24 @@ const displayedNotebookAtom = atom((get): ShownNotebook | null => {
195
176
  const document = replica === null ? null : get(replica.shownDocumentAtom);
196
177
  return replica === null || document === null
197
178
  ? null
198
- : { replica, id: document.revision.notebook_id };
179
+ : {
180
+ replica,
181
+ id: document.revision.notebook_id,
182
+ generation: get(replica.generationAtom),
183
+ };
199
184
  });
200
185
 
201
186
  /** What every cell's output derives from. Pair with `cellOutputView`. */
202
187
  export const cellOutputInputsAtom = atom((get): CellOutputInputs => {
203
188
  const replica = get(activeNotebookReplicaAtom);
204
- const notebookId = get(displayedNotebookAtom)?.id ?? null;
205
189
  return {
206
190
  executions: replica === null ? NO_EXECUTIONS : get(replica.executionsAtom),
207
191
  results: replica === null ? NO_RESULTS : get(replica.resultsAtom),
208
- cleared: current(get(clearedOutputsAtom), replica, notebookId, NO_CLEARED),
192
+ cleared: current(
193
+ get(clearedOutputsAtom),
194
+ get(displayedNotebookAtom),
195
+ NO_CLEARED,
196
+ ),
209
197
  cells:
210
198
  replica === null
211
199
  ? NO_CELLS
@@ -347,18 +335,12 @@ export function requestRun(
347
335
  store: Store,
348
336
  cellIds: readonly Protocol.CellId[],
349
337
  ): RunRequest | null {
350
- const notebook = shownNotebook(store);
338
+ const notebook = store.get(shownNotebookAtom);
351
339
  if (notebook === null) {
352
340
  return null;
353
341
  }
354
342
  const request: RunRequest = { notebook, cells: [...cellIds] };
355
- const marks = store.get(requestsAtom);
356
- const kept =
357
- marks !== null &&
358
- marks.notebook.replica === notebook.replica &&
359
- marks.notebook.id === notebook.id
360
- ? marks.value
361
- : NO_REQUESTS;
343
+ const kept = current(store.get(requestsAtom), notebook, NO_REQUESTS);
362
344
  store.set(requestsAtom, { notebook, value: new Set(kept).add(request) });
363
345
  return request;
364
346
  }
@@ -385,13 +367,14 @@ export function acceptRun(
385
367
  request: RunRequest,
386
368
  run: Protocol.Run,
387
369
  ): void {
388
- const { replica } = request.notebook;
389
- const unsubscribe = store.sub(replica.executionsAtom, check);
370
+ const waitingAtom = atom(
371
+ (get) =>
372
+ sameNotebook(request.notebook, get(shownNotebookAtom)) &&
373
+ !get(request.notebook.replica.executionsAtom).runs.has(run.id),
374
+ );
375
+ const unsubscribe = store.sub(waitingAtom, check);
390
376
  function check() {
391
- if (
392
- isCurrent(store, request) &&
393
- !store.get(replica.executionsAtom).runs.has(run.id)
394
- ) {
377
+ if (store.get(waitingAtom)) {
395
378
  return;
396
379
  }
397
380
  unsubscribe();
@@ -415,7 +398,10 @@ function setCellBlocked(
415
398
  } else {
416
399
  next.delete(cellId);
417
400
  }
418
- store.set(blockedCellsAtom, mark(store, next));
401
+ const notebook = store.get(shownNotebookAtom);
402
+ if (notebook !== null) {
403
+ store.set(blockedCellsAtom, { notebook, value: next });
404
+ }
419
405
  }
420
406
 
421
407
  /**
@@ -60,6 +60,8 @@ const MarimoMessage = z.object({ marimo: z.unknown() });
60
60
  export interface NotebookReplica {
61
61
  /** Connection status, independent of application session readiness. */
62
62
  readonly connectionAtom: Atom<ConnectionStatus>;
63
+ /** Local generation of the open stream. Opening or resetting a stream starts a new one. */
64
+ readonly generationAtom: Atom<number>;
63
65
  /** Identity and capabilities, available while the stream is connected and ready. */
64
66
  readonly notebookAtom: Atom<NotebookOutput | null>;
65
67
  /**
@@ -160,6 +162,7 @@ export function createNotebookReplica(
160
162
  const connectionAtom = atom<ConnectionStatus>({
161
163
  state: ConnectionState.NOT_STARTED,
162
164
  });
165
+ const generationAtom = atom(0);
163
166
  // One atom holds the whole stream, which resource atoms derive from.
164
167
  const reducer = createNotebookReducer({
165
168
  document,
@@ -257,6 +260,7 @@ export function createNotebookReplica(
257
260
 
258
261
  return {
259
262
  connectionAtom,
263
+ generationAtom,
260
264
  notebookAtom,
261
265
  documentAtom,
262
266
  shownDocumentAtom,
@@ -298,6 +302,7 @@ export function createNotebookReplica(
298
302
  if (status === "connecting") {
299
303
  setStatus({ state: ConnectionState.CONNECTING });
300
304
  } else if (status === "connected") {
305
+ store.set(generationAtom, (generation) => generation + 1);
301
306
  setStatus({ state: ConnectionState.OPEN });
302
307
  }
303
308
  },
@@ -338,6 +343,9 @@ export function createNotebookReplica(
338
343
  }
339
344
  try {
340
345
  setStream(reducer.reduce(store.get(streamAtom), event));
346
+ if (event.event === "reset") {
347
+ store.set(generationAtom, (generation) => generation + 1);
348
+ }
341
349
  } catch (error) {
342
350
  // An event the replica cannot apply, such as another notebook's.
343
351
  Logger.error("Failed to apply notebook event", error);