FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ScratchAddons/addon-api/content-script/ReduxHandler.js at patch-2 · rosics-code/ScratchAddons · GitHub
rosics-code
/
ScratchAddons
Public
forked from
ScratchAddons/ScratchAddons
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ScratchAddons
/
addon-api
/
content-script
/
ReduxHandler.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
74 lines (69 loc) · 2.29 KB
Breadcrumbs
ScratchAddons
/
addon-api
/
content-script
/
ReduxHandler.js
Copy path
File metadata and controls
74 lines (69 loc) · 2.29 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
67
68
69
70
71
72
73
74
import
Listenable
from
"../common/Listenable.js"
;
/**
* Handles Redux state.
*
@extends
Listenable
*
@property
{
boolean
} initialized Whether the handler is initialized or not.
*/
export
default
class
ReduxHandler
extends
Listenable
{
constructor
(
)
{
super
(
)
;
this
.
initialized
=
false
;
this
.
initialize
(
)
;
}
/**
* Initialize the handler. Must be called before adding events.
*/
initialize
(
)
{
if
(
!
__scratchAddonsRedux
.
target
||
this
.
initialized
)
return
;
this
.
initialized
=
true
;
__scratchAddonsRedux
.
target
.
addEventListener
(
"statechanged"
,
(
{
detail
}
)
=>
{
const
newEvent
=
new
CustomEvent
(
"statechanged"
,
{
detail
:
{
action
:
detail
.
action
,
prev
:
detail
.
prev
,
next
:
detail
.
next
,
}
,
}
)
;
this
.
dispatchEvent
(
newEvent
)
;
}
)
;
}
/**
* Redux state.
*
@type
{
object
}
*/
get
state
(
)
{
return
__scratchAddonsRedux
.
state
;
}
/**
* Dispatches redux state change.
*
@param
{
object
} payload - payload to pass to redux.
*
@throws
when Redux is unavailable.
*/
dispatch
(
payload
)
{
if
(
!
__scratchAddonsRedux
.
dispatch
)
throw
new
Error
(
"Redux is unavailable"
)
;
__scratchAddonsRedux
.
dispatch
(
payload
)
;
}
/**
* Waits until a state meets the condition.
*
@param
{
function
} condition - a function that takes redux state and returns whether to keep waiting or not.
*
@param
{
object=
} opts - options.
*
@param
{
string=|string[]=
} actions - the action(s) to check for.
*
@returns
{
Promise
} a Promise resolved when the state meets the condition.
*/
waitForState
(
condition
,
opts
=
{
}
)
{
this
.
initialize
(
)
;
if
(
!
__scratchAddonsRedux
.
target
)
return
Promise
.
reject
(
new
Error
(
"Redux is unavailable"
)
)
;
if
(
condition
(
__scratchAddonsRedux
.
state
)
)
return
Promise
.
resolve
(
)
;
let
actions
=
opts
.
actions
||
null
;
if
(
typeof
actions
===
"string"
)
actions
=
[
actions
]
;
return
new
Promise
(
(
resolve
)
=>
{
const
listener
=
(
{
detail
}
)
=>
{
if
(
actions
&&
!
actions
.
includes
(
detail
.
action
.
type
)
)
return
;
if
(
!
condition
(
detail
.
next
)
)
return
;
__scratchAddonsRedux
.
target
.
removeEventListener
(
"statechanged"
,
listener
)
;
setTimeout
(
resolve
,
0
)
;
}
;
__scratchAddonsRedux
.
target
.
addEventListener
(
"statechanged"
,
listener
)
;
}
)
;
}
}
Back
|
FazBrowse Home
|
New Git URL