Skip to content

Commit

Permalink
WIP: dfgsdghd
Browse files Browse the repository at this point in the history
  • Loading branch information
jsayol committed Jun 16, 2017
2 parents e71ec03 + 6404ff2 commit e8a4253
Show file tree
Hide file tree
Showing 8 changed files with 562 additions and 532 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ applications using Firebase services. This SDK is distributed via:
- [npm package](https://www.npmjs.com/package/firebase)
- [Bower package](https://github.com/firebase/firebase-bower)

To get starting using Firebase, see
To get started using Firebase, see
[Add Firebase to your JavaScript Project](https://firebase.google.com/docs/web/setup).

## SDK Dev Workflow
Expand Down
2 changes: 2 additions & 0 deletions externs/firebase-app-externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ firebase.Promise = function(resolver) {};
* @param {(function(!Error): *)=} onReject Called when the Promise is rejected
* (with an error).
* @return {!firebase.Promise<*>}
* @override
*/
firebase.Promise.prototype.then = function(onResolve, onReject) {};

Expand All @@ -241,6 +242,7 @@ firebase.Promise.prototype.then = function(onResolve, onReject) {};
*
* @param {(function(!Error): *)=} onReject Called when the Promise is rejected
* (with an error).
* @override
*/
firebase.Promise.prototype.catch = function(onReject) {};

Expand Down
100 changes: 54 additions & 46 deletions src/database/core/view/CacheNode.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,74 @@
import { Node } from '../snap/Node';
import { Path } from '../util/Path';

/**
* A cache node only stores complete children. Additionally it holds a flag whether the node can be considered fully
* initialized in the sense that we know at one point in time this represented a valid state of the world, e.g.
* initialized with data from the server, or a complete overwrite by the client. The filtered flag also tracks
* whether a node potentially had children removed due to a filter.
*
* @param {!fb.core.snap.Node} node
* @param {boolean} fullyInitialized
* @param {boolean} filtered
* @constructor
*/
export class CacheNode {
export const CacheNode = function(node, fullyInitialized, filtered) {
/**
* @param {!fb.core.snap.Node} node_
* @param {boolean} fullyInitialized_
* @param {boolean} filtered_
* @type {!fb.core.snap.Node}
* @private
*/
constructor(private node_: Node,
private fullyInitialized_: boolean,
private filtered_: boolean) {

}
this.node_ = node;

/**
* Returns whether this node was fully initialized with either server data or a complete overwrite by the client
* @return {boolean}
* @type {boolean}
* @private
*/
isFullyInitialized(): boolean {
return this.fullyInitialized_;
}
this.fullyInitialized_ = fullyInitialized;

/**
* Returns whether this node is potentially missing children due to a filter applied to the node
* @return {boolean}
* @type {boolean}
* @private
*/
isFiltered(): boolean {
return this.filtered_;
}
this.filtered_ = filtered;
};

/**
* @param {!fb.core.util.Path} path
* @return {boolean}
*/
isCompleteForPath(path: Path): boolean {
if (path.isEmpty()) {
return this.isFullyInitialized() && !this.filtered_;
}
/**
* Returns whether this node was fully initialized with either server data or a complete overwrite by the client
* @return {boolean}
*/
CacheNode.prototype.isFullyInitialized = function() {
return this.fullyInitialized_;
};

const childKey = path.getFront();
return this.isCompleteForChild(childKey);
}
/**
* Returns whether this node is potentially missing children due to a filter applied to the node
* @return {boolean}
*/
CacheNode.prototype.isFiltered = function() {
return this.filtered_;
};

/**
* @param {!string} key
* @return {boolean}
*/
isCompleteForChild(key: string): boolean {
return (this.isFullyInitialized() && !this.filtered_) || this.node_.hasChild(key);
/**
* @param {!fb.core.util.Path} path
* @return {boolean}
*/
CacheNode.prototype.isCompleteForPath = function(path) {
if (path.isEmpty()) {
return this.isFullyInitialized() && !this.filtered_;
} else {
var childKey = path.getFront();
return this.isCompleteForChild(childKey);
}
};

/**
* @return {!fb.core.snap.Node}
*/
getNode(): Node {
return this.node_;
}
/**
* @param {!string} key
* @return {boolean}
*/
CacheNode.prototype.isCompleteForChild = function(key) {
return (this.isFullyInitialized() && !this.filtered_) || this.node_.hasChild(key);
};

}
/**
* @return {!fb.core.snap.Node}
*/
CacheNode.prototype.getNode = function() {
return this.node_;
};
133 changes: 70 additions & 63 deletions src/database/core/view/CompleteChildSource.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { CacheNode } from './CacheNode';
import { NamedNode, Node } from '../snap/Node';
import { Index } from '../snap/indexes/Index';
import { WriteTreeRef } from '../WriteTree';
import { ViewCache } from './ViewCache';
import { CacheNode } from "./CacheNode";

/**
* Since updates to filtered nodes might require nodes to be pulled in from "outside" the node, this interface
Expand All @@ -12,21 +8,21 @@ import { ViewCache } from './ViewCache';
*
* @interface
*/
export interface CompleteChildSource {
/**
* @param {!string} childKey
* @return {?fb.core.snap.Node}
*/
getCompleteChild(childKey: string): Node | null;
export const CompleteChildSource = function() { };

/**
* @param {!fb.core.snap.Index} index
* @param {!fb.core.snap.NamedNode} child
* @param {boolean} reverse
* @return {?fb.core.snap.NamedNode}
*/
getChildAfterChild(index: Index, child: NamedNode, reverse: boolean): NamedNode | null;
}
/**
* @param {!string} childKey
* @return {?fb.core.snap.Node}
*/
CompleteChildSource.prototype.getCompleteChild = function(childKey) { };

/**
* @param {!fb.core.snap.Index} index
* @param {!fb.core.snap.NamedNode} child
* @param {boolean} reverse
* @return {?fb.core.snap.NamedNode}
*/
CompleteChildSource.prototype.getChildAfterChild = function(index, child, reverse) { };


/**
Expand All @@ -36,23 +32,22 @@ export interface CompleteChildSource {
* @constructor
* @implements CompleteChildSource
*/
export class NoCompleteChildSource_ implements CompleteChildSource {
const NoCompleteChildSource_ = function() {
};

/**
* @inheritDoc
*/
getCompleteChild() {
return null;
}

/**
* @inheritDoc
*/
getChildAfterChild() {
return null;
}
}
/**
* @inheritDoc
*/
NoCompleteChildSource_.prototype.getCompleteChild = function() {
return null;
};

/**
* @inheritDoc
*/
NoCompleteChildSource_.prototype.getChildAfterChild = function() {
return null;
};

/**
* Singleton instance.
Expand All @@ -66,45 +61,57 @@ export const NO_COMPLETE_CHILD_SOURCE = new NoCompleteChildSource_();
* An implementation of CompleteChildSource that uses a WriteTree in addition to any other server data or
* old event caches available to calculate complete children.
*
* @param {!fb.core.WriteTreeRef} writes
* @param {!fb.core.view.ViewCache} viewCache
* @param {?fb.core.snap.Node} optCompleteServerCache
*
* @constructor
* @implements CompleteChildSource
*/
export class WriteTreeCompleteChildSource implements CompleteChildSource {
export const WriteTreeCompleteChildSource = function(writes, viewCache, optCompleteServerCache) {
/**
* @param {!fb.core.WriteTreeRef} writes_
* @param {!fb.core.view.ViewCache} viewCache_
* @param {?fb.core.snap.Node} optCompleteServerCache_
* @type {!fb.core.WriteTreeRef}
* @private
*/
constructor(private writes_: WriteTreeRef,
private viewCache_: ViewCache,
private optCompleteServerCache_: Node | null = null) {
}
this.writes_ = writes;

/**
* @inheritDoc
* @type {!fb.core.view.ViewCache}
* @private
*/
getCompleteChild(childKey) {
const node = this.viewCache_.getEventCache();
if (node.isCompleteForChild(childKey)) {
return node.getNode().getImmediateChild(childKey);
} else {
const serverNode = this.optCompleteServerCache_ != null ?
new CacheNode(this.optCompleteServerCache_, true, false) : this.viewCache_.getServerCache();
return this.writes_.calcCompleteChild(childKey, serverNode);
}
}
this.viewCache_ = viewCache;

/**
* @inheritDoc
* @type {?fb.core.snap.Node}
* @private
*/
getChildAfterChild(index, child, reverse) {
const completeServerData = this.optCompleteServerCache_ != null ? this.optCompleteServerCache_ :
this.optCompleteServerCache_ = optCompleteServerCache;
};

/**
* @inheritDoc
*/
WriteTreeCompleteChildSource.prototype.getCompleteChild = function(childKey) {
var node = this.viewCache_.getEventCache();
if (node.isCompleteForChild(childKey)) {
return node.getNode().getImmediateChild(childKey);
} else {
var serverNode = this.optCompleteServerCache_ != null ?
new CacheNode(this.optCompleteServerCache_, true, false) : this.viewCache_.getServerCache();
return this.writes_.calcCompleteChild(childKey, serverNode);
}
};

/**
* @inheritDoc
*/
WriteTreeCompleteChildSource.prototype.getChildAfterChild = function(index, child, reverse) {
var completeServerData = this.optCompleteServerCache_ != null ? this.optCompleteServerCache_ :
this.viewCache_.getCompleteServerSnap();
const nodes = this.writes_.calcIndexedSlice(completeServerData, child, 1, reverse, index);
if (nodes.length === 0) {
return null;
} else {
return nodes[0];
}
var nodes = this.writes_.calcIndexedSlice(completeServerData, child, 1, reverse, index);
if (nodes.length === 0) {
return null;
} else {
return nodes[0];
}
}
};
Loading

0 comments on commit e8a4253

Please sign in to comment.