Skip to content

Commit

Permalink
Rework collaboration system
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Jan 17, 2025
1 parent 7f325ec commit 2f243b6
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 227 deletions.
4 changes: 4 additions & 0 deletions app/lib/bloc/document_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ abstract class DocumentLoaded extends DocumentState {
{Size? viewportSize, double? pixelRatio, bool reset = false}) =>
currentIndexCubit.bake(this,
viewportSize: viewportSize, pixelRatio: pixelRatio, reset: reset);

@override
Future<Uint8List> saveBytes([NoteData? current]) =>
saveData().then((e) => e.exportAsBytes());
}

class DocumentLoadSuccess extends DocumentLoaded {
Expand Down
70 changes: 36 additions & 34 deletions app/lib/services/network.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ import 'package:rxdart/rxdart.dart';
part 'network.freezed.dart';
part 'network.g.dart';

enum NetworkingSide {
server,
client,
}

enum NetworkingType {
webSocket;

Expand All @@ -29,17 +24,9 @@ enum NetworkingType {
}

const kDefaultPort = 28005;
const kTimeout = Duration(seconds: 10);
const kTimeout = Duration(minutes: 5);
typedef NetworkingState = (NetworkerBase, NamedRpcNetworkerPipe<NetworkEvent>);

@freezed
class NetworkingInitMessage with _$NetworkingInitMessage {
const factory NetworkingInitMessage(List<int>? data) = _NetworkingInitMessage;

factory NetworkingInitMessage.fromJson(Map<String, dynamic> json) =>
_$NetworkingInitMessageFromJson(json);
}

@freezed
class NetworkingUser with _$NetworkingUser {
const factory NetworkingUser({
Expand Down Expand Up @@ -101,23 +88,32 @@ class NetworkingService {
final rpc = NamedRpcServerNetworkerPipe<NetworkEvent>();
_setupRpc(rpc, server);
void sendConnections() {
rpc.callFunction(NetworkEvent.connections.index,
Uint8List.fromList(jsonEncode(connections.toList()).codeUnits));
final current = server.clientConnections;
_connections.add(current);
rpc.sendMessage(RpcNetworkerPacket(
function: NetworkEvent.connections.index,
data: Uint8List.fromList(jsonEncode(current.toList()).codeUnits),
channel: kAuthorityChannel,
));
}

server.clientConnect.listen((event) async {
final state = _bloc?.state;
rpc.callFunction(
NetworkEvent.init.index,
Uint8List.fromList(
jsonEncode(NetworkingInitMessage((await state?.saveBytes())))
.codeUnits));
if (state is! DocumentLoaded) return;
rpc.sendMessage(
RpcNetworkerPacket(
function: NetworkEvent.init.index,
data: await state.saveBytes(),
channel: kAuthorityChannel,
),
event.$1);
sendConnections();
});
server.clientDisconnect.listen((event) {
sendConnections();
});
server.connect(rpc);
await server.init();
_subject.add((server, rpc));
}

Expand All @@ -126,20 +122,18 @@ class NetworkingService {
if (!uri.hasPort) {
uri = uri.replace(port: kDefaultPort);
}
if(!uri.hasScheme) {
uri = uri.replace(scheme: 'ws');
}
final client = NetworkerSocketClient(uri);
final rpc = NamedRpcClientNetworkerPipe<NetworkEvent>();
_setupRpc(rpc, client);
final completer = Completer<Uint8List?>();
rpc
.registerFunction(NetworkEvent.init.index,
mode: RpcNetworkerMode.authority)
.connect(RawJsonNetworkerPlugin()
..read.listen((message) {
final init = NetworkingInitMessage.fromJson(message.data);
completer.complete(
init.data == null ? null : Uint8List.fromList(init.data!));
}));
rpc.registerNamedFunction(NetworkEvent.init).read.listen((message) {
completer.complete(message.data);
});
client.connect(rpc);
await client.init();
_subject.add((client, rpc));
return completer.future.timeout(kTimeout);
}
Expand Down Expand Up @@ -179,16 +173,24 @@ class NetworkingService {
}

void sendUser(NetworkingUser user) {
state?.$2.callNamedFunction(NetworkEvent.user,
Uint8List.fromList(jsonEncode(user.toJson()).codeUnits));
state?.$2.sendMessage(RpcNetworkerPacket(
function: NetworkEvent.user.index,
data: Uint8List.fromList(jsonEncode(user.toJson()).codeUnits),
channel: kAuthorityChannel,
));
}

bool _externalEvent = false;

void onEvent(DocumentEvent event) {
if (!event.shouldSync() || _externalEvent) return;
state?.$2.callNamedFunction(NetworkEvent.event,
Uint8List.fromList(jsonEncode(event.toJson()).codeUnits));
state?.$2.sendMessage(RpcNetworkerPacket(
function: NetworkEvent.event.index,
data: Uint8List.fromList(
jsonEncode(event.toJson()).codeUnits,
),
channel: state?.$1 is NetworkerServer ? kAuthorityChannel : kAnyChannel,
));
}

void onMessage(DocumentEvent event) {
Expand Down
173 changes: 0 additions & 173 deletions app/lib/services/network.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,179 +14,6 @@ T _$identity<T>(T value) => value;
final _privateConstructorUsedError = UnsupportedError(
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models');

NetworkingInitMessage _$NetworkingInitMessageFromJson(
Map<String, dynamic> json) {
return _NetworkingInitMessage.fromJson(json);
}

/// @nodoc
mixin _$NetworkingInitMessage {
List<int>? get data => throw _privateConstructorUsedError;

/// Serializes this NetworkingInitMessage to a JSON map.
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;

/// Create a copy of NetworkingInitMessage
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
$NetworkingInitMessageCopyWith<NetworkingInitMessage> get copyWith =>
throw _privateConstructorUsedError;
}

/// @nodoc
abstract class $NetworkingInitMessageCopyWith<$Res> {
factory $NetworkingInitMessageCopyWith(NetworkingInitMessage value,
$Res Function(NetworkingInitMessage) then) =
_$NetworkingInitMessageCopyWithImpl<$Res, NetworkingInitMessage>;
@useResult
$Res call({List<int>? data});
}

/// @nodoc
class _$NetworkingInitMessageCopyWithImpl<$Res,
$Val extends NetworkingInitMessage>
implements $NetworkingInitMessageCopyWith<$Res> {
_$NetworkingInitMessageCopyWithImpl(this._value, this._then);

// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;

/// Create a copy of NetworkingInitMessage
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? data = freezed,
}) {
return _then(_value.copyWith(
data: freezed == data
? _value.data
: data // ignore: cast_nullable_to_non_nullable
as List<int>?,
) as $Val);
}
}

/// @nodoc
abstract class _$$NetworkingInitMessageImplCopyWith<$Res>
implements $NetworkingInitMessageCopyWith<$Res> {
factory _$$NetworkingInitMessageImplCopyWith(
_$NetworkingInitMessageImpl value,
$Res Function(_$NetworkingInitMessageImpl) then) =
__$$NetworkingInitMessageImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({List<int>? data});
}

/// @nodoc
class __$$NetworkingInitMessageImplCopyWithImpl<$Res>
extends _$NetworkingInitMessageCopyWithImpl<$Res,
_$NetworkingInitMessageImpl>
implements _$$NetworkingInitMessageImplCopyWith<$Res> {
__$$NetworkingInitMessageImplCopyWithImpl(_$NetworkingInitMessageImpl _value,
$Res Function(_$NetworkingInitMessageImpl) _then)
: super(_value, _then);

/// Create a copy of NetworkingInitMessage
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? data = freezed,
}) {
return _then(_$NetworkingInitMessageImpl(
freezed == data
? _value._data
: data // ignore: cast_nullable_to_non_nullable
as List<int>?,
));
}
}

/// @nodoc
@JsonSerializable()
class _$NetworkingInitMessageImpl
with DiagnosticableTreeMixin
implements _NetworkingInitMessage {
const _$NetworkingInitMessageImpl(final List<int>? data) : _data = data;

factory _$NetworkingInitMessageImpl.fromJson(Map<String, dynamic> json) =>
_$$NetworkingInitMessageImplFromJson(json);

final List<int>? _data;
@override
List<int>? get data {
final value = _data;
if (value == null) return null;
if (_data is EqualUnmodifiableListView) return _data;
// ignore: implicit_dynamic_type
return EqualUnmodifiableListView(value);
}

@override
String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) {
return 'NetworkingInitMessage(data: $data)';
}

@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties
..add(DiagnosticsProperty('type', 'NetworkingInitMessage'))
..add(DiagnosticsProperty('data', data));
}

@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$NetworkingInitMessageImpl &&
const DeepCollectionEquality().equals(other._data, _data));
}

@JsonKey(includeFromJson: false, includeToJson: false)
@override
int get hashCode =>
Object.hash(runtimeType, const DeepCollectionEquality().hash(_data));

/// Create a copy of NetworkingInitMessage
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@override
@pragma('vm:prefer-inline')
_$$NetworkingInitMessageImplCopyWith<_$NetworkingInitMessageImpl>
get copyWith => __$$NetworkingInitMessageImplCopyWithImpl<
_$NetworkingInitMessageImpl>(this, _$identity);

@override
Map<String, dynamic> toJson() {
return _$$NetworkingInitMessageImplToJson(
this,
);
}
}

abstract class _NetworkingInitMessage implements NetworkingInitMessage {
const factory _NetworkingInitMessage(final List<int>? data) =
_$NetworkingInitMessageImpl;

factory _NetworkingInitMessage.fromJson(Map<String, dynamic> json) =
_$NetworkingInitMessageImpl.fromJson;

@override
List<int>? get data;

/// Create a copy of NetworkingInitMessage
/// with the given fields replaced by the non-null parameter values.
@override
@JsonKey(includeFromJson: false, includeToJson: false)
_$$NetworkingInitMessageImplCopyWith<_$NetworkingInitMessageImpl>
get copyWith => throw _privateConstructorUsedError;
}

NetworkingUser _$NetworkingUserFromJson(Map<String, dynamic> json) {
return _NetworkingUser.fromJson(json);
}
Expand Down
11 changes: 0 additions & 11 deletions app/lib/services/network.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions app/lib/views/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,13 @@ class _ProjectPageState extends State<ProjectPage> {
NoteData? document;
var data = widget.data;
final uri = Uri.tryParse(widget.uri ?? '');
var type = widget.type.isEmpty ? (fileType ?? widget.type) : widget.type;
if (widget.uri != null && uri != null) {
data = await networkingService.createSocketClient(uri);
type = '';
}
if (data != null) {
document ??= await globalImportService.load(
type: widget.type.isEmpty ? (fileType ?? widget.type) : widget.type,
data: data);
document ??= await globalImportService.load(type: type, data: data);
if (document == null) {
GoRouter.of(context).pop();
return;
Expand Down
8 changes: 4 additions & 4 deletions app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -873,17 +873,17 @@ packages:
dependency: "direct main"
description:
path: "packages/networker/networker"
ref: "071e02761bf55a47bcde0cdc2a711b8ff5fbbb18"
resolved-ref: "071e02761bf55a47bcde0cdc2a711b8ff5fbbb18"
ref: "4d8b20841bcfa644695963c69f3e3032b764d283"
resolved-ref: "4d8b20841bcfa644695963c69f3e3032b764d283"
url: "https://github.com/LinwoodDev/dart_pkgs"
source: git
version: "1.0.0"
networker_socket:
dependency: "direct main"
description:
path: "packages/networker/networker_socket"
ref: "9085d42cf75c3d3edc8edb9ef22697dbbe2bf38c"
resolved-ref: "9085d42cf75c3d3edc8edb9ef22697dbbe2bf38c"
ref: "630928b8588064a0895e45ca91993ce05723811f"
resolved-ref: "630928b8588064a0895e45ca91993ce05723811f"
url: "https://github.com/LinwoodDev/dart_pkgs"
source: git
version: "1.0.0"
Expand Down
4 changes: 2 additions & 2 deletions app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ dependencies:
networker:
git:
url: https://github.com/LinwoodDev/dart_pkgs
ref: 071e02761bf55a47bcde0cdc2a711b8ff5fbbb18
ref: 4d8b20841bcfa644695963c69f3e3032b764d283
path: packages/networker/networker
networker_socket:
git:
url: https://github.com/LinwoodDev/dart_pkgs
ref: 9085d42cf75c3d3edc8edb9ef22697dbbe2bf38c
ref: 630928b8588064a0895e45ca91993ce05723811f
path: packages/networker/networker_socket
lw_file_system:
git:
Expand Down
Loading

0 comments on commit 2f243b6

Please sign in to comment.