Merge branch 'master' into grpc-web-example-package-web

This commit is contained in:
Aran Donohue 2025-02-20 11:05:15 -08:00
commit 4546b39267
No known key found for this signature in database
GPG Key ID: EA98761C2C20A998
3 changed files with 23 additions and 17 deletions

View File

@ -14,9 +14,7 @@
// limitations under the License. // limitations under the License.
import 'dart:async'; import 'dart:async';
// ignore: deprecated_member_use (#756) import 'package:web/web.dart';
import 'dart:html';
import 'src/generated/echo.pbgrpc.dart'; import 'src/generated/echo.pbgrpc.dart';
class EchoApp { class EchoApp {
@ -57,13 +55,23 @@ class EchoApp {
} }
void _addMessage(String message, String cssClass) { void _addMessage(String message, String cssClass) {
final classes = cssClass.split(' '); document.querySelector('#first')!.after(HTMLDivElement()
querySelector('#first')!.after(DivElement() ..classList.add('row')
..classes.add('row') ..append(HTMLHeadingElement.h2()
..append(Element.tag('h2') ..append(HTMLSpanElement()
..append(SpanElement() ..classList.add('label')
..classes.add('label') ..classList.addAll(cssClass)
..classes.addAll(classes)
..text = message))); ..text = message)));
} }
} }
// The documentation of DOMTokenList.add implies it can handle multiple classes,
// but in Chrome at least it does not.
extension AddAll on DOMTokenList {
void addAll(String cssClass) {
final classes = cssClass.split(' ');
for (final c in classes) {
add(c);
}
}
}

View File

@ -9,6 +9,7 @@ dependencies:
grpc: grpc:
path: ../../ path: ../../
protobuf: ^3.0.0 protobuf: ^3.0.0
web: ^1.1.0
dev_dependencies: dev_dependencies:
build_runner: ^2.4.13 build_runner: ^2.4.13

View File

@ -12,23 +12,20 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// ignore: deprecated_member_use (#756)
import 'dart:html';
import 'package:grpc/grpc_web.dart'; import 'package:grpc/grpc_web.dart';
import 'package:grpc_web/app.dart'; import 'package:grpc_web/app.dart';
import 'package:grpc_web/src/generated/echo.pbgrpc.dart'; import 'package:grpc_web/src/generated/echo.pbgrpc.dart';
import 'package:web/web.dart';
void main() { void main() {
final channel = GrpcWebClientChannel.xhr(Uri.parse('http://localhost:8080')); final channel = GrpcWebClientChannel.xhr(Uri.parse('http://localhost:8080'));
final service = EchoServiceClient(channel); final service = EchoServiceClient(channel);
final app = EchoApp(service); final app = EchoApp(service);
final button = querySelector('#send') as ButtonElement; final button = document.querySelector('#send') as HTMLButtonElement;
button.onClick.listen((e) async { button.onClick.listen((e) async {
final msg = querySelector('#msg') as TextInputElement; final msg = document.querySelector('#msg') as HTMLInputElement;
final value = msg.value!.trim(); final value = msg.value.trim();
msg.value = ''; msg.value = '';
if (value.isEmpty) return; if (value.isEmpty) return;