-
Notifications
You must be signed in to change notification settings - Fork 383
Expand file tree
/
Copy pathnative_db.dart
More file actions
108 lines (98 loc) · 3.15 KB
/
Copy pathnative_db.dart
File metadata and controls
108 lines (98 loc) · 3.15 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// coverage:ignore-file
import 'dart:io';
import 'dart:isolate';
import 'package:drift/drift.dart';
import 'package:drift/isolate.dart';
import 'package:drift/native.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:stream_chat_persistence/src/db/drift_chat_database.dart';
import 'package:stream_chat_persistence/src/stream_chat_persistence_client.dart';
import 'package:stream_chat_persistence/stream_chat_persistence.dart';
/// A Helper class to construct new instances of [DriftChatDatabase]
/// specifically for native platform applications.
class SharedDB {
/// Returns a new instance of [DriftChatDatabase].
static Future<DriftChatDatabase> constructDatabase(
String userId, {
bool logStatements = false,
ConnectionMode connectionMode = ConnectionMode.regular,
bool webUseIndexedDbIfSupported = false, // Ignored on native
}) async {
final dbName = 'db_$userId';
if (connectionMode == ConnectionMode.background) {
return DriftChatDatabase(
userId,
DatabaseConnection.delayed(Future(() async {
final isolate = await _createDriftIsolate(
dbName,
logStatements: logStatements,
);
return isolate.connect();
})),
);
}
return DriftChatDatabase(
userId,
LazyDatabase(
() async => _constructDatabase(
dbName,
logStatements: logStatements,
),
),
);
}
static Future<NativeDatabase> _constructDatabase(
String dbName, {
bool logStatements = false,
}) async {
if (Platform.isIOS || Platform.isAndroid) {
final dir = await getApplicationDocumentsDirectory();
final path = join(dir.path, '$dbName.sqlite');
final file = File(path);
return NativeDatabase(file, logStatements: logStatements);
}
if (Platform.isMacOS || Platform.isLinux) {
final file = File('$dbName.sqlite');
return NativeDatabase(file, logStatements: logStatements);
}
return NativeDatabase.memory(logStatements: logStatements);
}
static void _startBackground(_IsolateStartRequest request) {
final executor = LazyDatabase(() async => NativeDatabase(
File(request.targetPath),
logStatements: request.logStatements,
));
final driftIsolate = DriftIsolate.inCurrent(
() => DatabaseConnection(executor),
);
request.sendDriftIsolate.send(driftIsolate);
}
static Future<DriftIsolate> _createDriftIsolate(
String dbName, {
bool logStatements = false,
}) async {
final dir = await getApplicationDocumentsDirectory();
final path = join(dir.path, '$dbName.sqlite');
final receivePort = ReceivePort();
await Isolate.spawn(
_startBackground,
_IsolateStartRequest(
receivePort.sendPort,
path,
logStatements: logStatements,
),
);
return await receivePort.first as DriftIsolate;
}
}
class _IsolateStartRequest {
const _IsolateStartRequest(
this.sendDriftIsolate,
this.targetPath, {
this.logStatements = false,
});
final SendPort sendDriftIsolate;
final String targetPath;
final bool logStatements;
}