Skip to content

Instantly share code, notes, and snippets.

View xsahil03x's full-sized avatar
🤹‍♂️
Working on something awesome

Sahil Kumar xsahil03x

🤹‍♂️
Working on something awesome
View GitHub Profile
@xsahil03x
xsahil03x / event_emitter.dart
Last active March 15, 2021 12:14
Dart Event Emitter
import 'dart:async';
import 'dart:collection';
import 'package:meta/meta.dart';
/// A listener that can be added to a [EventEmitter] using
/// [EventEmitter.on] or [EventEmitter.addListener].
///
/// This callback gets invoked once we call [EventEmitter.emit].
typedef Listener<T> = void Function(T data);
@xsahil03x
xsahil03x / rate_limit.dart
Created March 5, 2021 18:24
RxDart based implementation of rate limit
import 'dart:async';
import 'package:rxdart/rxdart.dart';
class StreamDebounce {
final Function func;
final Duration interval;
final subject = PublishSubject<List<dynamic>>();
StreamSubscription subscriber;
@xsahil03x
xsahil03x / rate_limit.dart
Created March 5, 2021 16:20
Rate Limit similar to `stream_transformer`
import 'dart:async';
/// Signature for for the static method [Function.apply].
typedef ApplicableFunction = void Function(
List<dynamic> positionalArguments, {
Map<Symbol, dynamic> namedArguments,
});
/// Useful rate limiter extensions for [Function] class.
extension RateLimit on Function {
@xsahil03x
xsahil03x / rate_limit.dart
Created March 5, 2021 09:26
Rate Limiter
import 'dart:async';
import 'dart:math' as math;
/// Creates a debounced function that delays invoking `func` until after `wait`
/// milliseconds have elapsed since the last time the debounced function was
/// invoked, or until the next browser frame is drawn. The debounced function
/// comes with a `cancel` method to cancel delayed `func` invocations and a
/// `flush` method to immediately invoke them. Provide `options` to indicate
/// whether `func` should be invoked on the leading and/or trailing edge of the
/// `wait` timeout. The `func` is invoked with the last arguments provided to the
@xsahil03x
xsahil03x / batch_client_test.dart
Created March 4, 2021 21:19
Batch Client Test
import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:mockito/mockito.dart';
import 'package:stream_feed_dart/src/core/api/batch_api_impl.dart';
import 'package:stream_feed_dart/src/core/http/http_client.dart';
import 'package:stream_feed_dart/src/core/http/token.dart';
import 'package:stream_feed_dart/src/core/models/activity.dart';
import 'package:stream_feed_dart/src/core/models/feed_id.dart';
import 'package:stream_feed_dart/src/core/util/routes.dart';
@xsahil03x
xsahil03x / string_x.dart
Created June 1, 2020 14:57
String extension to parse ISO8601 duration String to duration object.
extension StringX on String {
/// Creates a `Duration` object from an ISO8601 formatted string.
///
/// This method parses out a subset of the ISO8601 duration format. As years
/// and months are not of a set length this method does not support that. We
/// are then left with support for weeks, days, hours, minutes and seconds.
Duration get toDuration {
if (!RegExp(
r"^(-|\+)?P(?:([-+]?[0-9,.]*)Y)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)W)?(?:([-+]?[0-9,.]*)D)?(?:T(?:([-+]?[0-9,.]*)H)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)S)?)?$")
.hasMatch(this)) {
import 'dart:collection';
import 'dart:convert';
void main() {
final data = json.decode(
"[{\"videoId\":1,\"videoName\":\"Test Video 1\",\"duration\":300,\"createdTime\":\"1969-07-20 20:18:04Z\",\"isWatch\":1},{\"videoId\":1,\"videoName\":\"Test Video 1\",\"duration\":300,\"createdTime\":\"1969-07-20 20:18:04Z\",\"isWatch\":50},{\"videoId\":1,\"videoName\":\"Test Video 1\",\"duration\":300,\"createdTime\":\"1969-07-21 20:18:04Z\",\"isWatch\":20}]");
final map = HashMap<String, int>();
for (var value in (data as List)) {
if (map[value['createdTime']] == null) {
@xsahil03x
xsahil03x / api_exceptions.dart
Created October 23, 2019 18:36
Error Handling
@immutable
class ApiException implements Exception {
final String _message;
final String _prefix;
String get message => _message;
const ApiException([this._message, this._prefix]);
String toString() => "$_prefix$_message";
@xsahil03x
xsahil03x / main.dart
Created August 21, 2019 19:40 — forked from CodyDunlap/app_module.dart
Injecting services into Widget's using package:inject
void main() async {
var container = await AppComponent.create(ServicesModule(), ViewModelModule());
runApp(container.app);
}
@provide
class MyApp extends StatelessWidget {
final Router _router;
MyApp(this._router);
class MovieModel extends Model {
MovieRepository _movieRepository;
ApiResponse<List<Movie>> _movieList;
ApiResponse<List<Movie>> get movieList => _movieList;
MovieModel() {
_movieList = ApiResponse();
_movieRepository = MovieRepository();