Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
H
Hubs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ar-noc
Hubs
Commits
2498e3e1
Commit
2498e3e1
authored
7 years ago
by
Greg Fodor
Browse files
Options
Downloads
Patches
Plain Diff
Added store
parent
47133532
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/storage/store.js
+77
-0
77 additions, 0 deletions
src/storage/store.js
with
77 additions
and
0 deletions
src/storage/store.js
0 → 100644
+
77
−
0
View file @
2498e3e1
import
uuid
from
"
uuid/v4
"
;
import
{
Validator
}
from
"
jsonschema
"
;
const
LOCAL_STORE_KEY
=
"
___mozilla_hubs
"
;
const
STORE_STATE_CACHE_KEY
=
Symbol
();
const
validator
=
new
Validator
();
// Durable (via local-storage) schema-enforced state that is meant to be consumed via forward data flow.
// (Think flux but with way less incidental complexity, at least for now :))
const
SCHEMA
=
{
id
:
"
/MozillaHubsStore
"
,
definitions
:
{
profile
:
{
type
:
"
object
"
,
additionalProperties
:
false
,
properties
:
{
display_name
:
{
type
:
"
string
"
,
pattern
:
"
^[A-Za-z0-9-]{3,32}$
"
},
}
}
},
type
:
"
object
"
,
properties
:
{
id
:
{
type
:
"
string
"
,
pattern
:
"
^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$
"
},
profile
:
{
"
$ref
"
:
"
#/definitions/profile
"
},
},
additionalProperties
:
false
}
export
default
class
Store
{
subscribers
=
new
Set
();
constructor
()
{
if
(
localStorage
.
getItem
(
LOCAL_STORE_KEY
)
===
null
)
{
localStorage
.
setItem
(
LOCAL_STORE_KEY
,
JSON
.
stringify
({
id
:
uuid
()
}));
}
}
get
state
()
{
if
(
!
this
.
hasOwnProperty
(
STORE_STATE_CACHE_KEY
))
{
this
[
STORE_STATE_CACHE_KEY
]
=
JSON
.
parse
(
localStorage
.
getItem
(
LOCAL_STORE_KEY
));
}
return
this
[
STORE_STATE_CACHE_KEY
];
}
subscribe
(
subscriber
)
{
this
.
subscribers
.
add
(
subscriber
);
}
update
(
newState
)
{
if
(
newState
.
id
)
{
console
.
error
(
"
Store id is immutable.
"
);
return
;
}
const
finalState
=
{
...
this
.
state
,
...
newState
};
const
isValid
=
validator
.
validate
(
finalState
,
SCHEMA
).
valid
;
if
(
!
isValid
)
{
console
.
warn
(
`Write of
${
JSON
.
stringify
(
finalState
)}
to store failed schema validation.`
)
return
;
}
localStorage
.
setItem
(
LOCAL_STORE_KEY
,
JSON
.
stringify
(
finalState
));
delete
this
[
STORE_STATE_CACHE_KEY
];
for
(
const
subscriber
of
this
.
subscribers
)
{
subscriber
();
}
return
finalState
;
}
}
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