Skip to content
This repository has been archived by the owner on Jul 30, 2022. It is now read-only.

Add State:Reset() method and State:SetState() now also accepts a callback #8

Merged
merged 5 commits into from Nov 4, 2021

Conversation

Gaffal
Copy link
Contributor

@Gaffal Gaffal commented Jul 22, 2021

State:Reset()

Simply resets the current state to the initial state

local state = BasicState.new({
	name = 'bob',
	age = 50,
})

state:SetState({
	age = 25,
	random_info = true,
})

print(state)

state:Reset()
print(state)

Result:
State Result

State:SetState(callback: Callback)

Just like Roact's setState, this method also accepts a callback:

local state = BasicState.new({
	animal = 'dog',
	age = 1,
})

state:SetState(function(state)
	if (state.animal == 'dog') then
		state.cute = true
	end
	
	state.age += 1
	return state  -- the state (copy of the current state) should be returned
end)

print(state)

Result:
Screenshot_1

@cxmeel cxmeel self-requested a review November 2, 2021 16:07
@cxmeel cxmeel self-assigned this Nov 2, 2021
@cxmeel cxmeel added the enhancement New feature or request label Nov 2, 2021
@cxmeel cxmeel merged commit 75bf4f9 into cxmeel:default Nov 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants