• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vanvalenlab / deepcell-label / 4029042521

pending completion
4029042521

Pull #392

github

GitHub
Merge dd3cca3d5 into a52d16389
Pull Request #392: Implement cell types machine, basic controls, and canvas rendering

334 of 697 branches covered (47.92%)

Branch coverage included in aggregate %.

53 of 247 new or added lines in 24 files covered. (21.46%)

71 existing lines in 3 files now uncovered.

2483 of 3862 relevant lines covered (64.29%)

1106.57 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/frontend/src/Project/service/idbWebWorker.js
1
/** Reads and writes project data to the projects table in the deepcell-label IndexedDB in a web worker.
2
 *
3
 * When initialized, waits for a PROJECT_ID event, then checks if that project is in the database.
4
 * If yes, responds with a LOADED event, and if not, responds with PROJECT_NOT_IN_DB and
5
 * waits for a LOADED event and writes the loaded data to the database.
6
 *
7
 * Once the project is in the database, the machine rewrites updated project data after receiving
8
 * EDITED_SEGMENT, RESTORED_SEGMENT, CELLS, DIVISIONS, and SPOTS events.
9
 */
10

11
import { openDB } from 'idb';
12
import { assign, createMachine, sendParent } from 'xstate';
13
import { interpretInWebWorker } from './from-web-worker';
14

15
const idbMachine = createMachine(
×
16
  {
17
    id: 'idbWebWorker',
18
    context: {
19
      db: null,
20
      projectId: null,
21
      project: {
22
        raw: null, // Uint8Array[][][]
23
        labeled: null, // Int32Array[][][]
24
        cells: null, // list of cells (not the Cells object) { value: number, cell, number, t: number}[]
25
        cellTypes: null, // list of cell types { id: number, name: string, color: string, cells: number[] }
26
        divisions: null, // list of divisions { parent: number, daughters: number[], t: number}[]
27
        spots: null, // list of spot coordinates [number, number][]
28
      },
29
    },
30
    initial: 'waitForId',
31
    states: {
32
      waitForId: {
33
        on: {
34
          PROJECT_ID: { target: 'openDb', actions: 'setProjectId' },
35
        },
36
      },
37
      openDb: {
38
        invoke: {
39
          src: 'openDB',
40
          onDone: { target: 'getProject', actions: 'setDb' },
41
        },
42
      },
43
      getProject: {
44
        invoke: {
45
          src: 'getProject',
46
          onDone: [
47
            {
48
              cond: 'projectInDb',
49
              target: 'idle',
50
              actions: ['setProjectFromDb', 'sendLoaded'],
51
            },
52
            { target: 'loading', actions: 'sendProjectNotInDB' },
53
          ],
54
        },
55
      },
56
      loading: {
57
        on: {
58
          LOADED: { target: 'idle.putProject', actions: 'setProject' },
59
        },
60
      },
61
      idle: {
62
        on: {
63
          EDITED_SEGMENT: {
64
            target: '.putProject',
65
            actions: 'updateLabeled',
66
            internal: false,
67
          },
68
          RESTORED_SEGMENT: {
69
            target: '.putProject',
70
            actions: 'updateLabeled',
71
            internal: false,
72
          },
73
          CELLS: { target: '.putProject', actions: 'updateCells', internal: false },
74
          CELLTYPES: { target: '.putProject', actions: 'updateCellTypes', internal: false },
75
          DIVISIONS: {
76
            target: '.putProject',
77
            actions: 'updateDivisions',
78
            internal: false,
79
          },
80
          SPOTS: { target: '.putProject', actions: 'updateSpots', internal: false },
81
        },
82
        initial: 'idle',
83
        states: {
84
          idle: {},
85
          putProject: {
86
            invoke: { src: 'putProject', onDone: 'idle' },
87
          },
88
        },
89
      },
90
    },
91
  },
92
  {
93
    guards: {
94
      projectInDb: (ctx, evt) => evt.data !== undefined,
×
95
    },
96
    services: {
97
      openDB: () =>
98
        openDB('deepcell-label', 2, {
×
99
          upgrade(db) {
100
            db.createObjectStore('projects');
×
101
          },
102
        }),
103
      getProject: (ctx) => ctx.db.get('projects', ctx.projectId),
×
104
      putProject: (ctx) => ctx.db.put('projects', ctx.project, ctx.projectId),
×
105
    },
106
    actions: {
107
      setProjectId: assign({ projectId: (ctx, evt) => evt.projectId }),
×
108
      setDb: assign({ db: (ctx, evt) => evt.data }),
×
109
      sendProjectNotInDB: sendParent('PROJECT_NOT_IN_DB'),
110
      sendLoaded: sendParent((ctx, evt) => ({
×
111
        type: 'LOADED',
112
        ...evt.data,
113
        message: 'from idb web worker machine',
114
      })),
115
      setProject: assign((ctx, evt) => {
116
        const { type, ...project } = evt;
×
117
        return { project };
×
118
      }),
119
      setProjectFromDb: assign({ project: (ctx, evt) => ({ ...evt.data }) }),
×
120
      updateLabeled: assign({
121
        project: (ctx, evt) => {
122
          const { t, c } = evt;
×
123
          const labeled = ctx.project.labeled.map((arr, i) =>
×
124
            i === c ? arr.map((arr, j) => (j === t ? evt.labeled : arr)) : arr
×
125
          );
126
          return { ...ctx.project, labeled };
×
127
        },
128
      }),
129
      updateCells: assign({
130
        project: (ctx, evt) => ({ ...ctx.project, cells: evt.cells }),
×
131
      }),
132
      updateCellTypes: assign({
NEW
133
        project: (ctx, evt) => ({ ...ctx.project, cellTypes: evt.cellTypes }),
×
134
      }),
135
      updateDivisions: assign({
136
        project: (ctx, evt) => ({ ...ctx.project, divisions: evt.divisions }),
×
137
      }),
138
      updateSpots: assign({
139
        project: (ctx, evt) => ({ ...ctx.project, spots: evt.spots }),
×
140
      }),
141
    },
142
  }
143
);
144

145
const service = interpretInWebWorker(idbMachine);
×
146
service.start();
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc