Skip to content

WASM Support: Migrate from package:js to dart:js_interop #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 22 additions & 2 deletions example/.metadata
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,27 @@
# This file should be version controlled and should not be manually edited.

version:
revision: ffb2ecea5223acdd139a5039be2f9c796962833d
channel: stable
revision: "c519ee916eaeb88923e67befb89c0f1dabfa83e6"
channel: "stable"

project_type: app

# Tracks metadata for the flutter migrate command
migration:
platforms:
- platform: root
create_revision: c519ee916eaeb88923e67befb89c0f1dabfa83e6
base_revision: c519ee916eaeb88923e67befb89c0f1dabfa83e6
- platform: web
create_revision: c519ee916eaeb88923e67befb89c0f1dabfa83e6
base_revision: c519ee916eaeb88923e67befb89c0f1dabfa83e6

# User provided section

# List of Local paths (relative to this file) that should be
# ignored by the migrate tool.
#
# Files that are not part of the templates will be ignored by default.
unmanaged_files:
- 'lib/main.dart'
- 'ios/Runner.xcodeproj/project.pbxproj'
10 changes: 1 addition & 9 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,6 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
js:
dependency: transitive
description:
name: js
sha256: c1b2e9b5ea78c45e1a0788d29606ba27dc5f71f019f32ca5140f61ef071838cf
url: "https://pub.dev"
source: hosted
version: "0.7.1"
leak_tracker:
dependency: transitive
description:
Expand Down Expand Up @@ -214,7 +206,7 @@ packages:
path: ".."
relative: true
source: path
version: "2.4.0"
version: "2.5.0"
shared_preferences:
dependency: "direct main"
description:
Expand Down
Binary file modified example/web/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified example/web/icons/Icon-192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified example/web/icons/Icon-512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified example/web/icons/Icon-maskable-192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified example/web/icons/Icon-maskable-512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
83 changes: 12 additions & 71 deletions example/web/index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<!DOCTYPE html>
<html>

<head>
<script charset="utf-8" src="https://js.pusher.com/7.4/pusher.min.js"></script>
<script charset="utf-8" src="https://js.pusher.com/8.4.0/pusher.min.js"></script>

<!--
If you are serving your web app in a path other than the root, change the
href value below to reflect the base path you are serving from.
Expand All @@ -19,84 +21,23 @@

<meta charset="UTF-8">
<meta content="IE=Edge" http-equiv="X-UA-Compatible">
<meta name="description" content="Demonstrates how to use the pusher_channels plugin.">
<meta name="description" content="A new Flutter project.">

<!-- iOS meta tags & icons -->
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-title" content="pusher_channels_flutter_example">
<link rel="apple-touch-icon" href="icons/Icon-192.png">

<!-- Favicon -->
<link rel="icon" type="image/png" href="favicon.png" />

<title>pusher_channels_flutter_example</title>
<link rel="manifest" href="manifest.json">
</head>
<body>
<!-- This script installs service_worker.js to provide PWA functionality to
application. For more information, see:
https://developers.google.com/web/fundamentals/primers/service-workers -->
<script>
var serviceWorkerVersion = null;
var scriptLoaded = false;
function loadMainDartJs() {
if (scriptLoaded) {
return;
}
scriptLoaded = true;
var scriptTag = document.createElement('script');
scriptTag.src = 'main.dart.js';
scriptTag.type = 'application/javascript';
document.body.append(scriptTag);
}

if ('serviceWorker' in navigator) {
// Service workers are supported. Use them.
window.addEventListener('load', function () {
// Wait for registration to finish before dropping the <script> tag.
// Otherwise, the browser will load the script multiple times,
// potentially different versions.
var serviceWorkerUrl = 'flutter_service_worker.js?v=' + serviceWorkerVersion;
navigator.serviceWorker.register(serviceWorkerUrl)
.then((reg) => {
function waitForActivation(serviceWorker) {
serviceWorker.addEventListener('statechange', () => {
if (serviceWorker.state == 'activated') {
console.log('Installed new service worker.');
loadMainDartJs();
}
});
}
if (!reg.active && (reg.installing || reg.waiting)) {
// No active web worker and we have installed or are installing
// one for the first time. Simply wait for it to activate.
waitForActivation(reg.installing || reg.waiting);
} else if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) {
// When the app updates the serviceWorkerVersion changes, so we
// need to ask the service worker to update.
console.log('New service worker available.');
reg.update();
waitForActivation(reg.installing);
} else {
// Existing service worker is still good.
console.log('Loading app from service worker.');
loadMainDartJs();
}
});

// If service worker doesn't succeed in a reasonable amount of time,
// fallback to plaint <script> tag.
setTimeout(() => {
if (!scriptLoaded) {
console.warn(
'Failed to load app from service worker. Falling back to plain <script> tag.',
);
loadMainDartJs();
}
}, 4000);
});
} else {
// Service workers not supported. Just drop the <script> tag.
loadMainDartJs();
}
</script>
<body>
<script src="flutter_bootstrap.js" async></script>
</body>
</html>

</html>
2 changes: 1 addition & 1 deletion example/web/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"display": "standalone",
"background_color": "#0175C2",
"theme_color": "#0175C2",
"description": "Demonstrates how to use the pusher_channels_flutter plugin.",
"description": "A new Flutter project.",
"orientation": "portrait-primary",
"prefer_related_applications": false,
"icons": [
Expand Down
14 changes: 0 additions & 14 deletions lib/pusher-js/core/auth/auth_transports.dart

This file was deleted.

66 changes: 66 additions & 0 deletions lib/pusher-js/core/auth/deprecated_channel_authorizer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import 'dart:js_interop';

import '../channels/channel.dart';
import 'options.dart';

@JSExport()
abstract class DartDeprecatedChannelAuthorizer {
@JSExport()
void authorize(
String socketId,
ChannelAuthorizationCallback callback,
) {}
}

extension type DeprecatedChannelAuthorizer._(JSObject _) implements JSObject {
static DeprecatedChannelAuthorizer create(
DartDeprecatedChannelAuthorizer authorizer,
) =>
createJSInteropWrapper<DartDeprecatedChannelAuthorizer>(authorizer)
as DeprecatedChannelAuthorizer;

external void authorize(
String socketId,
ChannelAuthorizationCallback callback,
);
}

typedef DartChannelAuthorizerGenerator = DeprecatedChannelAuthorizer Function(
Channel channel,
DeprecatedAuthorizerOptions options,
);

extension type ChannelAuthorizerGenerator._(JSFunction _)
implements JSFunction {
static ChannelAuthorizerGenerator create(
DartChannelAuthorizerGenerator function) =>
function.toJS as ChannelAuthorizerGenerator;
}

extension type DeprecatedAuthOptions._(JSObject _) implements JSObject {
external DeprecatedAuthOptions({JSAny? params, JSAny? headers});

external JSAny? params;

external JSAny? headers;
}

extension type DeprecatedAuthorizerOptions._(JSObject _) implements JSObject {
/// Allow 'ajax' | 'jsonp'
external String authTransport;

external String authEndpoint;

external DeprecatedAuthOptions? auth;
}

extension DeprecatedAuthorizerOptionsToDart on DeprecatedAuthorizerOptions {
Map<String, dynamic> toMap() => {
'authTransport': authTransport,
'authEndpoint': authEndpoint,
'auth': {
'params': auth?.params?.toExternalReference.toDartObject,
'headers': auth?.headers?.toExternalReference.toDartObject
}
};
}
Loading
Loading