Skip to content

Commit 7ca7bb9

Browse files
authored
Add files via upload
1 parent 17a5d74 commit 7ca7bb9

File tree

5 files changed

+261
-0
lines changed

5 files changed

+261
-0
lines changed

cache.ts

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import { DocumentNode } from 'graphql';
2+
import { getFragmentQueryDocument } from 'apollo-utilities';
3+
4+
import { DataProxy, Cache } from './types';
5+
6+
export type Transaction<T> = (c: ApolloCache<T>) => void;
7+
8+
export abstract class ApolloCache<TSerialized> implements DataProxy {
9+
// required to implement
10+
// core API
11+
public abstract read<T>(query: Cache.ReadOptions): T;
12+
public abstract write(write: Cache.WriteOptions): void;
13+
public abstract diff<T>(query: Cache.DiffOptions): Cache.DiffResult<T>;
14+
public abstract watch(watch: Cache.WatchOptions): () => void;
15+
public abstract evict(query: Cache.EvictOptions): Cache.EvictionResult;
16+
public abstract reset(): Promise<void>;
17+
18+
// intializer / offline / ssr API
19+
/**
20+
* Replaces existing state in the cache (if any) with the values expressed by
21+
* `serializedState`.
22+
*
23+
* Called when hydrating a cache (server side rendering, or offline storage),
24+
* and also (potentially) during hot reloads.
25+
*/
26+
public abstract restore(
27+
serializedState: TSerialized,
28+
): ApolloCache<TSerialized>;
29+
30+
/**
31+
* Exposes the cache's complete state, in a serializable format for later restoration.
32+
*/
33+
public abstract extract(optimistic: boolean): TSerialized;
34+
35+
// optimistic API
36+
public abstract removeOptimistic(id: string): void;
37+
38+
// transactional API
39+
public abstract performTransaction(
40+
transaction: Transaction<TSerialized>,
41+
): void;
42+
public abstract recordOptimisticTransaction(
43+
transaction: Transaction<TSerialized>,
44+
id: string,
45+
): void;
46+
47+
// optional API
48+
public transformDocument(document: DocumentNode): DocumentNode {
49+
return document;
50+
}
51+
// experimental
52+
public transformForLink(document: DocumentNode): DocumentNode {
53+
return document;
54+
}
55+
56+
// DataProxy API
57+
/**
58+
*
59+
* @param options
60+
* @param optimistic
61+
*/
62+
public readQuery<QueryType>(
63+
options: DataProxy.Query,
64+
optimistic: boolean = false,
65+
): QueryType {
66+
return this.read({
67+
query: options.query,
68+
variables: options.variables,
69+
optimistic,
70+
});
71+
}
72+
73+
public readFragment<FragmentType>(
74+
options: DataProxy.Fragment,
75+
optimistic: boolean = false,
76+
): FragmentType | null {
77+
return this.read({
78+
query: getFragmentQueryDocument(options.fragment, options.fragmentName),
79+
variables: options.variables,
80+
rootId: options.id,
81+
optimistic,
82+
});
83+
}
84+
85+
public writeQuery(options: Cache.WriteQueryOptions): void {
86+
this.write({
87+
dataId: 'ROOT_QUERY',
88+
result: options.data,
89+
query: options.query,
90+
variables: options.variables,
91+
});
92+
}
93+
94+
public writeFragment(options: Cache.WriteFragmentOptions): void {
95+
this.write({
96+
dataId: options.id,
97+
result: options.data,
98+
variables: options.variables,
99+
query: getFragmentQueryDocument(options.fragment, options.fragmentName),
100+
});
101+
}
102+
}

classes.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Animal {
2+
constructor(public name) { }
3+
move(meters) {
4+
alert(this.name + " moved " + meters + "m.");
5+
}
6+
}
7+
8+
class Snake extends Animal {
9+
constructor(name) { super(name); }
10+
move() {
11+
alert("Slithering...");
12+
super.move(5);
13+
}
14+
}
15+
16+
class Horse extends Animal {
17+
constructor(name) { super(name); }
18+
move() {
19+
alert("Galloping...");
20+
super.move(45);
21+
}
22+
}
23+
24+
var sam = new Snake("Sammy the Python")
25+
var tom: Animal = new Horse("Tommy the Palomino")
26+
27+
sam.move()
28+
tom.move(34)

conditionParser.mts

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
const thisFile = import.meta.url;
2+
3+
const waitOneTick = await Promise.resolve();
4+
5+
export function parse(
6+
source: string
7+
): {
8+
test: string;
9+
consequent: string | null;
10+
alternate: string | null;
11+
hash: string | null;
12+
} {
13+
const PROTOCOL = "condition:";
14+
15+
if (!source.startsWith(PROTOCOL, 0)) {
16+
throw new Error(`Expected 'condition:' at index 0 (${source})`);
17+
}
18+
19+
let pos = PROTOCOL.length;
20+
21+
skipWs();
22+
23+
const test = eatRegExp(/[\w-]+/y);
24+
if (!test) {
25+
throw new Error(`Expected an identifier at index ${pos} (${source})`);
26+
}
27+
28+
skipWs();
29+
expect("?");
30+
skipWs();
31+
32+
let consequent = null;
33+
if (source[pos] === "(") {
34+
consequent = eatParenthesized().trim() || null;
35+
skipWs();
36+
} else if (source[pos] !== ":") {
37+
consequent = eatUntil(":").trimRight() || null;
38+
}
39+
40+
expect(":");
41+
skipWs();
42+
43+
let alternate = null;
44+
if (pos < source.length) {
45+
if (source[pos] === "(") {
46+
alternate = eatParenthesized().trim() || null;
47+
skipWs();
48+
} else if (source[pos] !== ":") {
49+
alternate = eatUntil("#").trimRight() || null;
50+
}
51+
}
52+
53+
let hash = null;
54+
if (pos < source.length && source[pos] === "#") {
55+
pos++;
56+
hash = eatRegExp(/\w+/y);
57+
skipWs();
58+
}
59+
60+
if (pos !== source.length) {
61+
throw new Error(`Unexpected '${source[pos]}' at index ${pos} (${source})`);
62+
}
63+
64+
return { test, consequent, alternate, hash };
65+
66+
function expect(ch: string) {
67+
if (source[pos] !== ch) {
68+
throw new Error(`Expected '${ch}' at index ${pos} (${source})`);
69+
}
70+
pos++;
71+
}
72+
73+
function skipWs() {
74+
eatRegExp(/\s*/y);
75+
}
76+
77+
function eatRegExp(re: RegExp) {
78+
re.lastIndex = pos;
79+
const match = re.exec(source);
80+
if (!match) return null;
81+
82+
pos += match[0].length;
83+
return match[0];
84+
}
85+
86+
function eatUntil(end: string) {
87+
const start = pos;
88+
pos = source.indexOf(end, start);
89+
if (pos === -1) pos = source.length;
90+
return source.slice(start, pos);
91+
}
92+
93+
function eatParenthesized() {
94+
expect("(");
95+
96+
let depth = 1;
97+
let contents = "";
98+
99+
while (depth) {
100+
if (pos === source.length) {
101+
throw new Error(`Expected ')' at index ${pos} (${source})`);
102+
}
103+
104+
const ch = source[pos];
105+
106+
if (ch === "(") depth++;
107+
if (ch === ")") depth--;
108+
109+
if (ch !== ")" || depth > 0) {
110+
contents += ch;
111+
}
112+
113+
pos++;
114+
}
115+
116+
return contents;
117+
}
118+
}

hello.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Hello, World!");

promisified_cp.cts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import cp = require("child_process");
2+
3+
export = function run(command: string): Promise<{ stdout: string; stderr: string }> {
4+
const s = (b) => String(b).trim();
5+
6+
return new Promise((resolve, reject) => {
7+
cp.exec(command, (error, stdout, stderr) => {
8+
if (error) return reject(error);
9+
resolve({ stdout: s(stdout), stderr: s(stderr) });
10+
});
11+
});
12+
}

0 commit comments

Comments
 (0)