Skip to content

Commit 2ee2e49

Browse files
authored
Merge pull request #79 from dewanakl/improve-ui
fix: cache pool, refactor code, and add dir auto
2 parents e695c86 + 243a5f4 commit 2ee2e49

11 files changed

Lines changed: 95 additions & 129 deletions

File tree

assets/images/placeholder.webp

3.51 KB
Loading

js/app/admin/admin.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { storage } from '../../common/storage.js';
88
import { session } from '../../common/session.js';
99
import { offline } from '../../common/offline.js';
1010
import { comment } from '../components/comment.js';
11-
import { request, HTTP_GET, HTTP_PATCH, HTTP_PUT } from '../../connection/request.js';
11+
import { pool, request, HTTP_GET, HTTP_PATCH, HTTP_PUT } from '../../connection/request.js';
1212

1313
export const admin = (() => {
1414

@@ -310,7 +310,7 @@ export const admin = (() => {
310310
/**
311311
* @returns {void}
312312
*/
313-
const domLoaded = () => {
313+
const pageLoaded = () => {
314314
lang.init();
315315
lang.setDefault('en');
316316

@@ -346,7 +346,7 @@ export const admin = (() => {
346346
storage('information').clear();
347347
}
348348

349-
document.addEventListener('DOMContentLoaded', domLoaded);
349+
window.addEventListener('load', () => pool.init(pageLoaded, ['gif']));
350350

351351
return {
352352
util,

js/app/components/card.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ export const card = (() => {
178178
const data = util.convertMarkdownToHTML(util.escapeHtml(moreMaxLength ? (c.comment.slice(0, maxCommentLength) + '...') : c.comment));
179179

180180
return head + `
181-
<p class="text-theme-auto my-1 mx-0 p-0" style="white-space: pre-wrap !important; font-size: 0.95rem;" data-comment="${util.base64Encode(c.comment)}" id="content-${c.uuid}">${data}</p>
181+
<p dir="auto" class="text-theme-auto my-1 mx-0 p-0" style="white-space: pre-wrap !important; font-size: 0.95rem;" data-comment="${util.base64Encode(c.comment)}" id="content-${c.uuid}">${data}</p>
182182
${moreMaxLength ? `<p class="d-block mb-2 mt-0 mx-0 p-0"><a class="text-theme-auto" role="button" style="font-size: 0.85rem;" data-show="false" onclick="undangan.comment.showMore(this, '${c.uuid}')">Selengkapnya</a></p>` : ''}`;
183183
};
184184

@@ -203,19 +203,13 @@ export const card = (() => {
203203
* @param {ReturnType<typeof dto.getCommentResponse>[]} cs
204204
* @returns {Promise<string>}
205205
*/
206-
const renderContentMany = (cs) => {
207-
return gif.prepareCache()
208-
.then(() => Promise.all(cs.map((i) => renderContent(i))))
209-
.then((r) => r.join(''));
210-
};
206+
const renderContentMany = (cs) => Promise.all(cs.map((i) => renderContent(i))).then((r) => r.join(''));
211207

212208
/**
213209
* @param {ReturnType<typeof dto.getCommentResponse>} cs
214210
* @returns {Promise<string>}
215211
*/
216-
const renderContentSingle = (cs) => {
217-
return gif.prepareCache().then(() => renderContent(cs));
218-
};
212+
const renderContentSingle = (cs) => renderContent(cs);
219213

220214
/**
221215
* @param {string} id

js/app/components/gif.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,6 @@ export const gif = (() => {
7979
});
8080
};
8181

82-
/**
83-
* @returns {Promise<void>}
84-
*/
85-
const prepareCache = () => c.open();
86-
8782
/**
8883
* @param {string} url
8984
* @returns {Promise<string>}
@@ -562,6 +557,5 @@ export const gif = (() => {
562557
buttonCancel,
563558
removeGifSearch,
564559
removeButtonBack,
565-
prepareCache,
566560
};
567561
})();

js/app/guest/guest.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { session } from '../../common/session.js';
1212
import { offline } from '../../common/offline.js';
1313
import { comment } from '../components/comment.js';
1414
import * as confetti from '../../libs/confetti.js';
15+
import { pool } from '../../connection/request.js';
1516

1617
export const guest = (() => {
1718

@@ -315,7 +316,7 @@ export const guest = (() => {
315316
/**
316317
* @returns {void}
317318
*/
318-
const domLoaded = () => {
319+
const pageLoaded = () => {
319320
lang.init();
320321
offline.init();
321322
comment.init();
@@ -359,8 +360,7 @@ export const guest = (() => {
359360
img.load();
360361
}
361362

362-
// fetch after document is loaded.
363-
const load = () => session.guest(params.get('k') ?? token).then(({ data }) => {
363+
session.guest(params.get('k') ?? token).then(({ data }) => {
364364
document.dispatchEvent(new Event('undangan.session'));
365365
progress.complete('config');
366366

@@ -377,8 +377,6 @@ export const guest = (() => {
377377
.catch(() => progress.invalid('comment'));
378378

379379
}).catch(() => progress.invalid('config'));
380-
381-
window.addEventListener('load', load);
382380
}
383381
};
384382

@@ -397,7 +395,15 @@ export const guest = (() => {
397395
storage('comment').clear();
398396
}
399397

400-
document.addEventListener('DOMContentLoaded', domLoaded);
398+
window.addEventListener('load', () => {
399+
pool.init(pageLoaded, [
400+
'image',
401+
'video',
402+
'audio',
403+
'libs',
404+
'gif',
405+
]);
406+
});
401407

402408
return {
403409
util,

js/app/guest/image.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,9 @@ export const image = (() => {
9494
* @returns {Promise<void>}
9595
*/
9696
const runGroup = async (filter) => {
97+
urlCache.length = 0;
9798
imgs.filter(filter).forEach((el) => el.hasAttribute('data-src') ? getByFetch(el) : getByDefault(el));
98-
99-
if (urlCache.length) {
100-
await c.run(urlCache, progress.getAbort());
101-
urlCache.length = 0;
102-
}
99+
await c.run(urlCache, progress.getAbort());
103100
};
104101

105102
await runGroup((el) => el.hasAttribute('fetchpriority'));

js/app/guest/progress.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export const progress = (() => {
4949

5050
if (loaded === total) {
5151
valid = false;
52+
cancelProgress = null;
5253
document.dispatchEvent(new Event('undangan.progress.done'));
5354
}
5455
};

js/app/guest/video.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,6 @@ export const video = (() => {
113113
});
114114
};
115115

116-
if (!window.isSecureContext) {
117-
return fetchBasic();
118-
}
119-
120116
return c.has(src).then((res) => {
121117
if (!res) {
122118
return c.del(src).then(fetchBasic).then((r) => c.set(src, r));

js/connection/cache.js

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,5 @@
11
import { request, cacheWrapper, HTTP_GET } from './request.js';
22

3-
const objectPool = (() => {
4-
/**
5-
* @type {Map<string, Promise<Cache>>|null}
6-
*/
7-
let cachePool = null;
8-
9-
return {
10-
/**
11-
* @param {string} name
12-
* @returns {Promise<Cache>}
13-
*/
14-
getInstance: (name) => {
15-
if (!cachePool) {
16-
cachePool = new Map();
17-
}
18-
19-
if (!cachePool.has(name)) {
20-
cachePool.set(name, window.caches.open(name));
21-
}
22-
23-
return cachePool.get(name);
24-
},
25-
};
26-
})();
27-
283
export const cache = (cacheName) => {
294

305
/**
@@ -38,49 +13,38 @@ export const cache = (cacheName) => {
3813
const inFlightRequests = new Map();
3914

4015
/**
41-
* @type {Cache|null}
16+
* @type {ReturnType<typeof cacheWrapper>}
4217
*/
43-
let cacheObject = null;
18+
const cw = cacheWrapper(cacheName);
4419

4520
let ttl = 1000 * 60 * 60 * 6;
4621

4722
let forceCache = false;
4823

49-
/**
50-
* @returns {Promise<Cache>|null}
51-
*/
52-
const open = async () => {
53-
if (!cacheObject && window.isSecureContext) {
54-
cacheObject = await objectPool.getInstance(cacheName);
55-
}
56-
57-
return cacheObject;
58-
};
59-
6024
/**
6125
* @param {string|URL} input
6226
* @param {Response} res
6327
* @returns {Response}
6428
*/
65-
const set = (input, res) => open().then(cacheWrapper).then((cw) => {
29+
const set = (input, res) => {
6630
if (!res.ok) {
6731
throw new Error(res.statusText);
6832
}
6933

7034
return cw.set(input, res, forceCache, ttl);
71-
});
35+
};
7236

7337
/**
7438
* @param {string|URL} input
7539
* @returns {Promise<Response|null>}
7640
*/
77-
const has = (input) => open().then(cacheWrapper).then((cw) => cw.has(input));
41+
const has = (input) => cw.has(input);
7842

7943
/**
8044
* @param {string|URL} input
8145
* @returns {Promise<boolean>}
8246
*/
83-
const del = (input) => open().then(cacheWrapper).then((cw) => cw.del(input));
47+
const del = (input) => cw.del(input);
8448

8549
/**
8650
* @param {string} input
@@ -101,8 +65,8 @@ export const cache = (cacheName) => {
10165
*/
10266
const fetchPut = () => request(HTTP_GET, input).withCancel(cancel).withRetry().default();
10367

104-
const inflightPromise = open()
105-
.then(() => window.isSecureContext ? has(input).then((res) => res ? Promise.resolve(res) : del(input).then(fetchPut).then((r) => set(input, r))) : fetchPut())
68+
const inflightPromise = has(input)
69+
.then((res) => res ? Promise.resolve(res) : del(input).then(fetchPut).then((r) => set(input, r)))
10670
.then((r) => r.blob())
10771
.then((b) => objectUrls.set(input, URL.createObjectURL(b)))
10872
.then(() => objectUrls.get(input))
@@ -117,13 +81,9 @@ export const cache = (cacheName) => {
11781
* @param {Promise<void>|null} cancel
11882
* @returns {Promise<void>}
11983
*/
120-
const run = (items, cancel = null) => open().then(() => {
84+
const run = (items, cancel = null) => {
12185
const uniq = new Map();
12286

123-
if (!window.isSecureContext) {
124-
console.warn('Cache is not supported in insecure context');
125-
}
126-
12787
if (items.length === 0) {
12888
return Promise.resolve();
12989
}
@@ -143,7 +103,7 @@ export const cache = (cacheName) => {
143103
return r;
144104
})
145105
));
146-
});
106+
};
147107

148108
/**
149109
* @param {string} input

0 commit comments

Comments
 (0)