Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion example/lib/random_operation_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:math';

import 'package:example/models/user.dart';
import 'package:loon/loon.dart';
import 'package:loon/utils/id.dart';

enum Operations {
hydrate,
Expand All @@ -24,7 +25,7 @@ class RandomOperationRunner {
_logger.log('Persist $count');

for (int i = 0; i < count; i++) {
final id = uuid.v4();
final id = generateId();
UserModel.store.doc(id).create(UserModel(name: 'User $id'));
}
} else if (operationIndex >= 80 && operationIndex < 90) {
Expand Down
4 changes: 1 addition & 3 deletions lib/broadcast_observer.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
part of './loon.dart';

const uuid = Uuid();

/// A mixin that provides an observable interface for the access and streaming of data broadcasted from the store.
mixin BroadcastObserver<T, S> {
late final StreamController<T> _controller;
Expand Down Expand Up @@ -45,7 +43,7 @@ mixin BroadcastObserver<T, S> {
_controllerValue = initialValue;
_controller.add(initialValue);

_observerId = "${path}__${uuid.v4()}";
_observerId = "${path}__${generateId()}";

Loon._instance.broadcastManager.addObserver(this, initialValue);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/collection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ class Collection<T> implements Queryable<T>, StoreReference {
Loon._instance._isGlobalPersistenceEnabled;
}

Document<T> doc(String id) {
Document<T> doc([String? id]) {
return Document<T>(
path,
id,
id ?? generateId(),
fromJson: fromJson,
toJson: toJson,
persistorSettings: persistorSettings,
Expand Down
4 changes: 2 additions & 2 deletions lib/loon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'dart:async';
import 'dart:convert';
import 'package:flutter/foundation.dart' hide Key;
import 'package:loon/persistor/data_store_encrypter.dart';
import 'package:uuid/uuid.dart';
import 'package:loon/utils/id.dart';
import 'dart:collection';
import 'persistor/index.dart';

Expand Down Expand Up @@ -121,7 +121,7 @@ class Loon {
},
).toList();

return List<DocumentSnapshot<T>>.from(snaps ?? []);
return snaps ?? [];
}

DocumentSnapshot<T> writeDocument<T>(
Expand Down
6 changes: 2 additions & 4 deletions lib/persistor/worker/messages.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import 'dart:isolate';
import 'package:loon/persistor/persist_payload.dart';
import 'package:uuid/uuid.dart';

const uuid = Uuid();
import 'package:loon/utils/id.dart';

abstract class Message {}

abstract class MessageRequest<T extends MessageResponse> extends Message {
final id = uuid.v4();
final id = generateId();

MessageRequest();

Expand Down
29 changes: 29 additions & 0 deletions lib/utils/id.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'dart:math';
import 'dart:typed_data';

const String _alphabet =
'_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
final Uint8List _alphabytes = Uint8List.fromList(_alphabet.codeUnits);
const int _u32 = 0x100000000; // 2^32

final Random _rand = Random.secure();

/// Generates a cryptographically secure, URL-safe random ID.
/// Default: 21 chars ≈ 126 bits of entropy.
String generateId([int size = 21]) {
final out = Uint8List(size);
var i = 0;

while (i < size) {
int r = _rand.nextInt(_u32); // 5×6-bit chars (30 bits used, 2 bits unused)

var k = 0;
while (k < 5 && i < size) {
out[i++] = _alphabytes[r & 63];
r >>= 6;
k++;
}
}

return String.fromCharCodes(out);
}
1 change: 0 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ environment:
flutter: ">=1.17.0"

dependencies:
uuid: ^4.3.3
encrypt: ^5.0.3
flutter:
sdk: flutter
Expand Down
2 changes: 0 additions & 2 deletions test/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import 'dart:async';
import 'dart:convert';
import 'package:encrypt/encrypt.dart';
import 'package:loon/loon.dart';
import 'package:uuid/uuid.dart';

const uuid = Uuid();
final testEncryptionKey = Key.fromSecureRandom(32);

String encryptData(Json json) {
Expand Down
Loading