source: frontend/node_modules/side-channel-list/index.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.2 KB
Line 
1'use strict';
2
3var inspect = require('object-inspect');
4
5var $TypeError = require('es-errors/type');
6
7/*
8* This function traverses the list returning the node corresponding to the given key.
9*
10* That node is also moved to the head of the list, so that if it's accessed again we don't need to traverse the whole list.
11* By doing so, all the recently used nodes can be accessed relatively quickly.
12*/
13/** @type {import('./list.d.ts').listGetNode} */
14// eslint-disable-next-line consistent-return
15var listGetNode = function (list, key, isDelete) {
16 /** @type {typeof list | NonNullable<(typeof list)['next']>} */
17 var prev = list;
18 /** @type {(typeof list)['next']} */
19 var curr;
20 // eslint-disable-next-line eqeqeq
21 for (; (curr = prev.next) != null; prev = curr) {
22 if (curr.key === key) {
23 prev.next = curr.next;
24 if (!isDelete) {
25 // eslint-disable-next-line no-extra-parens
26 curr.next = /** @type {NonNullable<typeof list.next>} */ (list.next);
27 list.next = curr; // eslint-disable-line no-param-reassign
28 }
29 return curr;
30 }
31 }
32};
33
34/** @type {import('./list.d.ts').listGet} */
35var listGet = function (objects, key) {
36 if (!objects) {
37 return void undefined;
38 }
39 var node = listGetNode(objects, key);
40 return node && node.value;
41};
42/** @type {import('./list.d.ts').listSet} */
43var listSet = function (objects, key, value) {
44 var node = listGetNode(objects, key);
45 if (node) {
46 node.value = value;
47 } else {
48 // Prepend the new node to the beginning of the list
49 objects.next = /** @type {import('./list.d.ts').ListNode<typeof value, typeof key>} */ ({ // eslint-disable-line no-param-reassign, no-extra-parens
50 key: key,
51 next: objects.next,
52 value: value
53 });
54 }
55};
56/** @type {import('./list.d.ts').listHas} */
57var listHas = function (objects, key) {
58 if (!objects) {
59 return false;
60 }
61 return !!listGetNode(objects, key);
62};
63/** @type {import('./list.d.ts').listDelete} */
64// eslint-disable-next-line consistent-return
65var listDelete = function (objects, key) {
66 if (objects) {
67 return listGetNode(objects, key, true);
68 }
69};
70
71/** @type {import('.')} */
72module.exports = function getSideChannelList() {
73 /** @typedef {ReturnType<typeof getSideChannelList>} Channel */
74 /** @typedef {Parameters<Channel['get']>[0]} K */
75 /** @typedef {Parameters<Channel['set']>[1]} V */
76
77 /** @type {import('./list.d.ts').RootNode<V, K> | undefined} */ var $o;
78
79 /** @type {Channel} */
80 var channel = {
81 assert: function (key) {
82 if (!channel.has(key)) {
83 throw new $TypeError('Side channel does not contain ' + inspect(key));
84 }
85 },
86 'delete': function (key) {
87 var deletedNode = listDelete($o, key);
88 if (deletedNode && $o && !$o.next) {
89 $o = void undefined;
90 }
91 return !!deletedNode;
92 },
93 get: function (key) {
94 return listGet($o, key);
95 },
96 has: function (key) {
97 return listHas($o, key);
98 },
99 set: function (key, value) {
100 if (!$o) {
101 // Initialize the linked list as an empty node, so that we don't have to special-case handling of the first node: we can always refer to it as (previous node).next, instead of something like (list).head
102 $o = {
103 next: void undefined
104 };
105 }
106 // eslint-disable-next-line no-extra-parens
107 listSet(/** @type {NonNullable<typeof $o>} */ ($o), key, value);
108 }
109 };
110 return channel;
111};
Note: See TracBrowser for help on using the repository browser.