-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
scoped_value_listener.dart
313 lines (291 loc) · 9.78 KB
/
scoped_value_listener.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
part of katana_scoped;
/// [ScopedValueListener] that targets the app.
///
/// アプリを対象にしている[ScopedValueListener]。
class AppScopedValueListener extends ScopedValueListener {
AppScopedValueListener._({
required BuildContext context,
required VoidCallback callback,
required AppRef appRef,
required super.scope,
}) : super._(
context: context,
callback: callback,
appRef: appRef,
);
@override
ScopedValueContainer get container {
if (_containerCache != null) {
return _containerCache!;
}
return _containerCache = _appRef._scopedValueContainer;
}
@override
@protected
String? get _managedBy {
return _appRef.runtimeType.toString();
}
}
/// [ScopedValueListener] that targets the page.
///
/// ページを対象にしている[ScopedValueListener]。
class PageScopedValueListener extends _ScopedValueListenerOnPage {
PageScopedValueListener._({
required BuildContext context,
required VoidCallback callback,
required AppRef appRef,
required super.scope,
}) : super._(
context: context,
callback: callback,
appRef: appRef,
);
@override
ScopedValueContainer get container {
if (_containerCache != null) {
return _containerCache!;
}
return _containerCache = _PageScopedScope.of(_context)._container;
}
@override
@protected
String? get _managedBy {
return _PageScopedScope.of(_context).widget.toString();
}
}
class _ScopedValueListenerOnPage extends ScopedValueListener {
_ScopedValueListenerOnPage._({
required BuildContext context,
required VoidCallback callback,
required AppRef appRef,
ScopedValueContainer? container,
ScopedLoggerScope scope = ScopedLoggerScope.page,
}) : super._(
context: context,
callback: callback,
appRef: appRef,
container: container,
scope: scope,
);
@override
TResult? getAlreadtExistsScopedValueResult<TResult,
TScopedValue extends ScopedValue<TResult>>({
bool listen = false,
Object? name,
bool recursive = true,
}) {
final res = super.getAlreadtExistsScopedValueResult<TResult, TScopedValue>(
listen: listen,
name: name,
);
if (res != null || !recursive) {
return res;
}
return _PageScopedScope.maybeOf(_context)
?._pageListener
.getAlreadtExistsScopedValueResult<TResult, TScopedValue>(
listen: listen,
name: name,
);
}
}
class _ScopedValueListenerOnWidget extends ScopedValueListener {
_ScopedValueListenerOnWidget._({
required BuildContext context,
required VoidCallback callback,
required AppRef appRef,
ScopedValueContainer? container,
ScopedLoggerScope scope = ScopedLoggerScope.widget,
}) : super._(
context: context,
callback: callback,
container: container,
appRef: appRef,
scope: scope,
);
@override
TResult? getAlreadtExistsScopedValueResult<TResult,
TScopedValue extends ScopedValue<TResult>>({
bool listen = false,
Object? name,
bool recursive = true,
}) {
final res = super.getAlreadtExistsScopedValueResult<TResult, TScopedValue>(
listen: listen,
name: name,
);
if (res != null || !recursive) {
return res;
}
return _ScopedScope.maybeOf(_context)
?._widgetListener
.getAlreadtExistsScopedValueResult<TResult, TScopedValue>(
listen: listen,
name: name,
);
}
}
/// An object to monitor [ScopedValue] in the widget.
///
/// [container] to obtain a [ScopedValueContainer] and pass the change notification sent from the [ScopedValue] stored in the container to the associated widget.
///
/// [getScopedValueResult] associates [ScopedValue] and reads the result.
///
/// [ScopedValue]をウィジェット中で監視するためのオブジェクト。
///
/// [container]で[ScopedValueContainer]を取得しそこに保存されている[ScopedValue]から送られた変更通知を関連付けれられたウィジェットに渡します。
///
/// [getScopedValueResult]で[ScopedValue]の関連付けと結果の読み取りを行ないます。
abstract class ScopedValueListener {
ScopedValueListener._({
required BuildContext context,
required VoidCallback callback,
required AppRef appRef,
ScopedValueContainer? container,
ScopedLoggerScope scope = ScopedLoggerScope.widget,
}) : _scope = scope,
_appRef = appRef,
_context = context,
_callback = callback,
_container = container;
final BuildContext _context;
final VoidCallback _callback;
final ScopedValueContainer? _container;
ScopedValueContainer? _containerCache;
final Set<ScopedValueState> _watched = {};
late final AppRef _appRef;
// AppRef get _appRef {
// return _AppScopedScope.of(_context).widget.appRef;
// }
@protected
final ScopedLoggerScope _scope;
@protected
String? get _managedBy {
return _context.widget.toString();
}
String? __managedBy;
@protected
String get _listenedBy {
return _context.widget.toString();
}
String? __listendBy;
/// [ScopedValueContainer] that stores [ScopedValue].
///
/// [ScopedValue]を保存している[ScopedValueContainer]。
ScopedValueContainer get container {
if (_containerCache != null) {
return _containerCache!;
}
assert(_container != null, "[ScopedValueContainer] is not passed.");
return _containerCache = _container!;
}
/// [TScopedValue] by passing [provider] and returns the result.
///
/// If [listen] is `true`, then it should be associated with the widget to notify it of changes.
///
/// [name] so that they can be recognized as different objects even if they have the same type.
///
/// [provider]を渡すことにより[TScopedValue]を取得し、その結果を返します。
///
/// [listen]が`true`の場合、ウィジェットに関連付けて変更を通知するようにします。
///
/// [name]を指定すると型が同じ場合でも別のオブジェクトとして認識できるようにすることが可能です。
TResult
getScopedValueResult<TResult, TScopedValue extends ScopedValue<TResult>>(
TScopedValue Function() provider, {
void Function(ScopedValueState<TResult, TScopedValue> state)?
onInitOrUpdate,
bool listen = false,
Object? name,
}) {
__listendBy ??= _listenedBy;
__managedBy ??= _managedBy;
final state = container.getScopedValueState<TResult, TScopedValue>(
provider,
onInitOrUpdate: (state) {
if (state.disposed) {
return;
}
_watched.add(state);
state._addListener(this, listen ? _callback : null);
state._sendLog(ScopedLoggerEvent.listen, additionalParameter: {
ScopedLoggerEvent.listenedKey: __listendBy,
});
onInitOrUpdate?.call(state);
},
name: name,
scope: _scope,
managedBy: __managedBy,
loggerAdapters: _appRef.loggerAdapters,
);
return state.build();
}
/// Retrieves the [ScopedValueState] associated with the [TScopedValue] already stored in the [ScopedValueContainer] and returns the result.
///
/// Returns [Null] if [TScopedValue] does not exist.
///
/// If [TScopedValue] was saved with [name], specify the same [name].
///
/// If [listen] is `true`, then it should be associated with the widget to notify it of changes.
///
/// If [recursive] is `true`, the search is recursive from child to parent in the same scope.
///
/// [ScopedValueState.setState], [ScopedValueState.initValue] and [ScopedValueState.didUpdateValue] are not executed.
///
/// [ScopedValueContainer]にすでに保存されている[TScopedValue]に関連する[ScopedValueState]を取得し、その結果を返します。
///
/// [TScopedValue]が存在しない場合は[Null]を返します。
///
/// [name]を指定して[TScopedValue]を保存していた場合、同じ[name]を指定してください。
///
/// [listen]が`true`の場合、ウィジェットに関連付けて変更を通知するようにします。
///
/// [recursive]が`true`な場合、同じスコープの子から親へと再帰的に検索します。
///
/// [ScopedValueState.setState]や[ScopedValueState.initValue]、[ScopedValueState.didUpdateValue]は実行されません。
TResult? getAlreadtExistsScopedValueResult<TResult,
TScopedValue extends ScopedValue<TResult>>({
bool listen = false,
Object? name,
bool recursive = true,
}) {
final state =
container.getAlreadyExistsScopedValueState<TResult, TScopedValue>(
onInitOrUpdate: (state) {
if (state.disposed) {
return;
}
_watched.add(state);
state._addListener(this, listen ? _callback : null);
},
name: name,
);
return state?.build();
}
/// Executed when the widget is destroyed.
///
/// ScopedValueState.dispose] is executed on the monitored [ScopedValue] and the retained [ScopedValueContainer].
///
/// ウィジェットが破棄される際に実行します。
///
/// 監視している[ScopedValue]と保持している[ScopedValueContainer]を対象に[ScopedValueState.dispose]が実行されます。
void dispose() {
for (final watched in _watched) {
if (watched.disposed) {
continue;
}
watched._removeListener(this, _callback);
watched._sendLog(ScopedLoggerEvent.unlisten, additionalParameter: {
ScopedLoggerEvent.listenedKey: __listendBy,
});
watched.deactivate();
if (watched.autoDisposeWhenUnreferenced && watched._listeners.isEmpty) {
container.remove(watched);
}
}
_watched.clear();
if (_container != null) {
_container!.dispose();
}
}
}