GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — javascript-toolkit/utils/state-queue.mjs at main · virtualitems/javascript-toolkit

Expand file tree

/

state-queue.mjs

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
66 lines (51 loc) · 1.34 KB
/

state-queue.mjs

Copy path

File metadata and controls

66 lines (51 loc) · 1.34 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
const SET_ACTION = 'set_action'
const UPDATE_ACTION = 'update_action'
export class State {
#value
#queue = []
constructor(value) {
this.#value = value
}
get value() {
return this.#value
}
set(value) {
this.#schedule(SET_ACTION, value)
}
update(updater) {
if (typeof updater !== 'function') {
throw new TypeError('Updater must be a function')
}
this.#schedule(UPDATE_ACTION, updater)
}
#consume() {
while (this.#queue.length > 0) {
const item = this.#queue[0]
const { action, details } = item
switch (action) {
case SET_ACTION: {
this.#value = details[0]
break
}
case UPDATE_ACTION: {
const updater = details[0]
this.#value = updater(this.#value)
break
}
default:
throw new TypeError('Unknown state action')
}
this.#queue.shift()
}
}
#schedule(action, ...details) {
const isIdle = this.#queue.length === 0
this.#queue.push({
action,
details
})
if (isIdle === true) {
this.#consume()
}
}
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.