-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.ts
416 lines (310 loc) · 8.94 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import type { Config, Page, LiteralUnion } from 'types';
import { $ } from './app/session';
import { log } from './shared/logs';
import { defineGetter, forNode, hasProp, size } from './shared/utils';
import { configure } from './app/config';
import { getRoute } from './app/location';
import { LogType, VisitType } from './shared/enums';
import { assign, d, defineProps, isArray, isBrowser, o, origin } from './shared/native';
import { initialize, disconnect, observe } from './app/controller';
import { clear } from './app/queries';
import { on, off, emit } from './app/events';
import { morph } from './morph/morph';
import { Component } from './components/extends';
import { registerComponents, getComponentId } from './components/register';
import { takeSnapshot } from './shared/dom';
import * as q from './app/queries';
import * as hrefs from './observe/hrefs';
import * as request from './app/fetch';
import * as renderer from './app/render';
import * as history from './observe/history';
import * as components from './observe/components';
const spx = o({
get $ () { return $; },
Component,
on,
off,
observe,
connect,
component,
capture,
form,
render,
session,
reload,
fetch,
clear,
hydrate,
prefetch,
visit,
disconnect,
register,
get config () { return $.config; },
supported: !!(
isBrowser &&
window.history.pushState &&
window.requestAnimationFrame &&
window.DOMParser &&
window.Proxy
),
history: o({
get state () { return history.api.state; },
api: history.api,
push: history.push,
replace: history.replace,
has: history.has,
reverse: history.reverse
})
});
/**
* Connect SPX
*/
function connect (options: Config = {}) {
if (isBrowser === false) {
return log(LogType.ERROR, 'Invalid runtime environment: window is undefined.');
}
if (!spx.supported) {
return log(LogType.ERROR, 'Browser does not support SPX');
}
if (!window.location.protocol.startsWith('http')) {
return log(LogType.ERROR, 'Invalid protocol, SPX expects HTTPS or HTTP protocol');
}
configure(options);
if ($.config.globalThis && hasProp(window, 'spx') === false) {
defineGetter(window, 'spx', spx);
}
const promise = initialize();
return async function (callback: any) {
const state = (await promise);
if (callback.constructor.name === 'AsyncFunction') {
try {
await callback(state);
} catch (e) {
log(LogType.WARN, 'Connection Error', e);
}
} else {
callback(state);
}
log(LogType.INFO, 'Connection Established');
};
};
function component (identifer: string) {
const mounts = q.getMounted();
return mounts[identifer][0];
}
function register (...classes: any[]) {
if (typeof classes[0] === 'string') {
if (classes.length > 2) {
log(LogType.ERROR, [
`Named component registration expects 2 parameters, recieved ${classes.length}.`,
'Registry should follow this structure: spx.register("identifer", YourComponent)'
], classes);
}
registerComponents({ [components[0]]: classes[1] });
} else {
for (const component of classes) {
if (isArray(component)) {
for (const item of component) {
if (typeof item[0] === 'string') {
registerComponents({ [item[0]]: item[1] });
} else if (typeof item === 'function') {
registerComponents({ [getComponentId(item)]: item }, true);
}
}
} else {
const type = typeof component;
if (type === 'function') {
registerComponents({ [getComponentId(component)]: component }, true);
} else if (type === 'object') {
registerComponents(component);
}
}
}
}
if (!$.ready) {
on('x', function run () {
components.connect();
off('x', run);
emit('connected');
});
} else {
components.connect();
}
}
/**
* Session
*
* Returns the current SPX session
*/
function session () {
return defineProps(o(), {
config: { get: () => $.config },
snaps: { get: () => $.snaps },
pages: { get: () => $.pages },
observers: { get: () => $.observe },
components: { get: () => $.components },
fragments: { get: () => $.fragments },
memory: {
get () {
const memory = $.memory;
memory.size = size(memory.bytes);
return memory;
}
}
});
}
// function global (model?: { [key: string]: any }) {
// if (isEmpty($.global)) {
// if (model) {
// assign($.global, model);
// if ($.logLevel === LogLevel.INFO) {
// log(LogType.INFO, 'Global has been defined');
// } else if ($.logLevel === LogLevel.VERBOSE) {
// log(LogType.VERBOSE, 'Global has been defined: ', $.global);
// }
// }
// } else {
// if (model) {
// log(LogType.WARN, 'You cannot re-define global data within an SPX session');
// }
// }
// return $.global;
// }
/**
* Reload
*
* Reloads the current page
*/
async function reload () {
const state = $.pages[history.api.state.key];
state.type = VisitType.RELOAD;
const page = await request.fetch(state);
if (page) {
log(LogType.INFO, 'Triggered reload, page was re-cached');
return renderer.update(page);
}
log(LogType.WARN, 'Reload failed, triggering refresh (cache will purge)');
return location.assign(state.key);
};
/**
* Fetch
*/
async function fetch (url: string) {
const link = getRoute(url, VisitType.FETCH);
if (link.location.origin !== origin) {
log(LogType.ERROR, 'Cross origin fetches are not allowed');
}
const dom = await request.request<Document>(link.key);
if (dom) return dom;
}
async function render (url: string, pushState: 'intersect' | 'replace' | 'push', fn: (
this: Page,
dom: Document,
) => Document) {
const page = $.page;
const route = getRoute(url);
if (route.location.origin !== origin) log(LogType.ERROR, 'Cross origin fetches are not allowed');
const dom = await request.request<Document>(route.key, { type: 'document' });
if (!dom) log(LogType.ERROR, `Fetch failed for: ${route.key}`, dom);
await fn.call(page, dom) as Document;
if (pushState === 'replace') {
page.title = dom.title;
const state = q.update(assign(page, route), takeSnapshot(dom));
history.replace(state);
return state;
} else {
return renderer.update(q.set(route, takeSnapshot(dom)));
}
}
function capture (targets?: string[]) {
const page = q.getPage();
if (!page) return;
const dom = q.getSnapDom();
targets = isArray(targets) ? targets : page.target;
if (targets.length === 1 && targets[0] === 'body') {
morph(dom.body, d());
q.update(page, takeSnapshot(dom));
return;
}
const selector = targets.join(',');
const current = d().querySelectorAll<HTMLElement>(selector);
forNode(dom.body.querySelectorAll<HTMLElement>(selector), (node, i) => {
morph(node, current[i]);
});
q.update(page, takeSnapshot(dom));
}
/**
* Prefetch
*/
async function prefetch (link: string): Promise<void|Page> {
const path = getRoute(link, VisitType.PREFETCH);
if (q.has(path.key)) {
log(LogType.WARN, `Cache already exists for ${path.key}, prefetch skipped`);
return;
}
const prefetch = await request.fetch(q.create(path));
if (prefetch) return prefetch;
log(LogType.ERROR, `Prefetch failed for ${path.key}`);
};
async function form (action: string, options: {
method: LiteralUnion<'POST' | 'PUT' | 'DELETE' | 'GET', string>,
data: { [key: string]: string };
hydrate?: string[]
}) {
const body = new FormData();
for (const key in options.data) {
body.append(key, options.data[key]);
}
const submit = await request.request(action, {
method: options.method,
body
});
return submit;
}
/**
* Hydrate the current document
*/
async function hydrate (link: string, nodes?: string[]): Promise<Document> {
const route = getRoute(link, VisitType.HYDRATE);
request.fetch(route);
if (isArray(nodes)) {
route.hydrate = [];
route.preserve = [];
for (const node of nodes) {
if (node.charCodeAt(0) === 33) {
route.preserve.push(node.slice(1));
} else {
route.hydrate.push(node);
}
}
} else {
route.hydrate = $.config.fragments;
}
const page = await request.wait(route);
if (page) {
const { key } = history.api.state;
history.replace(page);
renderer.update(page);
if (route.key !== key) {
if ($.index === key) $.index = route.key;
for (const p in $.pages) {
if ($.pages[p].rev === key) {
$.pages[p].rev = route.key;
}
}
q.clear(key);
}
}
return q.getSnapDom(page.key);
};
/**
* Visit
*/
async function visit (link: string, options?: Page): Promise<void|Page> {
const route = getRoute(link);
const merge = typeof options === 'object' ? assign(route, options) : route;
return q.has(route.key)
? hrefs.navigate(route.key, q.update(merge))
: hrefs.navigate(route.key, q.create(merge));
};
export default spx;