1 |
|
2 | "use strict";
|
3 | export type Primitive = string | number | boolean | null;
|
4 | export type JsonValue = Primitive | JsonMap | JsonValue[];
|
5 | export type JsonMap = { [key: string]: JsonValue | void };
|
6 |
|
7 | export type Key = Primitive;
|
8 | export type KeyRangeTo = { from?: number, to: number, +length?: empty };
|
9 | export type KeyRangeLength = { from?: number, length: number, +to?: empty };
|
10 | export type KeyRange = KeyRangeTo | KeyRangeLength;
|
11 | export type Path = Key[];
|
12 | export type KeySet = Key | KeyRange | Array<Key | KeyRange>;
|
13 | export type PathSet = KeySet[];
|
14 |
|
15 | export type JsonGraph = { [key: string]: JsonGraphNode | void, +$type?: empty };
|
16 | export type JsonGraphNode = JsonGraph | JsonGraphLeaf;
|
17 | export type JsonGraphLeaf =
|
18 | | JsonGraphAtom
|
19 | | JsonGraphError
|
20 | | JsonGraphRef
|
21 | | Primitive;
|
22 |
|
23 | export type JsonGraphAtomDefined<T: ?JsonValue> = JsonGraphMetadata & {
|
24 | +$type: "atom",
|
25 | +value: T
|
26 | };
|
27 |
|
28 | export type JsonGraphAtomUndefined = JsonGraphMetadata & {
|
29 | +$type: "atom",
|
30 | +value?: void
|
31 | };
|
32 |
|
33 | export type JsonGraphAtom = JsonGraphMetadata & {
|
34 | +$type: "atom",
|
35 | +value?: ?JsonValue
|
36 | };
|
37 |
|
38 | export type JsonGraphError = JsonGraphMetadata & {
|
39 | +$type: "error",
|
40 | +value: JsonValue
|
41 | };
|
42 | export type JsonGraphRef = JsonGraphMetadata & {
|
43 | +$type: "ref",
|
44 | +value: Path
|
45 | };
|
46 |
|
47 | export type JsonGraphMetadata = {
|
48 | +$expires?: number,
|
49 | +$size?: number,
|
50 | +$timestamp?: number
|
51 | };
|
52 |
|
53 | export type JsonGraphEnvelope = {
|
54 | jsonGraph: JsonGraph,
|
55 | paths?: PathSet[],
|
56 | invalidated?: PathSet[],
|
57 | context?: JsonGraph
|
58 | };
|
59 |
|
60 | export type PathValue = {
|
61 | path: PathSet,
|
62 | value: JsonGraphLeaf,
|
63 | invalidated?: empty,
|
64 | jsonGraph?: empty
|
65 | };
|
66 |
|
67 | export type PathInvalidation = {
|
68 | path: PathSet,
|
69 | value?: empty,
|
70 | invalidated: true,
|
71 | jsonGraph?: empty
|
72 | };
|
73 |
|
74 | export interface IDisposable {
|
75 | dispose(): void;
|
76 | +isDisposed: boolean;
|
77 | }
|
78 |
|
79 | export type PartialObserver<T> = {
|
80 | +onNext?: (value: T) => void,
|
81 | +onError?: (error: Error) => void,
|
82 | +onCompleted?: () => void
|
83 | };
|
84 |
|
85 | export interface IObservable<T> {
|
86 | subscribe(
|
87 | onNext: ?PartialObserver<T> | ((value: T) => void),
|
88 | onError: ?(error: Error) => void,
|
89 | onCompleted: ?() => void
|
90 | ): IDisposable;
|
91 | }
|
92 |
|
93 | export interface IDataSource {
|
94 | get(paths: PathSet[]): IObservable<JsonGraphEnvelope>;
|
95 | set(jsonGraphEnvelope: JsonGraphEnvelope): IObservable<JsonGraphEnvelope>;
|
96 | call(
|
97 | functionPath: Path,
|
98 | args?: JsonGraphNode[],
|
99 | refSuffixes?: PathSet[],
|
100 | thisPaths?: PathSet[]
|
101 | ): IObservable<JsonGraphEnvelope>;
|
102 | }
|
103 |
|
104 | const {
|
105 | ref,
|
106 | atom,
|
107 | undefinedAtom,
|
108 | error,
|
109 | pathValue,
|
110 | pathInvalidation
|
111 | } = require("./factories");
|
112 |
|
113 | const {
|
114 | mergeJsonGraph,
|
115 | mergeJsonGraphEnvelope,
|
116 | mergeJsonGraphNode
|
117 | } = require("./merge");
|
118 |
|
119 | module.exports = {
|
120 | ref,
|
121 | atom,
|
122 | undefinedAtom,
|
123 | undefined: undefinedAtom,
|
124 | error,
|
125 | pathValue,
|
126 | pathInvalidation,
|
127 | mergeJsonGraph,
|
128 | mergeJsonGraphEnvelope,
|
129 | mergeJsonGraphNode
|
130 | };
|