Remove unneeded promises
This commit is contained in:
parent
708afaf4d3
commit
9983f9884d
2 changed files with 11 additions and 90 deletions
48
main.js
48
main.js
|
|
@ -121,71 +121,33 @@ function generateAltText() {
|
||||||
altText = () => alert(generateAltText());
|
altText = () => alert(generateAltText());
|
||||||
|
|
||||||
if ('webkit' in window && 'messageHandlers' in window.webkit) {
|
if ('webkit' in window && 'messageHandlers' in window.webkit) {
|
||||||
let _promiseId = 0;
|
altText = () => window.webkit.messageHandlers.altText.postMessage([generateAltText()]);
|
||||||
let _promises = new Map();
|
|
||||||
|
|
||||||
window.addEventListener('message', (event) => {
|
|
||||||
const [id] = event.detail;
|
|
||||||
const [resolve,] = _promises.get(id) ?? [];
|
|
||||||
if (!resolve) return;
|
|
||||||
_promises.delete(id);
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
|
|
||||||
window.addEventListener('message', (event) => {
|
|
||||||
const [id] = event.detail;
|
|
||||||
const [,reject] = _promises.get(id) ?? [];
|
|
||||||
if (!reject) return;
|
|
||||||
_promises.delete(id);
|
|
||||||
reject();
|
|
||||||
});
|
|
||||||
|
|
||||||
altText = () => new Promise((resolve, reject) => {
|
|
||||||
const thisPromiseId = ++_promiseId;
|
|
||||||
|
|
||||||
_promises.set(thisPromiseId, [resolve, reject]);
|
|
||||||
|
|
||||||
window.webkit.messageHandlers.altText.postMessage([
|
|
||||||
thisPromiseId,
|
|
||||||
generateAltText(),
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
|
|
||||||
copyClipboard = () => new Promise((resolve, reject) => {
|
|
||||||
const thisPromiseId = ++_promiseId;
|
|
||||||
|
|
||||||
_promises.set(thisPromiseId, [resolve, reject]);
|
|
||||||
|
|
||||||
|
copyClipboard = () => {
|
||||||
const schedule = document.getElementById('schedule');
|
const schedule = document.getElementById('schedule');
|
||||||
const boundingRect = schedule.getBoundingClientRect();
|
const boundingRect = schedule.getBoundingClientRect();
|
||||||
|
|
||||||
window.webkit.messageHandlers.copySnapshot.postMessage([
|
window.webkit.messageHandlers.copySnapshot.postMessage([
|
||||||
thisPromiseId,
|
|
||||||
'clipboard',
|
'clipboard',
|
||||||
boundingRect.x + 1,
|
boundingRect.x + 1,
|
||||||
boundingRect.y + 1,
|
boundingRect.y + 1,
|
||||||
boundingRect.width - 2,
|
boundingRect.width - 2,
|
||||||
boundingRect.height - 2,
|
boundingRect.height - 2,
|
||||||
]);
|
]);
|
||||||
});
|
};
|
||||||
|
|
||||||
saveImage = () => new Promise((resolve, reject) => {
|
|
||||||
const thisPromiseId = ++_promiseId;
|
|
||||||
|
|
||||||
_promises.set(thisPromiseId, [resolve, reject]);
|
|
||||||
|
|
||||||
|
saveImage = () => {
|
||||||
const schedule = document.getElementById('schedule');
|
const schedule = document.getElementById('schedule');
|
||||||
const boundingRect = schedule.getBoundingClientRect();
|
const boundingRect = schedule.getBoundingClientRect();
|
||||||
|
|
||||||
window.webkit.messageHandlers.copySnapshot.postMessage([
|
window.webkit.messageHandlers.copySnapshot.postMessage([
|
||||||
thisPromiseId,
|
|
||||||
'file',
|
'file',
|
||||||
boundingRect.x + 1,
|
boundingRect.x + 1,
|
||||||
boundingRect.y + 1,
|
boundingRect.y + 1,
|
||||||
boundingRect.width - 2,
|
boundingRect.width - 2,
|
||||||
boundingRect.height - 2,
|
boundingRect.height - 2,
|
||||||
]);
|
]);
|
||||||
});
|
};
|
||||||
|
|
||||||
document.getElementById('clipboard').removeAttribute('disabled');
|
document.getElementById('clipboard').removeAttribute('disabled');
|
||||||
document.getElementById('save-image').removeAttribute('disabled');
|
document.getElementById('save-image').removeAttribute('disabled');
|
||||||
|
|
|
||||||
|
|
@ -80,7 +80,6 @@ static GdkTexture *ls_web_view_copy_snapshot_to_texture(GtkWidget *widget,
|
||||||
struct _LSWebViewCopySnapshotUserData {
|
struct _LSWebViewCopySnapshotUserData {
|
||||||
LSWebView *self;
|
LSWebView *self;
|
||||||
GCancellable *cancellable;
|
GCancellable *cancellable;
|
||||||
guint callback_id;
|
|
||||||
float xoffset;
|
float xoffset;
|
||||||
float yoffset;
|
float yoffset;
|
||||||
float width;
|
float width;
|
||||||
|
|
@ -92,18 +91,12 @@ static void ls_web_view_copy_snapshot_to_clipboard(void *user_data) {
|
||||||
struct _LSWebViewCopySnapshotUserData *typed_user_data = (struct _LSWebViewCopySnapshotUserData *)user_data;
|
struct _LSWebViewCopySnapshotUserData *typed_user_data = (struct _LSWebViewCopySnapshotUserData *)user_data;
|
||||||
LSWebView *self = typed_user_data->self;
|
LSWebView *self = typed_user_data->self;
|
||||||
GCancellable *cancellable = typed_user_data->cancellable;
|
GCancellable *cancellable = typed_user_data->cancellable;
|
||||||
guint callback_id = typed_user_data->callback_id;
|
|
||||||
float xoffset = typed_user_data->xoffset;
|
float xoffset = typed_user_data->xoffset;
|
||||||
float yoffset = typed_user_data->yoffset;
|
float yoffset = typed_user_data->yoffset;
|
||||||
float width = typed_user_data->width;
|
float width = typed_user_data->width;
|
||||||
float height = typed_user_data->height;
|
float height = typed_user_data->height;
|
||||||
|
|
||||||
if (g_cancellable_is_cancelled(cancellable)) {
|
if (g_cancellable_is_cancelled(cancellable)) {
|
||||||
WebKitUserMessage *message =
|
|
||||||
webkit_user_message_new("_nativePromiseReject", g_variant_new("(i)", callback_id));
|
|
||||||
|
|
||||||
webkit_web_view_send_message_to_page(WEBKIT_WEB_VIEW(self), message, cancellable, NULL, NULL);
|
|
||||||
|
|
||||||
goto ls_webview_copy_snapshot_to_clipboard_exit;
|
goto ls_webview_copy_snapshot_to_clipboard_exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -112,11 +105,6 @@ static void ls_web_view_copy_snapshot_to_clipboard(void *user_data) {
|
||||||
GdkClipboard *clipboard = gdk_display_get_clipboard(gdk_display_get_default());
|
GdkClipboard *clipboard = gdk_display_get_clipboard(gdk_display_get_default());
|
||||||
gdk_clipboard_set_texture(clipboard, texture);
|
gdk_clipboard_set_texture(clipboard, texture);
|
||||||
|
|
||||||
WebKitUserMessage *message =
|
|
||||||
webkit_user_message_new("_nativePromiseResolve", g_variant_new("(i)", callback_id));
|
|
||||||
|
|
||||||
webkit_web_view_send_message_to_page(WEBKIT_WEB_VIEW(self), message, NULL, NULL, NULL);
|
|
||||||
|
|
||||||
ls_webview_copy_snapshot_to_clipboard_exit:
|
ls_webview_copy_snapshot_to_clipboard_exit:
|
||||||
if (self->cancellable == cancellable) {
|
if (self->cancellable == cancellable) {
|
||||||
g_object_unref(self->cancellable);
|
g_object_unref(self->cancellable);
|
||||||
|
|
@ -135,15 +123,9 @@ static void ls_web_view_copy_snapshot_to_file_after_save_dialog(GObject *so
|
||||||
struct _LSWebViewCopySnapshotUserData *typed_user_data = (struct _LSWebViewCopySnapshotUserData *)user_data;
|
struct _LSWebViewCopySnapshotUserData *typed_user_data = (struct _LSWebViewCopySnapshotUserData *)user_data;
|
||||||
LSWebView *self = typed_user_data->self;
|
LSWebView *self = typed_user_data->self;
|
||||||
GCancellable *cancellable = typed_user_data->cancellable;
|
GCancellable *cancellable = typed_user_data->cancellable;
|
||||||
guint callback_id = typed_user_data->callback_id;
|
|
||||||
GdkTexture *texture = typed_user_data->texture;
|
GdkTexture *texture = typed_user_data->texture;
|
||||||
|
|
||||||
if (g_cancellable_is_cancelled(cancellable)) {
|
if (g_cancellable_is_cancelled(cancellable)) {
|
||||||
WebKitUserMessage *message =
|
|
||||||
webkit_user_message_new("_nativePromiseReject", g_variant_new("(i)", callback_id));
|
|
||||||
|
|
||||||
webkit_web_view_send_message_to_page(WEBKIT_WEB_VIEW(self), message, cancellable, NULL, NULL);
|
|
||||||
|
|
||||||
goto ls_web_view_copy_snapshot_to_file_after_save_dialog_exit;
|
goto ls_web_view_copy_snapshot_to_file_after_save_dialog_exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -158,11 +140,6 @@ static void ls_web_view_copy_snapshot_to_file_after_save_dialog(GObject *so
|
||||||
char *path = g_file_get_path(file);
|
char *path = g_file_get_path(file);
|
||||||
gdk_texture_save_to_png(texture, path);
|
gdk_texture_save_to_png(texture, path);
|
||||||
|
|
||||||
WebKitUserMessage *message =
|
|
||||||
webkit_user_message_new("_nativePromiseResolve", g_variant_new("(i)", callback_id));
|
|
||||||
|
|
||||||
webkit_web_view_send_message_to_page(WEBKIT_WEB_VIEW(self), message, NULL, NULL, NULL);
|
|
||||||
|
|
||||||
ls_web_view_copy_snapshot_to_file_after_save_dialog_exit:
|
ls_web_view_copy_snapshot_to_file_after_save_dialog_exit:
|
||||||
if (self->cancellable == cancellable) {
|
if (self->cancellable == cancellable) {
|
||||||
g_object_unref(self->cancellable);
|
g_object_unref(self->cancellable);
|
||||||
|
|
@ -178,18 +155,12 @@ static void ls_web_view_copy_snapshot_to_file(void *user_data) {
|
||||||
struct _LSWebViewCopySnapshotUserData *typed_user_data = (struct _LSWebViewCopySnapshotUserData *)user_data;
|
struct _LSWebViewCopySnapshotUserData *typed_user_data = (struct _LSWebViewCopySnapshotUserData *)user_data;
|
||||||
LSWebView *self = typed_user_data->self;
|
LSWebView *self = typed_user_data->self;
|
||||||
GCancellable *cancellable = typed_user_data->cancellable;
|
GCancellable *cancellable = typed_user_data->cancellable;
|
||||||
guint callback_id = typed_user_data->callback_id;
|
|
||||||
float xoffset = typed_user_data->xoffset;
|
float xoffset = typed_user_data->xoffset;
|
||||||
float yoffset = typed_user_data->yoffset;
|
float yoffset = typed_user_data->yoffset;
|
||||||
float width = typed_user_data->width;
|
float width = typed_user_data->width;
|
||||||
float height = typed_user_data->height;
|
float height = typed_user_data->height;
|
||||||
|
|
||||||
if (g_cancellable_is_cancelled(cancellable)) {
|
if (g_cancellable_is_cancelled(cancellable)) {
|
||||||
WebKitUserMessage *message =
|
|
||||||
webkit_user_message_new("_nativePromiseReject", g_variant_new("(i)", callback_id));
|
|
||||||
|
|
||||||
webkit_web_view_send_message_to_page(WEBKIT_WEB_VIEW(self), message, cancellable, NULL, NULL);
|
|
||||||
|
|
||||||
if (self->cancellable == cancellable) {
|
if (self->cancellable == cancellable) {
|
||||||
g_object_unref(self->cancellable);
|
g_object_unref(self->cancellable);
|
||||||
self->cancellable = NULL;
|
self->cancellable = NULL;
|
||||||
|
|
@ -214,9 +185,7 @@ static void ls_web_view_on_alt_text_message_received(WebKitUserContentManager *m
|
||||||
JSCValue *value,
|
JSCValue *value,
|
||||||
gpointer user_data)
|
gpointer user_data)
|
||||||
{
|
{
|
||||||
JSCValue *callback_id_val = jsc_value_object_get_property_at_index(value, 0);
|
JSCValue *alt_text_val = jsc_value_object_get_property_at_index(value, 0);
|
||||||
JSCValue *alt_text_val = jsc_value_object_get_property_at_index(value, 1);
|
|
||||||
int callback_id = jsc_value_to_int32(callback_id_val);
|
|
||||||
char *alt_text = jsc_value_to_string(alt_text_val);
|
char *alt_text = jsc_value_to_string(alt_text_val);
|
||||||
|
|
||||||
GdkClipboard *clipboard = gdk_display_get_clipboard(gdk_display_get_default());
|
GdkClipboard *clipboard = gdk_display_get_clipboard(gdk_display_get_default());
|
||||||
|
|
@ -224,12 +193,6 @@ static void ls_web_view_on_alt_text_message_received(WebKitUserContentManager *m
|
||||||
|
|
||||||
LSWebView *self = LS_WEB_VIEW(user_data);
|
LSWebView *self = LS_WEB_VIEW(user_data);
|
||||||
|
|
||||||
WebKitUserMessage *message =
|
|
||||||
webkit_user_message_new("_nativePromiseResolve", g_variant_new("(i)", callback_id));
|
|
||||||
|
|
||||||
webkit_web_view_send_message_to_page(WEBKIT_WEB_VIEW(self), message, NULL, NULL, NULL);
|
|
||||||
|
|
||||||
g_object_unref(callback_id_val);
|
|
||||||
g_object_unref(alt_text_val);
|
g_object_unref(alt_text_val);
|
||||||
g_free(alt_text);
|
g_free(alt_text);
|
||||||
}
|
}
|
||||||
|
|
@ -238,13 +201,11 @@ static void ls_web_view_on_copy_snapshot_message_received(WebKitUserContentManag
|
||||||
JSCValue *value,
|
JSCValue *value,
|
||||||
gpointer user_data)
|
gpointer user_data)
|
||||||
{
|
{
|
||||||
JSCValue *callback_id_val = jsc_value_object_get_property_at_index(value, 0);
|
JSCValue *kind_val = jsc_value_object_get_property_at_index(value, 0);
|
||||||
JSCValue *kind_val = jsc_value_object_get_property_at_index(value, 1);
|
JSCValue *xoffset_val = jsc_value_object_get_property_at_index(value, 1);
|
||||||
JSCValue *xoffset_val = jsc_value_object_get_property_at_index(value, 2);
|
JSCValue *yoffset_val = jsc_value_object_get_property_at_index(value, 2);
|
||||||
JSCValue *yoffset_val = jsc_value_object_get_property_at_index(value, 3);
|
JSCValue *width_val = jsc_value_object_get_property_at_index(value, 3);
|
||||||
JSCValue *width_val = jsc_value_object_get_property_at_index(value, 4);
|
JSCValue *height_val = jsc_value_object_get_property_at_index(value, 4);
|
||||||
JSCValue *height_val = jsc_value_object_get_property_at_index(value, 5);
|
|
||||||
int callback_id = jsc_value_to_int32(callback_id_val);
|
|
||||||
char *kind = jsc_value_to_string(kind_val);
|
char *kind = jsc_value_to_string(kind_val);
|
||||||
float xoffset = (float)jsc_value_to_double(xoffset_val);
|
float xoffset = (float)jsc_value_to_double(xoffset_val);
|
||||||
float yoffset = (float)jsc_value_to_double(yoffset_val);
|
float yoffset = (float)jsc_value_to_double(yoffset_val);
|
||||||
|
|
@ -263,7 +224,6 @@ static void ls_web_view_on_copy_snapshot_message_received(WebKitUserContentManag
|
||||||
(struct _LSWebViewCopySnapshotUserData *)g_malloc(sizeof(struct _LSWebViewCopySnapshotUserData));
|
(struct _LSWebViewCopySnapshotUserData *)g_malloc(sizeof(struct _LSWebViewCopySnapshotUserData));
|
||||||
task_user_data->self = g_object_ref(self);
|
task_user_data->self = g_object_ref(self);
|
||||||
task_user_data->cancellable = g_object_ref(self->cancellable);
|
task_user_data->cancellable = g_object_ref(self->cancellable);
|
||||||
task_user_data->callback_id = callback_id;
|
|
||||||
task_user_data->xoffset = xoffset;
|
task_user_data->xoffset = xoffset;
|
||||||
task_user_data->yoffset = yoffset;
|
task_user_data->yoffset = yoffset;
|
||||||
task_user_data->width = width;
|
task_user_data->width = width;
|
||||||
|
|
@ -275,7 +235,6 @@ static void ls_web_view_on_copy_snapshot_message_received(WebKitUserContentManag
|
||||||
g_idle_add_once(ls_web_view_copy_snapshot_to_file, task_user_data);
|
g_idle_add_once(ls_web_view_copy_snapshot_to_file, task_user_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
g_object_unref(callback_id_val);
|
|
||||||
g_object_unref(kind_val);
|
g_object_unref(kind_val);
|
||||||
g_object_unref(xoffset_val);
|
g_object_unref(xoffset_val);
|
||||||
g_object_unref(yoffset_val);
|
g_object_unref(yoffset_val);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue