Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
44 lines (37 sloc)
1.3 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library seaside.continuation; | |
import 'package:shelf/shelf.dart'; | |
import 'component.dart'; | |
import 'has_state.dart'; | |
import 'keys.dart'; | |
/// Type of an action callback function. | |
typedef Action = void Function(); | |
/// Part of the flow of pages within a session. | |
class Continuation { | |
final String sessionKey; | |
final String continuationKey; | |
final Map<HasState, dynamic> snapshots = Map.identity(); | |
final Map<String, Action> callbacks = {}; | |
Continuation(this.sessionKey, this.continuationKey, Component component) { | |
component.withAllChildren | |
.expand((child) => child.states) | |
.forEach((state) => snapshots[state] = state.snapshot()); | |
} | |
/// Restores the state and executes the callbacks of the request. | |
void call(Request request) { | |
final queryParameters = request.requestedUri.queryParameters; | |
snapshots.forEach((key, value) => key.restore(value)); | |
callbacks.keys | |
.where(queryParameters.containsKey) | |
.forEach((key) => callbacks[key]()); | |
} | |
/// Registers a [callback] and returns the corresponding URL. | |
Uri action(Action callback) { | |
final actionKey = callbacks.length.toString(); | |
callbacks[actionKey] = callback; | |
return Uri(queryParameters: { | |
sessionParam: sessionKey, | |
continuationParam: continuationKey, | |
actionKey: '', | |
}); | |
} | |
} |