FAckUserWrite.m 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FirebaseDatabase/Sources/Core/Operation/FAckUserWrite.h"
  17. #import "FirebaseDatabase/Sources/Core/Operation/FOperationSource.h"
  18. #import "FirebaseDatabase/Sources/Core/Utilities/FImmutableTree.h"
  19. #import "FirebaseDatabase/Sources/Core/Utilities/FPath.h"
  20. @implementation FAckUserWrite
  21. - (id)initWithPath:(FPath *)operationPath
  22. affectedTree:(FImmutableTree *)tree
  23. revert:(BOOL)shouldRevert {
  24. self = [super init];
  25. if (self) {
  26. self->_source = [FOperationSource userInstance];
  27. self->_type = FOperationTypeAckUserWrite;
  28. self->_path = operationPath;
  29. self->_affectedTree = tree;
  30. self->_revert = shouldRevert;
  31. }
  32. return self;
  33. }
  34. - (FAckUserWrite *)operationForChild:(NSString *)childKey {
  35. if (![self.path isEmpty]) {
  36. NSAssert([self.path.getFront isEqualToString:childKey],
  37. @"operationForChild called for unrelated child.");
  38. return [[FAckUserWrite alloc] initWithPath:[self.path popFront]
  39. affectedTree:self.affectedTree
  40. revert:self.revert];
  41. } else if (self.affectedTree.value != nil) {
  42. NSAssert(self.affectedTree.children.isEmpty,
  43. @"affectedTree should not have overlapping affected paths.");
  44. // All child locations are affected as well; just return same operation.
  45. return self;
  46. } else {
  47. FImmutableTree *childTree =
  48. [self.affectedTree subtreeAtPath:[[FPath alloc] initWith:childKey]];
  49. return [[FAckUserWrite alloc] initWithPath:[FPath empty]
  50. affectedTree:childTree
  51. revert:self.revert];
  52. }
  53. }
  54. - (NSString *)description {
  55. return
  56. [NSString stringWithFormat:
  57. @"FAckUserWrite { path=%@, revert=%d, affectedTree=%@ }",
  58. self.path, self.revert, self.affectedTree];
  59. }
  60. @end