Skip to content

Instantly share code, notes, and snippets.

View jtlapp's full-sized avatar

Joe Lapp jtlapp

View GitHub Profile
@jtlapp
jtlapp / default_flutter_app.dart
Created September 14, 2019 03:33
Default increment app that the Flutter plugin generates
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
@jtlapp
jtlapp / changenotifierprovider_counter.dart
Last active December 3, 2022 15:32
Counter app implemented with ChangeNotifierProvider
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
@jtlapp
jtlapp / exercism_rust_forth2.rs
Last active October 20, 2022 19:10
all working but alloc_attack
// thanks to assistance from Martin Kavik and discord/rust @kpreid
use std::collections::HashMap;
pub type Value = i32;
pub type Result = std::result::Result<(), Error>;
#[derive(Debug, PartialEq, Eq)]
pub enum Error {
DivisionByZero,
@jtlapp
jtlapp / exercism_rust_forth.rs
Created October 19, 2022 01:32
Exercism rust Forth exercise using closures
// Implements https://exercism.org/tracks/rust/exercises/forth. Errors on line 168.
use std::cell::RefCell;
use std::collections::HashMap;
pub type Value = i32;
pub type Result = std::result::Result<(), Error>;
#[derive(Debug, PartialEq, Eq)]
pub enum Error {
@jtlapp
jtlapp / futureprovider_example.dart
Created September 18, 2019 04:07
FutureProvider example
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
const appTitle = 'FutureProvider Demo';
@jtlapp
jtlapp / simplified-bindMainApi.ts
Last active April 10, 2022 12:53
Simplified version of bindMainApi()
export async function bindMainApi<T extends ElectronMainApi<T>>(
apiClassName: string
): Promise<MainApiBinding<T>> {
// Use IPC to get a list of the API's methods from the main process.
const methodNames = await requestApiMethods<T>(apiClassName);
// boundApi will hold the binding, after we've added each method.
const boundApi = {} as MainApiBinding<T>;
// For each method name reported to be in the API...
@jtlapp
jtlapp / simplified-exposeMainApi.ts
Last active April 9, 2022 20:35
Simplified implementation of exposeMainApi()
// This associates the name of each API with a list of the API's methods.
const exposedApis: Record<string, string[]> = {};
function exposeMainApi<T extends ElectronMainApi<T>>(mainApi: T): void {
const apiClassName = mainApi.constructor.name;
const methodNames: string[] = [];
// For each property of mainApi...
for (const methodName of getPropertyNames(mainApi)) {
@jtlapp
jtlapp / type-exposeMainApi.ts
Last active April 2, 2022 18:11
Type of the exposeMainApi() function
function exposeMainApi<T extends ElectronMainApi<T>>(mainApi: T): void {
/* ... */
}
@jtlapp
jtlapp / type-ElectronMainApi.ts
Last active April 2, 2022 17:40
Type definition of ElectronMainApi
export type PublicProperty<P> = P extends `_${string}`
? never
: (P extends `#${string}` ? never : P);
export type ElectronMainApi<T> = {
[K in PublicProperty<keyof T>]: (...args: any[]) => Promise<any>;
};
@jtlapp
jtlapp / binding-and-calling-MainApi1.ts
Created March 26, 2022 02:17
Focused example of binding and calling MainApi1
import type { MainApi1 } from '../backend/apis/main_api_1.ts';
async function doSomething() {
const mainApi1 = await bindMainApi<MainApi1>("MainApi1");
let result = await mainApi1.methodB();
/* ... */
}