Skip to content

Instantly share code, notes, and snippets.

View jtlapp's full-sized avatar

Joe Lapp jtlapp

View GitHub Profile
@jtlapp
jtlapp / counter_app_compact.dart
Last active October 19, 2021 17:39
Counter app with provider - compact version
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<Counter>( // <=== PROVIDER
builder: (context) => Counter(),
@jtlapp
jtlapp / counter_app_complex.dart
Last active September 30, 2019 03:28
Counter app with provider - complex version
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<Counter>( // <=== PROVIDER
builder: (context) => Counter(),
@jtlapp
jtlapp / MainApi1-no-type-checking.ts
Last active March 23, 2022 02:09
Example main API without type checking
export class MainApi1 {
methodA(arg1: string, arg2: number): Promise<void> {
// code for methodA
}
methodB(): Promise<number> {
// code for methodB
}
}
@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 / MainApi1-type-checking.ts
Created March 23, 2022 02:53
Demo MainApi1 with type checking
export class MainApi1 implements ElectronMainApi<MainApi1> {
methodA(arg1: string, arg2: number): Promise<void> {
// code for methodA
}
methodB(): Promise<number> {
// code for methodB
}
}
@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 / MainApiBinding.ts
Last active March 25, 2022 02:54
The MainApiBinding type
export type MainApiBinding<T> = {
[K in PublicProperty<keyof T>]: T[K];
};
@jtlapp
jtlapp / example-MainApiBinding.ts
Last active March 25, 2022 03:32
Example of binding to main APIs
import type { MainApi1 } from '../backend/apis/main_api_1.ts';
import type { MainApi2 } from '../backend/apis/main_api_2.ts';
async function bindMainApis() {
return {
mainApi1: await bindMainApi<MainApi1>("MainApi1"),
mainApi2: await bindMainApi<MainApi2>("MainApi2"),
/* ... */
};
}
@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 / 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();
/* ... */
}