fiber_crucible 1.0.0
fiber_crucible: ^1.0.0 copied to clipboard
The vocabulary a service is written in. Outcome, failure, async state, action, pagination and lifetime, with no logic of its own.
// Copyright (C) 2026 Fiber
//
// This Source Code Form is subject to the terms of the Mozilla Public License,
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at https://mozilla.org/MPL/2.0/.
//
// What you may do:
// - Use this software for any purpose, including commercially, and build and
// sell your own products on top of it.
// - Change it, and create new works based on it.
// - Distribute copies of it, with or without your changes.
// - Combine it with files under any other licence, proprietary ones included,
// and licence that larger work on your own terms.
//
// What you must do in return:
// - Keep this notice on every file you received it on.
// - Publish, under these same terms, the source of every file covered by them
// that you distribute, including the ones you changed, so that whoever
// receives your version can obtain that source.
// - Leave Fiber out of it: the name "Fiber", its branding, its logos and its
// trademarks may not be used to endorse or promote what you build, and this
// licence grants no right to them.
//
// Disclaimer:
// AS FAR AS THE LAW ALLOWS, THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY
// OR CONDITION OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
// NON-INFRINGEMENT. IN NO EVENT SHALL FIBER BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING BUT NOT
// LIMITED TO LOSS OF USE, DATA, PROFITS, OR BUSINESS INTERRUPTION) ARISING OUT
// OF OR RELATED TO THESE TERMS OR THE USE OR NATURE OF THE SOFTWARE, UNDER ANY
// KIND OF LEGAL CLAIM.
//
// This header is a summary written for convenience. Where it differs from the
// LICENSE file, the LICENSE file governs.
import 'package:example/fake/backend.dart';
import 'package:example/fake/store_service.dart';
import 'package:example/pages/async_page.dart';
import 'package:example/pages/command_page.dart';
import 'package:example/pages/fault_page.dart';
import 'package:example/pages/observable_page.dart';
import 'package:example/pages/pager_page.dart';
import 'package:example/pages/result_page.dart';
import 'package:example/pages/scope_page.dart';
import 'package:flutter/material.dart';
void main() => runApp(const ExamplesApp());
/// One runnable example per piece of fiber_crucible.
class ExamplesApp extends StatefulWidget {
/// Opens the list of examples.
const ExamplesApp({super.key});
@override
State<ExamplesApp> createState() => _ExamplesAppState();
}
class _ExamplesAppState extends State<ExamplesApp> {
final StoreService _service = StoreService(Backend());
@override
void dispose() {
_service.close().ignore();
super.dispose();
}
@override
Widget build(BuildContext context) => MaterialApp(
title: 'fiber_crucible examples',
theme: ThemeData(colorSchemeSeed: Colors.indigo, useMaterial3: true),
home: HomePage(service: _service),
);
}
/// The list every example is reached from.
class HomePage extends StatelessWidget {
/// Hands [service] to the examples that need one.
const HomePage({required this.service, super.key});
/// The one service every example shares.
final StoreService service;
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('fiber_crucible examples')),
body: ListView(
children: <Widget>[
_Entry(
title: 'answer and Result',
subtitle: 'One call, one outcome, and a method that never throws',
page: () => ResultPage(service: service),
),
_Entry(
title: 'Fault and Invalid',
subtitle: 'A closed failure set, and the one door the domain has',
page: () => FaultPage(service: service),
),
const _Entry(
title: 'Observable and Mutable',
subtitle: 'A value that is always there, and who is allowed to write',
page: ObservablePage.new,
),
_Entry(
title: 'Async',
subtitle: 'Three states, and no way to spell a fourth',
page: () => AsyncPage(service: service),
),
_Entry(
title: 'Command',
subtitle: 'An action, its state, and the guard against a double tap',
page: () => CommandPage(service: service),
),
_Entry(
title: 'Pager',
subtitle: 'A paged list, and what it keeps when a page fails',
page: () => PagerPage(service: service),
),
const _Entry(
title: 'Scope',
subtitle: 'The subscriptions of one session, cancelled together',
page: ScopePage.new,
),
],
),
);
}
class _Entry extends StatelessWidget {
const _Entry({
required this.title,
required this.subtitle,
required this.page,
});
final String title;
final String subtitle;
final Widget Function() page;
@override
Widget build(BuildContext context) => ListTile(
title: Text(title),
subtitle: Text(subtitle),
trailing: const Icon(Icons.chevron_right),
onTap: () => Navigator.of(
context,
).push(MaterialPageRoute<void>(builder: (BuildContext context) => page())),
);
}