Skip to content

Commit abb1346

Browse files
committed
Put all the worker logic inside the wallpaper_worker package
1 parent bd674d3 commit abb1346

7 files changed

Lines changed: 150 additions & 79 deletions

File tree

dart/pubspec.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ environment:
99
# Add regular dependencies here.
1010
dependencies:
1111
http: ^1.3.0
12-
image: ^4.5.2
1312
wallpaper_generator:
1413
path: ../packages/wallpaper_generator
1514
wallpaper:
1615
path: ../packages/wallpaper
17-
worker_database:
18-
path: ../packages/worker_database
16+
wallpaper_worker:
17+
path: ../packages/wallpaper_worker
1918

2019
dev_dependencies:
2120
build_runner: ^2.4.15
Lines changed: 2 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,3 @@
1-
import 'dart:typed_data';
2-
import 'dart:convert';
1+
import 'package:wallpaper_worker/wallpaper_worker.dart';
32

4-
import 'dart:html';
5-
6-
import 'package:wallpaper/wallpaper.dart';
7-
import 'package:wallpaper_generator/wallpaper_generator.dart';
8-
// import 'package:worker_database/worker_database.dart';
9-
10-
void main() {
11-
12-
// Database for already generated wallpaper
13-
// WorkerDatabase wdb = WorkerDatabase<ByteBuffer, String>("WallpaperDB", "wallpaperBytesData");
14-
15-
// Get the worker scope
16-
final DedicatedWorkerGlobalScope workerScope = DedicatedWorkerGlobalScope.instance;
17-
18-
// Listen for any message
19-
workerScope.onMessage.listen((MessageEvent event) async {
20-
21-
var data = event.data;
22-
23-
// Map the inputs from the message event
24-
if (data is Map) {
25-
26-
// Generate and post the wallpaper bitmap as response
27-
if (data['wallpaper'] != null) {
28-
29-
// Generate the wallpaper object
30-
String wallpaperRawJson = jsonEncode(data['wallpaper']);
31-
Wallpaper? wallpaper = Wallpaper.fromRawJson(wallpaperRawJson);
32-
33-
// Fetch already generated if any
34-
/*ByteBuffer? resCache = await wdb.tryFetch(wallpaper.toRawJson());
35-
if(resCache != null){
36-
print("Wallpaper Cache Hit from DB, posted to main thread.");
37-
Uint8List byteArray = Uint8List.view(resCache);
38-
workerScope.postMessage({'wallpaperBytes' : byteArray }, [byteArray.buffer]);
39-
return;
40-
}*/
41-
42-
// Generate the wallpaper
43-
Uint8List? res = await generateWallpaper(wallpaper);
44-
if (res != null) {
45-
print("Wallpaper Generated, posted to main thread.");
46-
// await wdb.tryPut(wallpaper.toRawJson(), res.buffer);
47-
workerScope.postMessage({'wallpaperBytes' : res }, [res.buffer]);
48-
} else {
49-
print("Wallpaper not Generated, something not doing well");
50-
workerScope.postMessage(null);
51-
}
52-
}
53-
}
54-
});
55-
}
56-
57-
// Generate the wallpaper into a byte array
58-
// this format is friendly to postMessage despite BitmapImage or OffscreenCanvas
59-
// cause for some reason in dart this object are set to null after despite they
60-
// are "transferable"... idk. Bruteforcing to Uint8List works like a charm.
61-
Future<Uint8List?> generateWallpaper(Wallpaper? wallpaper) async {
62-
63-
// Check if the wallpaper is not null
64-
if (wallpaper == null) {
65-
print("Wallpaper is null, cannot generate a null wallpaper.");
66-
return null;
67-
}
68-
69-
// Generate the wallpaper
70-
Uint8List? imageBytes = await WallpaperGenerator.generateWallpaper(wallpaper);
71-
if (imageBytes == null) {
72-
print("Generated image is null, there it was an error in the wallpaper generation.");
73-
return null;
74-
}
75-
76-
return imageBytes;
77-
}
3+
void main() { WallpaperWorker.initialize(); }
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# https://dart.dev/guides/libraries/private-files
2+
# Created by `dart pub`
3+
.dart_tool/
4+
5+
# Avoid committing pubspec.lock for library packages; see
6+
# https://dart.dev/guides/libraries/private-files#pubspeclock.
7+
pubspec.lock
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This file configures the static analysis results for your project (errors,
2+
# warnings, and lints).
3+
#
4+
# This enables the 'recommended' set of lints from `package:lints`.
5+
# This set helps identify many issues that may lead to problems when running
6+
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
7+
# style and format.
8+
#
9+
# If you want a smaller set of lints you can change this to specify
10+
# 'package:lints/core.yaml'. These are just the most critical lints
11+
# (the recommended set includes the core lints).
12+
# The core lints are also what is used by pub.dev for scoring packages.
13+
14+
include: package:lints/recommended.yaml
15+
16+
# Uncomment the following section to specify additional rules.
17+
18+
# linter:
19+
# rules:
20+
# - camel_case_types
21+
22+
# analyzer:
23+
# exclude:
24+
# - path/to/excluded/files/**
25+
26+
# For more information about the core and recommended set of lints, see
27+
# https://dart.dev/go/core-lints
28+
29+
# For additional information about configuring this file, see
30+
# https://dart.dev/guides/language/analysis-options
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// TODO: Put public facing types in this file.
2+
3+
/// Checks if you are awesome. Spoiler: you are.
4+
class Awesome {
5+
bool get isAwesome => true;
6+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
library;
2+
3+
import 'dart:typed_data';
4+
import 'dart:convert';
5+
6+
import 'dart:html';
7+
8+
import 'package:wallpaper/wallpaper.dart';
9+
import 'package:wallpaper_generator/wallpaper_generator.dart';
10+
// import 'package:worker_database/worker_database.dart';
11+
12+
class WallpaperWorker {
13+
14+
// Initialize the wallpaper worker (Execute this method inside the worker)
15+
static void initialize(){
16+
// Database for already generated wallpaper
17+
// WorkerDatabase wdb = WorkerDatabase<ByteBuffer, String>("WallpaperDB", "wallpaperBytesData");
18+
19+
// Get the worker scope
20+
final DedicatedWorkerGlobalScope workerScope = DedicatedWorkerGlobalScope.instance;
21+
22+
// Listen for any message
23+
workerScope.onMessage.listen((MessageEvent event) async {
24+
25+
var data = event.data;
26+
27+
// Map the inputs from the message event
28+
if (data is Map) {
29+
30+
// Generate and post the wallpaper bitmap as response
31+
if (data['wallpaper'] != null) {
32+
33+
// Generate the wallpaper object
34+
String wallpaperRawJson = jsonEncode(data['wallpaper']);
35+
Wallpaper? wallpaper = Wallpaper.fromRawJson(wallpaperRawJson);
36+
37+
// Fetch already generated if any
38+
/*ByteBuffer? resCache = await wdb.tryFetch(wallpaper.toRawJson());
39+
if(resCache != null){
40+
print("Wallpaper Cache Hit from DB, posted to main thread.");
41+
Uint8List byteArray = Uint8List.view(resCache);
42+
workerScope.postMessage({'wallpaperBytes' : byteArray }, [byteArray.buffer]);
43+
return;
44+
}*/
45+
46+
// Generate the wallpaper
47+
Uint8List? res = await generateWallpaper(wallpaper);
48+
if (res != null) {
49+
print("Wallpaper Generated, posted to main thread.");
50+
// await wdb.tryPut(wallpaper.toRawJson(), res.buffer);
51+
workerScope.postMessage({'wallpaperBytes' : res }, [res.buffer]);
52+
} else {
53+
print("Wallpaper not Generated, something not doing well");
54+
workerScope.postMessage(null);
55+
}
56+
}
57+
}
58+
});
59+
}
60+
61+
// Generate the wallpaper into a byte array
62+
// this format is friendly to postMessage despite BitmapImage or OffscreenCanvas
63+
// cause for some reason in dart this object are set to null after despite they
64+
// are "transferable"... idk. Bruteforcing to Uint8List works like a charm.
65+
static Future<Uint8List?> generateWallpaper(Wallpaper? wallpaper) async {
66+
67+
// Check if the wallpaper is not null
68+
if (wallpaper == null) {
69+
print("Wallpaper is null, cannot generate a null wallpaper.");
70+
return null;
71+
}
72+
73+
// Generate the wallpaper
74+
Uint8List? imageBytes = await WallpaperGenerator.generateWallpaper(wallpaper);
75+
if (imageBytes == null) {
76+
print("Generated image is null, there it was an error in the wallpaper generation.");
77+
return null;
78+
}
79+
80+
return imageBytes;
81+
}
82+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: wallpaper_worker
2+
description: A starting point for Dart libraries or applications.
3+
version: 1.0.0
4+
publish_to: none
5+
6+
environment:
7+
sdk: ^3.6.2
8+
9+
dependencies:
10+
http: ^1.3.0
11+
image: ^4.5.2
12+
wallpaper_generator:
13+
path: ../wallpaper_generator
14+
wallpaper:
15+
path: ../wallpaper
16+
worker_database:
17+
path: ../worker_database
18+
19+
dev_dependencies:
20+
lints: ^5.0.0
21+
test: ^1.24.0

0 commit comments

Comments
 (0)