-
Notifications
You must be signed in to change notification settings - Fork 100
/
backend.js
71 lines (64 loc) · 2.04 KB
/
backend.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { File } from '../sqlite-file';
import * as perf from 'perf-deets';
import { LOCK_TYPES, getPageSize } from '../sqlite-util';
import { FileOps } from './file-ops';
import { FileOpsFallback } from './file-ops-fallback';
export default class IndexedDBBackend {
constructor(onFallbackFailure) {
this.onFallbackFailure = onFallbackFailure;
}
createFile(filename) {
let ops;
if (typeof SharedArrayBuffer !== 'undefined') {
// SharedArrayBuffer exists! We can run this fully
ops = new FileOps(filename);
} else {
// SharedArrayBuffer is not supported. Use the fallback methods
// which provide a somewhat working version, but doesn't
// support mutations across connections (tabs)
ops = new FileOpsFallback(filename, this.onFallbackFailure);
}
let file = new File(filename, ops);
// If we don't need perf data, there's no reason for us to hold a
// reference to the files. If we did we'd have to worry about
// memory leaks
if (process.env.NODE_ENV !== 'production' || process.env.PERF_BUILD) {
if (this._files == null) {
this._files = new Set();
}
this._files.add(file);
}
return file;
}
// Instead of controlling the profiler from the main thread by
// posting a message to this worker, you can control it inside the
// worker manually with these methods
startProfile() {
perf.start();
for (let file of this._files) {
// If the writer doesn't exist, that means the file has been
// deleted
if (file.ops.writer) {
let writer = file.ops.writer;
let reader = file.ops.reader;
writer.string('profile-start');
writer.finalize();
reader.int32();
reader.done();
}
}
}
stopProfile() {
perf.stop();
for (let file of this._files) {
if (file.ops.writer) {
let writer = file.ops.writer;
let reader = file.ops.reader;
writer.string('profile-stop');
writer.finalize();
reader.int32();
reader.done();
}
}
}
}