Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Sharkey
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Dima Krasner
Sharkey
Commits
8e290ba7
Commit
8e290ba7
authored
3 years ago
by
tamaina
Browse files
Options
Downloads
Patches
Plain Diff
fix
parent
48a16951
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
packages/client/src/pizzax.ts
+44
-38
44 additions, 38 deletions
packages/client/src/pizzax.ts
with
44 additions
and
38 deletions
packages/client/src/pizzax.ts
+
44
−
38
View file @
8e290ba7
...
...
@@ -17,7 +17,8 @@ type ArrayElement<A> = A extends readonly (infer T)[] ? T : never;
const
connection
=
$i
&&
stream
.
useChannel
(
'
main
'
);
export
class
Storage
<
T
extends
StateDef
>
{
public
readonly
ready
:
PromiseLike
<
void
>
;
public
readonly
ready
:
Promise
<
void
>
;
public
readonly
loaded
:
Promise
<
void
>
;
public
readonly
key
:
string
;
public
readonly
deviceStateKeyName
:
string
;
...
...
@@ -49,32 +50,53 @@ export class Storage<T extends StateDef> {
this
.
def
=
def
;
this
.
ready
=
this
.
init
();
this
.
loaded
=
this
.
ready
.
then
(()
=>
this
.
load
());
}
private
init
():
Promise
<
void
>
{
return
new
Promise
(
async
(
resolve
,
reject
)
=>
{
await
this
.
migrate
();
private
async
init
():
Promise
<
void
>
{
await
this
.
migrate
();
const
deviceState
:
State
<
T
>
=
await
get
(
this
.
deviceStateKeyName
)
||
{};
const
deviceAccountState
=
$i
?
await
get
(
this
.
deviceAccountStateKeyName
)
||
{}
:
{};
const
registryCache
=
$i
?
await
get
(
this
.
registryCacheKeyName
)
||
{}
:
{};
const
deviceState
:
State
<
T
>
=
await
get
(
this
.
deviceStateKeyName
)
||
{};
const
deviceAccountState
=
$i
?
await
get
(
this
.
deviceAccountStateKeyName
)
||
{}
:
{};
const
registryCache
=
$i
?
await
get
(
this
.
registryCacheKeyName
)
||
{}
:
{};
for
(
const
[
k
,
v
]
of
Object
.
entries
(
this
.
def
)
as
[
keyof
T
,
T
[
keyof
T
]][])
{
if
(
v
.
where
===
'
device
'
&&
Object
.
prototype
.
hasOwnProperty
.
call
(
deviceState
,
k
))
{
this
.
state
[
k
]
=
deviceState
[
k
];
}
else
if
(
v
.
where
===
'
account
'
&&
$i
&&
Object
.
prototype
.
hasOwnProperty
.
call
(
registryCache
,
k
))
{
this
.
state
[
k
]
=
registryCache
[
k
];
}
else
if
(
v
.
where
===
'
deviceAccount
'
&&
Object
.
prototype
.
hasOwnProperty
.
call
(
deviceAccountState
,
k
))
{
this
.
state
[
k
]
=
deviceAccountState
[
k
];
}
else
{
this
.
state
[
k
]
=
v
.
default
;
if
(
_DEV_
)
console
.
log
(
'
Use default value
'
,
k
,
v
.
default
);
}
}
for
(
const
[
k
,
v
]
of
Object
.
entries
(
this
.
state
)
as
[
keyof
T
,
T
[
keyof
T
]][])
{
this
.
reactiveState
[
k
]
=
ref
(
v
);
for
(
const
[
k
,
v
]
of
Object
.
entries
(
this
.
def
)
as
[
keyof
T
,
T
[
keyof
T
]][])
{
if
(
v
.
where
===
'
device
'
&&
Object
.
prototype
.
hasOwnProperty
.
call
(
deviceState
,
k
))
{
this
.
state
[
k
]
=
deviceState
[
k
];
}
else
if
(
v
.
where
===
'
account
'
&&
$i
&&
Object
.
prototype
.
hasOwnProperty
.
call
(
registryCache
,
k
))
{
this
.
state
[
k
]
=
registryCache
[
k
];
}
else
if
(
v
.
where
===
'
deviceAccount
'
&&
Object
.
prototype
.
hasOwnProperty
.
call
(
deviceAccountState
,
k
))
{
this
.
state
[
k
]
=
deviceAccountState
[
k
];
}
else
{
this
.
state
[
k
]
=
v
.
default
;
if
(
_DEV_
)
console
.
log
(
'
Use default value
'
,
k
,
v
.
default
);
}
}
for
(
const
[
k
,
v
]
of
Object
.
entries
(
this
.
state
)
as
[
keyof
T
,
T
[
keyof
T
]][])
{
this
.
reactiveState
[
k
]
=
ref
(
v
);
}
if
(
$i
)
{
// streamingのuser storage updateイベントを監視して更新
connection
?.
on
(
'
registryUpdated
'
,
({
scope
,
key
,
value
}:
{
scope
?:
string
[],
key
:
keyof
T
,
value
:
T
[
typeof
key
][
'
default
'
]
})
=>
{
if
(
!
scope
||
scope
.
length
!==
2
||
scope
[
0
]
!==
'
client
'
||
scope
[
1
]
!==
this
.
key
||
this
.
state
[
key
]
===
value
)
return
;
this
.
state
[
key
]
=
value
;
this
.
reactiveState
[
key
].
value
=
value
;
this
.
addIdbSetJob
(
async
()
=>
{
const
cache
=
await
get
(
this
.
registryCacheKeyName
);
if
(
cache
[
key
]
!==
value
)
{
cache
[
key
]
=
value
;
await
set
(
this
.
registryCacheKeyName
,
cache
);
}
});
});
}
}
private
async
load
():
Promise
<
void
>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
if
(
$i
)
{
// なぜかsetTimeoutしないとapi関数内でエラーになる(おそらく循環参照してることに原因がありそう)
setTimeout
(()
=>
{
...
...
@@ -93,27 +115,11 @@ export class Storage<T extends StateDef> {
}
}
}
return
set
(
this
.
registryCacheKeyName
,
cache
);
})
.
then
(()
=>
resolve
());
},
1
);
// streamingのuser storage updateイベントを監視して更新
connection
?.
on
(
'
registryUpdated
'
,
({
scope
,
key
,
value
}:
{
scope
:
string
[],
key
:
keyof
T
,
value
:
T
[
typeof
key
][
'
default
'
]
})
=>
{
if
(
scope
.
length
!==
2
||
scope
[
0
]
!==
'
client
'
||
scope
[
1
]
!==
this
.
key
||
this
.
state
[
key
]
===
value
)
return
;
this
.
state
[
key
]
=
value
;
this
.
reactiveState
[
key
].
value
=
value
;
this
.
addIdbSetJob
(
async
()
=>
{
const
cache
=
await
get
(
this
.
registryCacheKeyName
);
if
(
cache
[
key
]
!==
value
)
{
cache
[
key
]
=
value
;
await
set
(
this
.
registryCacheKeyName
,
cache
);
}
}
});
}
});
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment