Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(161)

Side by Side Diff: watcher/lib/src/resubscribable.dart

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « watcher/lib/src/path_set.dart ('k') | watcher/lib/src/stat.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 library watcher.resubscribable;
6
7 import 'dart:async';
8
9 import '../watcher.dart';
10 import 'watch_event.dart';
11
12 typedef ManuallyClosedWatcher WatcherFactory();
13
14 /// A wrapper for [ManuallyClosedWatcher] that encapsulates support for closing
15 /// the watcher when it has no subscribers and re-opening it when it's
16 /// re-subscribed.
17 ///
18 /// It's simpler to implement watchers without worrying about this behavior.
19 /// This class wraps a watcher class which can be written with the simplifying
20 /// assumption that it can continue emitting events until an explicit `close`
21 /// method is called, at which point it will cease emitting events entirely. The
22 /// [ManuallyClosedWatcher] interface is used for these watchers.
23 ///
24 /// This would be more cleanly implemented as a function that takes a class and
25 /// emits a new class, but Dart doesn't support that sort of thing. Instead it
26 /// takes a factory function that produces instances of the inner class.
27 abstract class ResubscribableWatcher implements Watcher {
28 /// The factory function that produces instances of the inner class.
29 final WatcherFactory _factory;
30
31 final String path;
32
33 Stream<WatchEvent> get events => _eventsController.stream;
34 StreamController<WatchEvent> _eventsController;
35
36 bool get isReady => _readyCompleter.isCompleted;
37
38 Future get ready => _readyCompleter.future;
39 var _readyCompleter = new Completer();
40
41 /// Creates a new [ResubscribableWatcher] wrapping the watchers
42 /// emitted by [_factory].
43 ResubscribableWatcher(this.path, this._factory) {
44 var watcher;
45 var subscription;
46
47 _eventsController = new StreamController<WatchEvent>.broadcast(
48 onListen: () {
49 watcher = _factory();
50 subscription = watcher.events.listen(_eventsController.add,
51 onError: _eventsController.addError,
52 onDone: _eventsController.close);
53
54 // It's important that we complete the value of [_readyCompleter] at the
55 // time [onListen] is called, as opposed to the value when [watcher.ready]
56 // fires. A new completer may be created by that time.
57 watcher.ready.then(_readyCompleter.complete);
58 }, onCancel: () {
59 // Cancel the subscription before closing the watcher so that the
60 // watcher's `onDone` event doesn't close [events].
61 subscription.cancel();
62 watcher.close();
63 _readyCompleter = new Completer();
64 }, sync: true);
65 }
66 }
67
68 /// An interface for watchers with an explicit, manual [close] method.
69 ///
70 /// See [ResubscribableWatcher].
71 abstract class ManuallyClosedWatcher implements Watcher {
72 /// Closes the watcher.
73 ///
74 /// Subclasses should close their [events] stream and release any internal
75 /// resources.
76 void close();
77 }
OLDNEW
« no previous file with comments | « watcher/lib/src/path_set.dart ('k') | watcher/lib/src/stat.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698