FBLPromise+Validate.m 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. Copyright 2018 Google Inc. All rights reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at:
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #import "FBLPromise+Validate.h"
  14. #import "FBLPromisePrivate.h"
  15. @implementation FBLPromise (ValidateAdditions)
  16. - (FBLPromise*)validate:(FBLPromiseValidateWorkBlock)predicate {
  17. return [self onQueue:FBLPromise.defaultDispatchQueue validate:predicate];
  18. }
  19. - (FBLPromise*)onQueue:(dispatch_queue_t)queue validate:(FBLPromiseValidateWorkBlock)predicate {
  20. NSParameterAssert(queue);
  21. NSParameterAssert(predicate);
  22. FBLPromiseChainedFulfillBlock chainedFulfill = ^id(id value) {
  23. return predicate(value) ? value :
  24. [[NSError alloc] initWithDomain:FBLPromiseErrorDomain
  25. code:FBLPromiseErrorCodeValidationFailure
  26. userInfo:nil];
  27. };
  28. return [self chainOnQueue:queue chainedFulfill:chainedFulfill chainedReject:nil];
  29. }
  30. @end
  31. @implementation FBLPromise (DotSyntax_ValidateAdditions)
  32. - (FBLPromise* (^)(FBLPromiseValidateWorkBlock))validate {
  33. return ^(FBLPromiseValidateWorkBlock predicate) {
  34. return [self validate:predicate];
  35. };
  36. }
  37. - (FBLPromise* (^)(dispatch_queue_t, FBLPromiseValidateWorkBlock))validateOn {
  38. return ^(dispatch_queue_t queue, FBLPromiseValidateWorkBlock predicate) {
  39. return [self onQueue:queue validate:predicate];
  40. };
  41. }
  42. @end