|
2 | 2 | import type { PayloadsList } from "@nodesecure/cache"; |
3 | 3 |
|
4 | 4 | // Import Internal Dependencies |
| 5 | +import { context } from "../websocket.als.js"; |
5 | 6 | import type { |
6 | | - WebSocketContext, |
7 | | - WebSocketResponse |
| 7 | + WebSocketResponse, |
| 8 | + WebSocketContext |
8 | 9 | } from "../websocket.types.js"; |
9 | 10 |
|
10 | 11 | export async function* remove( |
11 | | - spec: string, |
12 | | - context: WebSocketContext |
| 12 | + spec: string |
13 | 13 | ): AsyncGenerator<WebSocketResponse, void, unknown> { |
14 | | - const { cache, logger } = context; |
| 14 | + const ctx = context.getStore()!; |
15 | 15 |
|
16 | | - const { mru, lru, current, lastUsed, root, availables } = await cache.payloadsList(); |
17 | | - delete lastUsed[spec]; |
18 | | - if (availables.includes(spec)) { |
19 | | - logger.info("[ws|command.remove] remove from availables"); |
20 | | - cache.removePayload(spec); |
21 | | - const updatedList: PayloadsList = { |
22 | | - mru, |
23 | | - current, |
24 | | - lru, |
25 | | - lastUsed: { |
26 | | - ...lastUsed |
27 | | - }, |
28 | | - root, |
29 | | - availables: availables.filter((iterSpec) => iterSpec !== spec) |
30 | | - }; |
31 | | - await cache.updatePayloadsList(updatedList); |
32 | | - |
33 | | - yield { |
34 | | - status: "RELOAD", |
35 | | - cache: updatedList |
36 | | - }; |
37 | | - |
38 | | - return; |
| 16 | + const cacheList = await ctx.cache.payloadsList(); |
| 17 | + let updatedList: PayloadsList; |
| 18 | + if (cacheList.availables.includes(spec)) { |
| 19 | + updatedList = await removeFromAvailables(spec, ctx, cacheList); |
| 20 | + } |
| 21 | + else { |
| 22 | + updatedList = await removeFromMruOrLru(spec, ctx, cacheList); |
39 | 23 | } |
40 | 24 |
|
41 | | - logger.debug(`[ws|command.remove](lru: ${lru}|current: ${current})`); |
| 25 | + yield { |
| 26 | + status: "RELOAD", |
| 27 | + cache: updatedList |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +async function removeFromAvailables( |
| 32 | + spec: string, |
| 33 | + context: WebSocketContext, |
| 34 | + cacheList: PayloadsList |
| 35 | +): Promise<PayloadsList> { |
| 36 | + const { cache, logger } = context; |
| 37 | + const { availables, lastUsed, ...rest } = cacheList; |
42 | 38 |
|
| 39 | + logger.info("[ws|command.remove] remove from availables"); |
| 40 | + const { [spec]: _, ...updatedLastUsed } = lastUsed; |
| 41 | + const updatedList: PayloadsList = { |
| 42 | + ...rest, |
| 43 | + lastUsed: updatedLastUsed, |
| 44 | + availables: availables.filter((iterSpec) => iterSpec !== spec) |
| 45 | + }; |
| 46 | + |
| 47 | + await cache.updatePayloadsList(updatedList); |
| 48 | + cache.removePayload(spec); |
| 49 | + |
| 50 | + return updatedList; |
| 51 | +} |
| 52 | + |
| 53 | +async function removeFromMruOrLru( |
| 54 | + spec: string, |
| 55 | + context: WebSocketContext, |
| 56 | + cacheList: PayloadsList |
| 57 | +): Promise<PayloadsList> { |
| 58 | + const { logger, cache } = context; |
| 59 | + const { mru, lru, current } = cacheList; |
| 60 | + |
| 61 | + logger.debug(`[ws|command.remove](lru: ${lru}|current: ${current})`); |
43 | 62 | if (mru.length === 1 && lru.length === 0) { |
44 | 63 | throw new Error("Cannot remove the last package."); |
45 | 64 | } |
46 | 65 |
|
47 | 66 | const mruIndex = mru.findIndex((iterSpec) => iterSpec === spec); |
48 | 67 | const lruIndex = lru.findIndex((iterSpec) => iterSpec === spec); |
49 | | - |
50 | 68 | if (mruIndex === -1 && lruIndex === -1) { |
51 | 69 | throw new Error("Package not found in cache."); |
52 | 70 | } |
53 | 71 |
|
54 | | - if (mruIndex > -1) { |
55 | | - logger.info("[ws|command.remove] removed from mru"); |
56 | | - const updatedMru = mru.filter((iterSpec) => iterSpec !== spec); |
57 | | - if (lru.length > 0) { |
58 | | - // We need to move the first lru package to the mru list |
59 | | - const olderLruPkg = lru.sort((a, b) => { |
60 | | - const aDate = lastUsed[a]; |
61 | | - const bDate = lastUsed[b]; |
62 | | - |
63 | | - return aDate - bDate; |
64 | | - }); |
65 | | - updatedMru.push(olderLruPkg[0]); |
66 | | - lru.splice(lru.indexOf(olderLruPkg[0]), 1); |
67 | | - } |
68 | | - |
69 | | - const updatedList: PayloadsList = { |
70 | | - mru: updatedMru, |
71 | | - lru, |
72 | | - lastUsed: { |
73 | | - ...lastUsed |
74 | | - }, |
75 | | - current: current === spec ? updatedMru[0] : current, |
76 | | - root, |
77 | | - availables |
78 | | - }; |
79 | | - await cache.updatePayloadsList(updatedList); |
80 | | - |
81 | | - yield { |
82 | | - status: "RELOAD", |
83 | | - cache: updatedList |
84 | | - }; |
85 | | - } |
86 | | - else { |
87 | | - logger.info("[ws|command.remove] removed from lru"); |
88 | | - const updatedLru = lru.filter((iterSpec) => iterSpec !== spec); |
89 | | - const updatedList: PayloadsList = { |
90 | | - mru, |
91 | | - lru: updatedLru, |
92 | | - availables, |
93 | | - lastUsed: { |
94 | | - ...lastUsed |
95 | | - }, |
96 | | - current, |
97 | | - root |
98 | | - }; |
99 | | - await cache.updatePayloadsList(updatedList); |
100 | | - |
101 | | - yield { |
102 | | - status: "RELOAD", |
103 | | - cache: updatedList |
104 | | - }; |
105 | | - } |
| 72 | + const isInMru = mruIndex > -1; |
| 73 | + logger.info(`[ws|command.remove] removing from ${isInMru ? "MRU" : "LRU"}`); |
| 74 | + const updatedList = isInMru ? |
| 75 | + removeFromMru(spec, cacheList) : |
| 76 | + removeFromLru(spec, cacheList); |
106 | 77 |
|
| 78 | + await cache.updatePayloadsList(updatedList); |
107 | 79 | cache.removePayload(spec); |
| 80 | + |
| 81 | + return updatedList; |
| 82 | +} |
| 83 | + |
| 84 | +function removeFromMru( |
| 85 | + spec: string, |
| 86 | + cacheList: PayloadsList |
| 87 | +): PayloadsList { |
| 88 | + const { mru, lru, current, lastUsed, root, availables } = cacheList; |
| 89 | + |
| 90 | + const updatedMru = mru.filter((iterSpec) => iterSpec !== spec); |
| 91 | + let updatedLru = lru; |
| 92 | + |
| 93 | + if (lru.length > 0) { |
| 94 | + const sortedLru = [...lru].sort((a, b) => lastUsed[a] - lastUsed[b]); |
| 95 | + const olderLruPkg = sortedLru[0]; |
| 96 | + updatedMru.push(olderLruPkg); |
| 97 | + updatedLru = lru.filter((iterSpec) => iterSpec !== olderLruPkg); |
| 98 | + } |
| 99 | + |
| 100 | + const { [spec]: _, ...updatedLastUsed } = lastUsed; |
| 101 | + const updatedList: PayloadsList = { |
| 102 | + mru: updatedMru, |
| 103 | + lru: updatedLru, |
| 104 | + lastUsed: updatedLastUsed, |
| 105 | + current: current === spec ? updatedMru[0] : current, |
| 106 | + root, |
| 107 | + availables |
| 108 | + }; |
| 109 | + |
| 110 | + return updatedList; |
| 111 | +} |
| 112 | + |
| 113 | +function removeFromLru( |
| 114 | + spec: string, |
| 115 | + cacheList: PayloadsList |
| 116 | +): PayloadsList { |
| 117 | + const { mru, lru, current, lastUsed, root, availables } = cacheList; |
| 118 | + |
| 119 | + const { [spec]: _, ...updatedLastUsed } = lastUsed; |
| 120 | + const updatedList: PayloadsList = { |
| 121 | + mru, |
| 122 | + lru: lru.filter((iterSpec) => iterSpec !== spec), |
| 123 | + availables, |
| 124 | + lastUsed: updatedLastUsed, |
| 125 | + current, |
| 126 | + root |
| 127 | + }; |
| 128 | + |
| 129 | + return updatedList; |
108 | 130 | } |
0 commit comments