panda-core 0.12.5 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/README.md +53 -0
- data/app/assets/builds/panda_core_admin.css +2 -0
- data/app/assets/tailwind/application.css +236 -36
- data/app/builders/panda/core/form_builder.rb +120 -18
- data/app/components/panda/core/UI/badge.html.erb +10 -0
- data/app/components/panda/core/UI/badge.rb +14 -30
- data/app/components/panda/core/UI/button.html.erb +5 -0
- data/app/components/panda/core/UI/button.rb +11 -15
- data/app/components/panda/core/UI/card.html.erb +3 -0
- data/app/components/panda/core/UI/card.rb +7 -6
- data/app/components/panda/core/admin/attribute_diff_component.html.erb +21 -0
- data/app/components/panda/core/admin/attribute_diff_component.rb +29 -0
- data/app/components/panda/core/admin/breadcrumb_component.html.erb +36 -0
- data/app/components/panda/core/admin/breadcrumb_component.rb +12 -64
- data/app/components/panda/core/admin/breadcrumbs_component.html.erb +16 -0
- data/app/components/panda/core/admin/breadcrumbs_component.rb +26 -0
- data/app/components/panda/core/admin/button_component.html.erb +9 -0
- data/app/components/panda/core/admin/button_component.rb +36 -31
- data/app/components/panda/core/admin/callout_component.html.erb +17 -0
- data/app/components/panda/core/admin/callout_component.rb +57 -0
- data/app/components/panda/core/admin/code_block_component.html.erb +9 -0
- data/app/components/panda/core/admin/code_block_component.rb +23 -0
- data/app/components/panda/core/admin/container_component.html.erb +27 -0
- data/app/components/panda/core/admin/container_component.rb +11 -77
- data/app/components/panda/core/admin/contributors_summary_component.html.erb +15 -0
- data/app/components/panda/core/admin/contributors_summary_component.rb +23 -0
- data/app/components/panda/core/admin/dashboard_default_content_component.html.erb +70 -0
- data/app/components/panda/core/admin/dashboard_default_content_component.rb +32 -0
- data/app/components/panda/core/admin/delete_button_component.html.erb +4 -0
- data/app/components/panda/core/admin/delete_button_component.rb +41 -0
- data/app/components/panda/core/admin/dropdown_button_component.html.erb +28 -0
- data/app/components/panda/core/admin/dropdown_button_component.rb +100 -0
- data/app/components/panda/core/admin/dropdown_component.html.erb +32 -0
- data/app/components/panda/core/admin/dropdown_component.rb +65 -0
- data/app/components/panda/core/admin/empty_state_component.html.erb +16 -0
- data/app/components/panda/core/admin/empty_state_component.rb +25 -0
- data/app/components/panda/core/admin/file_gallery_component.html.erb +58 -0
- data/app/components/panda/core/admin/file_gallery_component.rb +26 -98
- data/app/components/panda/core/admin/filter_pills_component.html.erb +6 -0
- data/app/components/panda/core/admin/filter_pills_component.rb +29 -0
- data/app/components/panda/core/admin/flash_message_component.html.erb +11 -0
- data/app/components/panda/core/admin/flash_message_component.rb +33 -60
- data/app/components/panda/core/admin/form_error_component.html.erb +7 -0
- data/app/components/panda/core/admin/form_error_component.rb +8 -16
- data/app/components/panda/core/admin/form_footer_component.html.erb +16 -0
- data/app/components/panda/core/admin/form_footer_component.rb +65 -0
- data/app/components/panda/core/admin/form_input_component.html.erb +1 -0
- data/app/components/panda/core/admin/form_input_component.rb +21 -13
- data/app/components/panda/core/admin/form_section_component.html.erb +16 -0
- data/app/components/panda/core/admin/form_section_component.rb +64 -0
- data/app/components/panda/core/admin/form_select_component.html.erb +5 -0
- data/app/components/panda/core/admin/form_select_component.rb +25 -26
- data/app/components/panda/core/admin/heading_component.html.erb +26 -0
- data/app/components/panda/core/admin/heading_component.rb +23 -60
- data/app/components/panda/core/admin/login_form_component.html.erb +38 -0
- data/app/components/panda/core/admin/login_form_component.rb +28 -0
- data/app/components/panda/core/admin/main_layout_component.html.erb +88 -0
- data/app/components/panda/core/admin/main_layout_component.rb +19 -0
- data/app/components/panda/core/admin/my_profile/connected_account_component.html.erb +41 -0
- data/app/components/panda/core/admin/my_profile/connected_account_component.rb +27 -0
- data/app/components/panda/core/admin/navigation/item_component.html.erb +34 -0
- data/app/components/panda/core/admin/navigation/item_component.rb +69 -0
- data/app/components/panda/core/admin/navigation/sub_item_component.html.erb +9 -0
- data/app/components/panda/core/admin/navigation/sub_item_component.rb +44 -0
- data/app/components/panda/core/admin/navigation/user_menu_component.html.erb +30 -0
- data/app/components/panda/core/admin/navigation/user_menu_component.rb +36 -0
- data/app/components/panda/core/admin/page_header_component.html.erb +28 -0
- data/app/components/panda/core/admin/page_header_component.rb +19 -35
- data/app/components/panda/core/admin/pagination_component.html.erb +33 -0
- data/app/components/panda/core/admin/pagination_component.rb +56 -0
- data/app/components/panda/core/admin/panel_component.html.erb +19 -0
- data/app/components/panda/core/admin/panel_component.rb +4 -29
- data/app/components/panda/core/admin/prose_panel_component.html.erb +6 -0
- data/app/components/panda/core/admin/prose_panel_component.rb +43 -0
- data/app/components/panda/core/admin/search_bar_component.html.erb +19 -0
- data/app/components/panda/core/admin/search_bar_component.rb +31 -0
- data/app/components/panda/core/admin/search_filter_bar_component.html.erb +22 -0
- data/app/components/panda/core/admin/search_filter_bar_component.rb +27 -0
- data/app/components/panda/core/admin/secret_field_component.html.erb +24 -0
- data/app/components/panda/core/admin/secret_field_component.rb +31 -0
- data/app/components/panda/core/admin/sidebar_component.html.erb +160 -0
- data/app/components/panda/core/admin/sidebar_component.rb +55 -0
- data/app/components/panda/core/admin/slideover_component.html.erb +67 -0
- data/app/components/panda/core/admin/slideover_component.rb +31 -70
- data/app/components/panda/core/admin/statistics_component.html.erb +4 -0
- data/app/components/panda/core/admin/statistics_component.rb +6 -8
- data/app/components/panda/core/admin/tab_bar_component.html.erb +48 -0
- data/app/components/panda/core/admin/tab_bar_component.rb +32 -67
- data/app/components/panda/core/admin/table_component.html.erb +82 -0
- data/app/components/panda/core/admin/table_component.rb +54 -87
- data/app/components/panda/core/admin/tag_badge_component.html.erb +7 -0
- data/app/components/panda/core/admin/tag_badge_component.rb +21 -0
- data/app/components/panda/core/admin/tag_component.html.erb +1 -0
- data/app/components/panda/core/admin/tag_component.rb +51 -20
- data/app/components/panda/core/admin/tag_input_component.html.erb +37 -0
- data/app/components/panda/core/admin/tag_input_component.rb +22 -0
- data/app/components/panda/core/admin/tags_panel_component.html.erb +8 -0
- data/app/components/panda/core/admin/tags_panel_component.rb +33 -0
- data/app/components/panda/core/admin/user_activity_component.html.erb +15 -0
- data/app/components/panda/core/admin/user_activity_component.rb +9 -22
- data/app/components/panda/core/admin/user_display_component.html.erb +8 -0
- data/app/components/panda/core/admin/user_display_component.rb +19 -22
- data/app/components/panda/core/base.rb +35 -42
- data/app/components/panda/core/shared/footer_component.html.erb +2 -0
- data/app/components/panda/core/shared/footer_component.rb +11 -0
- data/app/components/panda/core/shared/header_component.html.erb +32 -0
- data/app/components/panda/core/shared/header_component.rb +19 -0
- data/app/constraints/panda/core/admin_constraint.rb +2 -2
- data/app/controllers/concerns/panda/core/admin_authorization.rb +58 -0
- data/app/controllers/concerns/panda/core/authorizable.rb +107 -0
- data/app/controllers/panda/core/admin/base_controller.rb +62 -3
- data/app/controllers/panda/core/admin/editor/search_controller.rb +17 -0
- data/app/controllers/panda/core/admin/feature_flags_controller.rb +35 -0
- data/app/controllers/panda/core/admin/file_categories_controller.rb +84 -0
- data/app/controllers/panda/core/admin/files_controller.rb +230 -0
- data/app/controllers/panda/core/admin/import_sessions_controller.rb +120 -0
- data/app/controllers/panda/core/admin/my_profile/logins_controller.rb +24 -0
- data/app/controllers/panda/core/admin/my_profile_controller.rb +31 -0
- data/app/controllers/panda/core/admin/search_controller.rb +15 -0
- data/app/controllers/panda/core/admin/sessions_controller.rb +84 -10
- data/app/controllers/panda/core/admin/tags_controller.rb +77 -0
- data/app/controllers/panda/core/admin/test_sessions_controller.rb +5 -6
- data/app/controllers/panda/core/admin/users_controller.rb +184 -0
- data/app/controllers/panda/core/application_controller.rb +13 -1
- data/app/helpers/panda/core/active_storage_helper.rb +24 -0
- data/app/helpers/panda/core/feature_flag_helper.rb +11 -0
- data/app/helpers/panda/core/form_helper.rb +15 -0
- data/app/javascript/panda/core/controllers/avatar_upload_controller.js +73 -0
- data/app/javascript/panda/core/controllers/clipboard_controller.js +119 -0
- data/app/javascript/panda/core/controllers/collapsible_item_controller.js +45 -0
- data/app/javascript/panda/core/controllers/custom_select_controller.js +256 -0
- data/app/javascript/panda/core/controllers/datepicker_controller.js +121 -0
- data/app/javascript/panda/core/controllers/daterange_controller.js +193 -0
- data/app/javascript/panda/core/controllers/file_gallery_controller.js +134 -0
- data/app/javascript/panda/core/controllers/file_upload_controller.js +165 -0
- data/app/javascript/panda/core/controllers/global_search_controller.js +184 -0
- data/app/javascript/panda/core/controllers/index.js +36 -0
- data/app/javascript/panda/core/controllers/mobile_sidebar_controller.js +58 -0
- data/app/javascript/panda/core/controllers/navigation_toggle_controller.js +49 -24
- data/app/javascript/panda/core/controllers/row_link_controller.js +27 -0
- data/app/javascript/panda/core/controllers/tag_input_controller.js +183 -0
- data/app/javascript/panda/core/tailwindcss-stimulus-components.js +2 -3
- data/app/javascript/panda/core/vendor/@fortawesome--fontawesome-free@7.2.0.js +8129 -0
- data/app/javascript/panda/core/vendor/@hotwired--turbo.js +7197 -100
- data/app/javascript/panda/core/vendor/@rails--actioncable--src.js +511 -2
- data/app/javascript/panda/core/vendor/@tailwindplus--elements@1.0.22.js +65 -0
- data/app/javascript/panda/core/vendor/cropperjs@2.1.0.js +3 -0
- data/app/javascript/panda/core/vendor/vanilla-calendar-pro.js +2 -0
- data/app/models/concerns/panda/core/has_metadata.rb +164 -0
- data/app/models/concerns/panda/core/importable.rb +79 -0
- data/app/models/concerns/panda/core/searchable.rb +66 -0
- data/app/models/concerns/panda/core/taggable.rb +37 -0
- data/app/models/panda/core/feature_flag.rb +56 -0
- data/app/models/panda/core/file_categorization.rb +14 -0
- data/app/models/panda/core/file_category.rb +45 -0
- data/app/models/panda/core/import_session.rb +79 -0
- data/app/models/panda/core/presence.rb +24 -0
- data/app/models/panda/core/tag.rb +33 -0
- data/app/models/panda/core/tagging.rb +14 -0
- data/app/models/panda/core/user.rb +94 -16
- data/app/models/panda/core/user_activity.rb +42 -0
- data/app/models/panda/core/user_session.rb +35 -0
- data/app/services/panda/core/attach_avatar_service.rb +15 -3
- data/app/services/panda/core/csv_import_service.rb +65 -0
- data/app/services/panda/core/file_categorizer.rb +52 -0
- data/app/services/panda/core/file_parser.rb +76 -0
- data/app/services/panda/core/invite_user_service.rb +44 -0
- data/app/services/panda/core/presence_service.rb +49 -0
- data/app/views/layouts/panda/core/admin.html.erb +6 -76
- data/app/views/layouts/panda/core/admin_simple.html.erb +2 -2
- data/app/views/panda/core/admin/dashboard/_default_content.html.erb +1 -73
- data/app/views/panda/core/admin/dashboard/show.html.erb +12 -15
- data/app/views/panda/core/admin/feature_flags/index.html.erb +56 -0
- data/app/views/panda/core/admin/file_categories/_form.html.erb +20 -0
- data/app/views/panda/core/admin/file_categories/edit.html.erb +5 -0
- data/app/views/panda/core/admin/file_categories/index.html.erb +55 -0
- data/app/views/panda/core/admin/file_categories/new.html.erb +5 -0
- data/app/views/panda/core/admin/files/_file_details.html.erb +156 -0
- data/app/views/panda/core/admin/files/index.html.erb +61 -0
- data/app/views/panda/core/admin/import_sessions/column_map.html.erb +43 -0
- data/app/views/panda/core/admin/import_sessions/index.html.erb +48 -0
- data/app/views/panda/core/admin/import_sessions/new.html.erb +37 -0
- data/app/views/panda/core/admin/import_sessions/preview.html.erb +51 -0
- data/app/views/panda/core/admin/import_sessions/show.html.erb +87 -0
- data/app/views/panda/core/admin/my_profile/edit.html.erb +51 -39
- data/app/views/panda/core/admin/my_profile/logins/show.html.erb +72 -0
- data/app/views/panda/core/admin/sessions/new.html.erb +1 -37
- data/app/views/panda/core/admin/shared/_breadcrumbs.html.erb +1 -18
- data/app/views/panda/core/admin/shared/_sidebar.html.erb +1 -99
- data/app/views/panda/core/admin/shared/_slideover.html.erb +3 -3
- data/app/views/panda/core/admin/tags/_form.html.erb +21 -0
- data/app/views/panda/core/admin/tags/edit.html.erb +4 -0
- data/app/views/panda/core/admin/tags/index.html.erb +41 -0
- data/app/views/panda/core/admin/tags/new.html.erb +4 -0
- data/app/views/panda/core/admin/users/activity.html.erb +90 -0
- data/app/views/panda/core/admin/users/edit.html.erb +48 -0
- data/app/views/panda/core/admin/users/index.html.erb +161 -0
- data/app/views/panda/core/admin/users/sessions.html.erb +103 -0
- data/app/views/panda/core/admin/users/show.html.erb +224 -0
- data/app/views/panda/core/shared/_footer.html.erb +1 -2
- data/app/views/panda/core/shared/_header.html.erb +4 -22
- data/config/brakeman.ignore +68 -31
- data/config/importmap.rb +13 -11
- data/config/routes.rb +49 -2
- data/db/migrate/20260202171614_create_panda_core_file_categories.rb +30 -0
- data/db/migrate/20260203000001_add_user_management_fields_to_panda_core_users.rb +21 -0
- data/db/migrate/20260203000002_create_panda_core_user_activities.rb +24 -0
- data/db/migrate/20260203000003_create_panda_core_user_sessions.rb +25 -0
- data/db/migrate/20260207100000_create_panda_core_presences.rb +21 -0
- data/db/migrate/20260213000001_fix_active_storage_attachments_record_id_type.rb +44 -0
- data/db/migrate/20260217000001_create_panda_core_feature_flags.rb +14 -0
- data/db/migrate/20260303000001_add_metadata_to_panda_core_users.rb +11 -0
- data/db/migrate/20260310000001_create_panda_core_tags.rb +27 -0
- data/db/migrate/20260310000002_create_panda_core_import_sessions.rb +29 -0
- data/lib/generators/panda/core/install_generator.rb +32 -0
- data/lib/generators/panda/core/templates/panda.rb +63 -0
- data/lib/panda/core/asset_loader.rb +5 -2
- data/lib/panda/core/authentication.rb +3 -3
- data/lib/panda/core/configuration.rb +195 -1
- data/lib/panda/core/engine/admin_controller_config.rb +3 -6
- data/lib/panda/core/engine/autoload_config.rb +21 -9
- data/lib/panda/core/engine/omniauth_config.rb +108 -38
- data/lib/panda/core/engine/pending_migrations_check.rb +46 -0
- data/lib/panda/core/engine/sqlite_schema_compatibility.rb +36 -0
- data/lib/panda/core/engine/{phlex_config.rb → view_component_config.rb} +5 -7
- data/lib/panda/core/engine.rb +90 -25
- data/lib/panda/core/middleware.rb +35 -0
- data/lib/panda/core/navigation_registry.rb +455 -0
- data/lib/panda/core/oauth_providers.rb +27 -0
- data/lib/panda/core/permission_registry.rb +89 -0
- data/lib/panda/core/search_registry.rb +69 -0
- data/lib/panda/core/seeds/file_categories.rb +30 -0
- data/lib/panda/core/shared/inflections_config.rb +1 -5
- data/lib/panda/core/testing/rails_helper.rb +87 -0
- data/lib/panda/core/testing/support/authentication_test_helpers.rb +22 -18
- data/lib/panda/core/testing/support/system/cuprite_helpers.rb +4 -4
- data/lib/panda/core/testing/view_component_test_controller.rb +19 -0
- data/lib/panda/core/version.rb +1 -1
- data/lib/panda/core/widget_registry.rb +50 -0
- data/lib/panda/core.rb +17 -8
- data/lib/tasks/panda/core/file_categories.rake +41 -0
- data/public/panda-core-assets/panda-core-0.12.5.css +2 -0
- data/public/panda-core-assets/panda-core-0.13.0.css +2 -0
- data/public/panda-core-assets/panda-core-0.14.0.css +2 -0
- data/public/panda-core-assets/panda-core-0.14.4.css +2 -0
- data/public/panda-core-assets/panda-core.css +2 -2
- data/public/panda-core-assets/vanilla-calendar.css +47 -0
- metadata +203 -26
- data/lib/panda/core/engine/inflections_config.rb +0 -23
|
@@ -1,4 +1,513 @@
|
|
|
1
|
-
// @rails/actioncable
|
|
1
|
+
// @rails/actioncable@8.1.300 vendored from npm
|
|
2
|
+
var adapters = {
|
|
3
|
+
logger: typeof console !== "undefined" ? console : undefined,
|
|
4
|
+
WebSocket: typeof WebSocket !== "undefined" ? WebSocket : undefined
|
|
5
|
+
};
|
|
2
6
|
|
|
3
|
-
var
|
|
7
|
+
var logger = {
|
|
8
|
+
log(...messages) {
|
|
9
|
+
if (this.enabled) {
|
|
10
|
+
messages.push(Date.now());
|
|
11
|
+
adapters.logger.log("[ActionCable]", ...messages);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
};
|
|
4
15
|
|
|
16
|
+
const now = () => (new Date).getTime();
|
|
17
|
+
|
|
18
|
+
const secondsSince = time => (now() - time) / 1e3;
|
|
19
|
+
|
|
20
|
+
class ConnectionMonitor {
|
|
21
|
+
constructor(connection) {
|
|
22
|
+
this.visibilityDidChange = this.visibilityDidChange.bind(this);
|
|
23
|
+
this.connection = connection;
|
|
24
|
+
this.reconnectAttempts = 0;
|
|
25
|
+
}
|
|
26
|
+
start() {
|
|
27
|
+
if (!this.isRunning()) {
|
|
28
|
+
this.startedAt = now();
|
|
29
|
+
delete this.stoppedAt;
|
|
30
|
+
this.startPolling();
|
|
31
|
+
addEventListener("visibilitychange", this.visibilityDidChange);
|
|
32
|
+
logger.log(`ConnectionMonitor started. stale threshold = ${this.constructor.staleThreshold} s`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
stop() {
|
|
36
|
+
if (this.isRunning()) {
|
|
37
|
+
this.stoppedAt = now();
|
|
38
|
+
this.stopPolling();
|
|
39
|
+
removeEventListener("visibilitychange", this.visibilityDidChange);
|
|
40
|
+
logger.log("ConnectionMonitor stopped");
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
isRunning() {
|
|
44
|
+
return this.startedAt && !this.stoppedAt;
|
|
45
|
+
}
|
|
46
|
+
recordMessage() {
|
|
47
|
+
this.pingedAt = now();
|
|
48
|
+
}
|
|
49
|
+
recordConnect() {
|
|
50
|
+
this.reconnectAttempts = 0;
|
|
51
|
+
delete this.disconnectedAt;
|
|
52
|
+
logger.log("ConnectionMonitor recorded connect");
|
|
53
|
+
}
|
|
54
|
+
recordDisconnect() {
|
|
55
|
+
this.disconnectedAt = now();
|
|
56
|
+
logger.log("ConnectionMonitor recorded disconnect");
|
|
57
|
+
}
|
|
58
|
+
startPolling() {
|
|
59
|
+
this.stopPolling();
|
|
60
|
+
this.poll();
|
|
61
|
+
}
|
|
62
|
+
stopPolling() {
|
|
63
|
+
clearTimeout(this.pollTimeout);
|
|
64
|
+
}
|
|
65
|
+
poll() {
|
|
66
|
+
this.pollTimeout = setTimeout((() => {
|
|
67
|
+
this.reconnectIfStale();
|
|
68
|
+
this.poll();
|
|
69
|
+
}), this.getPollInterval());
|
|
70
|
+
}
|
|
71
|
+
getPollInterval() {
|
|
72
|
+
const {staleThreshold: staleThreshold, reconnectionBackoffRate: reconnectionBackoffRate} = this.constructor;
|
|
73
|
+
const backoff = Math.pow(1 + reconnectionBackoffRate, Math.min(this.reconnectAttempts, 10));
|
|
74
|
+
const jitterMax = this.reconnectAttempts === 0 ? 1 : reconnectionBackoffRate;
|
|
75
|
+
const jitter = jitterMax * Math.random();
|
|
76
|
+
return staleThreshold * 1e3 * backoff * (1 + jitter);
|
|
77
|
+
}
|
|
78
|
+
reconnectIfStale() {
|
|
79
|
+
if (this.connectionIsStale()) {
|
|
80
|
+
logger.log(`ConnectionMonitor detected stale connection. reconnectAttempts = ${this.reconnectAttempts}, time stale = ${secondsSince(this.refreshedAt)} s, stale threshold = ${this.constructor.staleThreshold} s`);
|
|
81
|
+
this.reconnectAttempts++;
|
|
82
|
+
if (this.disconnectedRecently()) {
|
|
83
|
+
logger.log(`ConnectionMonitor skipping reopening recent disconnect. time disconnected = ${secondsSince(this.disconnectedAt)} s`);
|
|
84
|
+
} else {
|
|
85
|
+
logger.log("ConnectionMonitor reopening");
|
|
86
|
+
this.connection.reopen();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
get refreshedAt() {
|
|
91
|
+
return this.pingedAt ? this.pingedAt : this.startedAt;
|
|
92
|
+
}
|
|
93
|
+
connectionIsStale() {
|
|
94
|
+
return secondsSince(this.refreshedAt) > this.constructor.staleThreshold;
|
|
95
|
+
}
|
|
96
|
+
disconnectedRecently() {
|
|
97
|
+
return this.disconnectedAt && secondsSince(this.disconnectedAt) < this.constructor.staleThreshold;
|
|
98
|
+
}
|
|
99
|
+
visibilityDidChange() {
|
|
100
|
+
if (document.visibilityState === "visible") {
|
|
101
|
+
setTimeout((() => {
|
|
102
|
+
if (this.connectionIsStale() || !this.connection.isOpen()) {
|
|
103
|
+
logger.log(`ConnectionMonitor reopening stale connection on visibilitychange. visibilityState = ${document.visibilityState}`);
|
|
104
|
+
this.connection.reopen();
|
|
105
|
+
}
|
|
106
|
+
}), 200);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
ConnectionMonitor.staleThreshold = 6;
|
|
112
|
+
|
|
113
|
+
ConnectionMonitor.reconnectionBackoffRate = .15;
|
|
114
|
+
|
|
115
|
+
var INTERNAL = {
|
|
116
|
+
message_types: {
|
|
117
|
+
welcome: "welcome",
|
|
118
|
+
disconnect: "disconnect",
|
|
119
|
+
ping: "ping",
|
|
120
|
+
confirmation: "confirm_subscription",
|
|
121
|
+
rejection: "reject_subscription"
|
|
122
|
+
},
|
|
123
|
+
disconnect_reasons: {
|
|
124
|
+
unauthorized: "unauthorized",
|
|
125
|
+
invalid_request: "invalid_request",
|
|
126
|
+
server_restart: "server_restart",
|
|
127
|
+
remote: "remote"
|
|
128
|
+
},
|
|
129
|
+
default_mount_path: "/cable",
|
|
130
|
+
protocols: [ "actioncable-v1-json", "actioncable-unsupported" ]
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const {message_types: message_types, protocols: protocols} = INTERNAL;
|
|
134
|
+
|
|
135
|
+
const supportedProtocols = protocols.slice(0, protocols.length - 1);
|
|
136
|
+
|
|
137
|
+
const indexOf = [].indexOf;
|
|
138
|
+
|
|
139
|
+
class Connection {
|
|
140
|
+
constructor(consumer) {
|
|
141
|
+
this.open = this.open.bind(this);
|
|
142
|
+
this.consumer = consumer;
|
|
143
|
+
this.subscriptions = this.consumer.subscriptions;
|
|
144
|
+
this.monitor = new ConnectionMonitor(this);
|
|
145
|
+
this.disconnected = true;
|
|
146
|
+
}
|
|
147
|
+
send(data) {
|
|
148
|
+
if (this.isOpen()) {
|
|
149
|
+
this.webSocket.send(JSON.stringify(data));
|
|
150
|
+
return true;
|
|
151
|
+
} else {
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
open() {
|
|
156
|
+
if (this.isActive()) {
|
|
157
|
+
logger.log(`Attempted to open WebSocket, but existing socket is ${this.getState()}`);
|
|
158
|
+
return false;
|
|
159
|
+
} else {
|
|
160
|
+
const socketProtocols = [ ...protocols, ...this.consumer.subprotocols || [] ];
|
|
161
|
+
logger.log(`Opening WebSocket, current state is ${this.getState()}, subprotocols: ${socketProtocols}`);
|
|
162
|
+
if (this.webSocket) {
|
|
163
|
+
this.uninstallEventHandlers();
|
|
164
|
+
}
|
|
165
|
+
this.webSocket = new adapters.WebSocket(this.consumer.url, socketProtocols);
|
|
166
|
+
this.installEventHandlers();
|
|
167
|
+
this.monitor.start();
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
close({allowReconnect: allowReconnect} = {
|
|
172
|
+
allowReconnect: true
|
|
173
|
+
}) {
|
|
174
|
+
if (!allowReconnect) {
|
|
175
|
+
this.monitor.stop();
|
|
176
|
+
}
|
|
177
|
+
if (this.isOpen()) {
|
|
178
|
+
return this.webSocket.close();
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
reopen() {
|
|
182
|
+
logger.log(`Reopening WebSocket, current state is ${this.getState()}`);
|
|
183
|
+
if (this.isActive()) {
|
|
184
|
+
try {
|
|
185
|
+
return this.close();
|
|
186
|
+
} catch (error) {
|
|
187
|
+
logger.log("Failed to reopen WebSocket", error);
|
|
188
|
+
} finally {
|
|
189
|
+
logger.log(`Reopening WebSocket in ${this.constructor.reopenDelay}ms`);
|
|
190
|
+
setTimeout(this.open, this.constructor.reopenDelay);
|
|
191
|
+
}
|
|
192
|
+
} else {
|
|
193
|
+
return this.open();
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
getProtocol() {
|
|
197
|
+
if (this.webSocket) {
|
|
198
|
+
return this.webSocket.protocol;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
isOpen() {
|
|
202
|
+
return this.isState("open");
|
|
203
|
+
}
|
|
204
|
+
isActive() {
|
|
205
|
+
return this.isState("open", "connecting");
|
|
206
|
+
}
|
|
207
|
+
triedToReconnect() {
|
|
208
|
+
return this.monitor.reconnectAttempts > 0;
|
|
209
|
+
}
|
|
210
|
+
isProtocolSupported() {
|
|
211
|
+
return indexOf.call(supportedProtocols, this.getProtocol()) >= 0;
|
|
212
|
+
}
|
|
213
|
+
isState(...states) {
|
|
214
|
+
return indexOf.call(states, this.getState()) >= 0;
|
|
215
|
+
}
|
|
216
|
+
getState() {
|
|
217
|
+
if (this.webSocket) {
|
|
218
|
+
for (let state in adapters.WebSocket) {
|
|
219
|
+
if (adapters.WebSocket[state] === this.webSocket.readyState) {
|
|
220
|
+
return state.toLowerCase();
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
installEventHandlers() {
|
|
227
|
+
for (let eventName in this.events) {
|
|
228
|
+
const handler = this.events[eventName].bind(this);
|
|
229
|
+
this.webSocket[`on${eventName}`] = handler;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
uninstallEventHandlers() {
|
|
233
|
+
for (let eventName in this.events) {
|
|
234
|
+
this.webSocket[`on${eventName}`] = function() {};
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
Connection.reopenDelay = 500;
|
|
240
|
+
|
|
241
|
+
Connection.prototype.events = {
|
|
242
|
+
message(event) {
|
|
243
|
+
if (!this.isProtocolSupported()) {
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
const {identifier: identifier, message: message, reason: reason, reconnect: reconnect, type: type} = JSON.parse(event.data);
|
|
247
|
+
this.monitor.recordMessage();
|
|
248
|
+
switch (type) {
|
|
249
|
+
case message_types.welcome:
|
|
250
|
+
if (this.triedToReconnect()) {
|
|
251
|
+
this.reconnectAttempted = true;
|
|
252
|
+
}
|
|
253
|
+
this.monitor.recordConnect();
|
|
254
|
+
return this.subscriptions.reload();
|
|
255
|
+
|
|
256
|
+
case message_types.disconnect:
|
|
257
|
+
logger.log(`Disconnecting. Reason: ${reason}`);
|
|
258
|
+
return this.close({
|
|
259
|
+
allowReconnect: reconnect
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
case message_types.ping:
|
|
263
|
+
return null;
|
|
264
|
+
|
|
265
|
+
case message_types.confirmation:
|
|
266
|
+
this.subscriptions.confirmSubscription(identifier);
|
|
267
|
+
if (this.reconnectAttempted) {
|
|
268
|
+
this.reconnectAttempted = false;
|
|
269
|
+
return this.subscriptions.notify(identifier, "connected", {
|
|
270
|
+
reconnected: true
|
|
271
|
+
});
|
|
272
|
+
} else {
|
|
273
|
+
return this.subscriptions.notify(identifier, "connected", {
|
|
274
|
+
reconnected: false
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
case message_types.rejection:
|
|
279
|
+
return this.subscriptions.reject(identifier);
|
|
280
|
+
|
|
281
|
+
default:
|
|
282
|
+
return this.subscriptions.notify(identifier, "received", message);
|
|
283
|
+
}
|
|
284
|
+
},
|
|
285
|
+
open() {
|
|
286
|
+
logger.log(`WebSocket onopen event, using '${this.getProtocol()}' subprotocol`);
|
|
287
|
+
this.disconnected = false;
|
|
288
|
+
if (!this.isProtocolSupported()) {
|
|
289
|
+
logger.log("Protocol is unsupported. Stopping monitor and disconnecting.");
|
|
290
|
+
return this.close({
|
|
291
|
+
allowReconnect: false
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
},
|
|
295
|
+
close(event) {
|
|
296
|
+
logger.log("WebSocket onclose event");
|
|
297
|
+
if (this.disconnected) {
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
this.disconnected = true;
|
|
301
|
+
this.monitor.recordDisconnect();
|
|
302
|
+
return this.subscriptions.notifyAll("disconnected", {
|
|
303
|
+
willAttemptReconnect: this.monitor.isRunning()
|
|
304
|
+
});
|
|
305
|
+
},
|
|
306
|
+
error() {
|
|
307
|
+
logger.log("WebSocket onerror event");
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
const extend = function(object, properties) {
|
|
312
|
+
if (properties != null) {
|
|
313
|
+
for (let key in properties) {
|
|
314
|
+
const value = properties[key];
|
|
315
|
+
object[key] = value;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
return object;
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
class Subscription {
|
|
322
|
+
constructor(consumer, params = {}, mixin) {
|
|
323
|
+
this.consumer = consumer;
|
|
324
|
+
this.identifier = JSON.stringify(params);
|
|
325
|
+
extend(this, mixin);
|
|
326
|
+
}
|
|
327
|
+
perform(action, data = {}) {
|
|
328
|
+
data.action = action;
|
|
329
|
+
return this.send(data);
|
|
330
|
+
}
|
|
331
|
+
send(data) {
|
|
332
|
+
return this.consumer.send({
|
|
333
|
+
command: "message",
|
|
334
|
+
identifier: this.identifier,
|
|
335
|
+
data: JSON.stringify(data)
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
unsubscribe() {
|
|
339
|
+
return this.consumer.subscriptions.remove(this);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
class SubscriptionGuarantor {
|
|
344
|
+
constructor(subscriptions) {
|
|
345
|
+
this.subscriptions = subscriptions;
|
|
346
|
+
this.pendingSubscriptions = [];
|
|
347
|
+
}
|
|
348
|
+
guarantee(subscription) {
|
|
349
|
+
if (this.pendingSubscriptions.indexOf(subscription) == -1) {
|
|
350
|
+
logger.log(`SubscriptionGuarantor guaranteeing ${subscription.identifier}`);
|
|
351
|
+
this.pendingSubscriptions.push(subscription);
|
|
352
|
+
} else {
|
|
353
|
+
logger.log(`SubscriptionGuarantor already guaranteeing ${subscription.identifier}`);
|
|
354
|
+
}
|
|
355
|
+
this.startGuaranteeing();
|
|
356
|
+
}
|
|
357
|
+
forget(subscription) {
|
|
358
|
+
logger.log(`SubscriptionGuarantor forgetting ${subscription.identifier}`);
|
|
359
|
+
this.pendingSubscriptions = this.pendingSubscriptions.filter((s => s !== subscription));
|
|
360
|
+
}
|
|
361
|
+
startGuaranteeing() {
|
|
362
|
+
this.stopGuaranteeing();
|
|
363
|
+
this.retrySubscribing();
|
|
364
|
+
}
|
|
365
|
+
stopGuaranteeing() {
|
|
366
|
+
clearTimeout(this.retryTimeout);
|
|
367
|
+
}
|
|
368
|
+
retrySubscribing() {
|
|
369
|
+
this.retryTimeout = setTimeout((() => {
|
|
370
|
+
if (this.subscriptions && typeof this.subscriptions.subscribe === "function") {
|
|
371
|
+
this.pendingSubscriptions.map((subscription => {
|
|
372
|
+
logger.log(`SubscriptionGuarantor resubscribing ${subscription.identifier}`);
|
|
373
|
+
this.subscriptions.subscribe(subscription);
|
|
374
|
+
}));
|
|
375
|
+
}
|
|
376
|
+
}), 500);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
class Subscriptions {
|
|
381
|
+
constructor(consumer) {
|
|
382
|
+
this.consumer = consumer;
|
|
383
|
+
this.guarantor = new SubscriptionGuarantor(this);
|
|
384
|
+
this.subscriptions = [];
|
|
385
|
+
}
|
|
386
|
+
create(channelName, mixin) {
|
|
387
|
+
const channel = channelName;
|
|
388
|
+
const params = typeof channel === "object" ? channel : {
|
|
389
|
+
channel: channel
|
|
390
|
+
};
|
|
391
|
+
const subscription = new Subscription(this.consumer, params, mixin);
|
|
392
|
+
return this.add(subscription);
|
|
393
|
+
}
|
|
394
|
+
add(subscription) {
|
|
395
|
+
this.subscriptions.push(subscription);
|
|
396
|
+
this.consumer.ensureActiveConnection();
|
|
397
|
+
this.notify(subscription, "initialized");
|
|
398
|
+
this.subscribe(subscription);
|
|
399
|
+
return subscription;
|
|
400
|
+
}
|
|
401
|
+
remove(subscription) {
|
|
402
|
+
this.forget(subscription);
|
|
403
|
+
if (!this.findAll(subscription.identifier).length) {
|
|
404
|
+
this.sendCommand(subscription, "unsubscribe");
|
|
405
|
+
}
|
|
406
|
+
return subscription;
|
|
407
|
+
}
|
|
408
|
+
reject(identifier) {
|
|
409
|
+
return this.findAll(identifier).map((subscription => {
|
|
410
|
+
this.forget(subscription);
|
|
411
|
+
this.notify(subscription, "rejected");
|
|
412
|
+
return subscription;
|
|
413
|
+
}));
|
|
414
|
+
}
|
|
415
|
+
forget(subscription) {
|
|
416
|
+
this.guarantor.forget(subscription);
|
|
417
|
+
this.subscriptions = this.subscriptions.filter((s => s !== subscription));
|
|
418
|
+
return subscription;
|
|
419
|
+
}
|
|
420
|
+
findAll(identifier) {
|
|
421
|
+
return this.subscriptions.filter((s => s.identifier === identifier));
|
|
422
|
+
}
|
|
423
|
+
reload() {
|
|
424
|
+
return this.subscriptions.map((subscription => this.subscribe(subscription)));
|
|
425
|
+
}
|
|
426
|
+
notifyAll(callbackName, ...args) {
|
|
427
|
+
return this.subscriptions.map((subscription => this.notify(subscription, callbackName, ...args)));
|
|
428
|
+
}
|
|
429
|
+
notify(subscription, callbackName, ...args) {
|
|
430
|
+
let subscriptions;
|
|
431
|
+
if (typeof subscription === "string") {
|
|
432
|
+
subscriptions = this.findAll(subscription);
|
|
433
|
+
} else {
|
|
434
|
+
subscriptions = [ subscription ];
|
|
435
|
+
}
|
|
436
|
+
return subscriptions.map((subscription => typeof subscription[callbackName] === "function" ? subscription[callbackName](...args) : undefined));
|
|
437
|
+
}
|
|
438
|
+
subscribe(subscription) {
|
|
439
|
+
if (this.sendCommand(subscription, "subscribe")) {
|
|
440
|
+
this.guarantor.guarantee(subscription);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
confirmSubscription(identifier) {
|
|
444
|
+
logger.log(`Subscription confirmed ${identifier}`);
|
|
445
|
+
this.findAll(identifier).map((subscription => this.guarantor.forget(subscription)));
|
|
446
|
+
}
|
|
447
|
+
sendCommand(subscription, command) {
|
|
448
|
+
const {identifier: identifier} = subscription;
|
|
449
|
+
return this.consumer.send({
|
|
450
|
+
command: command,
|
|
451
|
+
identifier: identifier
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
class Consumer {
|
|
457
|
+
constructor(url) {
|
|
458
|
+
this._url = url;
|
|
459
|
+
this.subscriptions = new Subscriptions(this);
|
|
460
|
+
this.connection = new Connection(this);
|
|
461
|
+
this.subprotocols = [];
|
|
462
|
+
}
|
|
463
|
+
get url() {
|
|
464
|
+
return createWebSocketURL(this._url);
|
|
465
|
+
}
|
|
466
|
+
send(data) {
|
|
467
|
+
return this.connection.send(data);
|
|
468
|
+
}
|
|
469
|
+
connect() {
|
|
470
|
+
return this.connection.open();
|
|
471
|
+
}
|
|
472
|
+
disconnect() {
|
|
473
|
+
return this.connection.close({
|
|
474
|
+
allowReconnect: false
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
ensureActiveConnection() {
|
|
478
|
+
if (!this.connection.isActive()) {
|
|
479
|
+
return this.connection.open();
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
addSubProtocol(subprotocol) {
|
|
483
|
+
this.subprotocols = [ ...this.subprotocols, subprotocol ];
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
function createWebSocketURL(url) {
|
|
488
|
+
if (typeof url === "function") {
|
|
489
|
+
url = url();
|
|
490
|
+
}
|
|
491
|
+
if (url && !/^wss?:/i.test(url)) {
|
|
492
|
+
const a = document.createElement("a");
|
|
493
|
+
a.href = url;
|
|
494
|
+
a.href = a.href;
|
|
495
|
+
a.protocol = a.protocol.replace("http", "ws");
|
|
496
|
+
return a.href;
|
|
497
|
+
} else {
|
|
498
|
+
return url;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
function createConsumer(url = getConfig("url") || INTERNAL.default_mount_path) {
|
|
503
|
+
return new Consumer(url);
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
function getConfig(name) {
|
|
507
|
+
const element = document.head.querySelector(`meta[name='action-cable-${name}']`);
|
|
508
|
+
if (element) {
|
|
509
|
+
return element.getAttribute("content");
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
export { Connection, ConnectionMonitor, Consumer, INTERNAL, Subscription, SubscriptionGuarantor, Subscriptions, adapters, createConsumer, createWebSocketURL, getConfig, logger };
|