Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove expired persisted states #837

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pkg/app/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ func NewClientTester(n UI) ClientDispatcher {
e := &engine{
ActionHandlers: actionHandlers,
}

if IsClient {
e.LocalStorage = newJSStorage("localStorage")
e.LocalStorage.Clear()

e.SessionStorage = newJSStorage("sessionStorage")
e.SessionStorage.Clear()
}

e.init()
e.Mount(n)
e.Consume()
Expand Down
1 change: 1 addition & 0 deletions pkg/app/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ func (e *engine) start(ctx context.Context) {
currentFrameDuration := frameDuration
frames := time.NewTicker(frameDuration)

e.states.Cleanup()
cleanups := time.NewTicker(time.Minute)
defer cleanups.Stop()

Expand Down
24 changes: 24 additions & 0 deletions pkg/app/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ func (s *store) Cleanup() {

s.removeUnusedObservers()
s.expireExpiredValues()
s.expireExpriredPersistentValues()
}

func (s *store) Close() {
Expand Down Expand Up @@ -371,6 +372,29 @@ func (s *store) expireExpiredValues() {
}
}

func (s *store) expireExpriredPersistentValues() {
object := Window().Get("Object")
if !object.Truthy() {
return
}

keys := object.Call("keys", Window().Get("localStorage"))
for i, l := 0, keys.Get("length").Int(); i < l; i++ {
key := keys.Index(i).String()

var state persistentState
s.disp.getLocalStorage().Get(key, &state)

if state.EncryptedValue == nil && state.Value == nil && state.ExpiresAt == (time.Time{}) {
continue
}

if state.isExpired(time.Now()) {
s.disp.getLocalStorage().Del(key)
}
}
}

func (s *store) expire(key string, state State) State {
s.disp.getLocalStorage().Del(key)
state.value = nil
Expand Down
40 changes: 40 additions & 0 deletions pkg/app/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,3 +576,43 @@ type copyTester struct {
slice []int
mapp map[string]int
}

func TestExpireExpriredPersistentValues(t *testing.T) {
if IsServer {
t.Skip()
}

d := NewClientTester(&foo{})
defer d.Close()
localStorage := d.getLocalStorage()

s := newStore(d)
defer s.Close()

t.Run("non expired state is not removed", func(t *testing.T) {
localStorage.Clear()
s.setPersistent("/hello", false, time.Now().Add(time.Minute), "hello")
require.Equal(t, 1, localStorage.Len())

s.expireExpriredPersistentValues()
require.Equal(t, 1, localStorage.Len())
})

t.Run("expired state is removed", func(t *testing.T) {
localStorage.Clear()
s.setPersistent("/bye", false, time.Now().Add(-time.Minute), "bye")
require.Equal(t, 1, localStorage.Len())

s.expireExpriredPersistentValues()
require.Equal(t, 0, localStorage.Len())
})

t.Run("non state value is not removed", func(t *testing.T) {
localStorage.Clear()
localStorage.Set("/hi", "hi")
require.Equal(t, 1, localStorage.Len())

s.expireExpriredPersistentValues()
require.Equal(t, 1, localStorage.Len())
})
}