# \[Feature request\] How to tell if historyStateChanged event is for new state?

**URL:** <https://forums.creativeclouddeveloper.com/t/feature-request-how-to-tell-if-historystatechanged-event-is-for-new-state/4725>\
**Category:** UXP Plugin API\
**Tags:** feature-request, uxp, javascript, batch-play\
**Created:** [May 25, 2022, 6:13am UTC](https://forums.creativeclouddeveloper.com/t/feature-request-how-to-tell-if-historystatechanged-event-is-for-new-state/4725 "2022-05-25T06:13:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karmalakas](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/karmalakas/32/1048_2.png) [@Karmalakas](https://forums.creativeclouddeveloper.com/u/Karmalakas)\
**Post date:** [May 25, 2022, 6:13am UTC](https://forums.creativeclouddeveloper.com/t/feature-request-how-to-tell-if-historystatechanged-event-is-for-new-state/4725/1 "2022-05-25T06:13:12Z")

</div>

My goal is to detect if there’s an active selection using notification listener. Initially I used `set` event and checked if that’s for (de)selection, but I figured I also need to check history state if user changes - I should also detect if there’s an active selection. So on `historyStateChanged` I get doc `selection` property and see if there’s one or not.

The problem is, I can’t see how to identify if history state is new, or user undid/redid something. I wouldn’t want to call BP getting doc prop on every user action 😕

So if there’s no way to know if state is new, that would be a feature request for the event - please add a boolean flag like `newState` or similar. Or at least total states count maybe, so that we were able to compare with `itemIndex` 🤔

---

<div class="post-metadata">

**Author:** ![simonhenke](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/simonhenke/32/957_2.png) [@simonhenke](https://forums.creativeclouddeveloper.com/u/simonhenke)\
**Post date:** [May 25, 2022, 9:02am UTC](https://forums.creativeclouddeveloper.com/t/feature-request-how-to-tell-if-historystatechanged-event-is-for-new-state/4725/2 "2022-05-25T09:02:24Z")

</div>

There’s a [count](https://github.com/simonhenke/photoshop-types/blob/main/types/History.d.ts#L4) property, which should be the total count of history steps.  
Also, history states have an `ID` so you could remember the highest `ID` and if you listen to a `historyStateChanged` event whose `ID` isn’t higher than that, it has been an undo.

---

<div class="post-metadata">

**Author:** ![Karmalakas](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/karmalakas/32/1048_2.png) [@Karmalakas](https://forums.creativeclouddeveloper.com/u/Karmalakas)\
**Post date:** [May 25, 2022, 9:21am UTC](https://forums.creativeclouddeveloper.com/t/feature-request-how-to-tell-if-historystatechanged-event-is-for-new-state/4725/3 "2022-05-25T09:21:44Z")

</div>

> [@simonhenke](#):
>
> There’s a [count](https://github.com/simonhenke/photoshop-types/blob/main/types/History.d.ts#L4) property, which should be the total count of history steps

At least I didn’t see this in Alchemist listener result. Would’ve definitely used that. Will recheck later 😕

The approach with keeping ID in memory, isn’t what I’d like to do, because it also would require getting highest IDs of all open documents (and saving them; and updating them) on plugin load, but will consider

---

<div class="post-metadata">

**Author:** ![Karmalakas](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/karmalakas/32/1048_2.png) [@Karmalakas](https://forums.creativeclouddeveloper.com/u/Karmalakas)\
**Post date:** [May 25, 2022, 3:58pm UTC](https://forums.creativeclouddeveloper.com/t/feature-request-how-to-tell-if-historystatechanged-event-is-for-new-state/4725/4 "2022-05-25T15:58:50Z")

</div>

So I checked and `count` isn’t there 😕  
But now I figured that count wouldn’t help anyway, because on the last state there would still be no way to know if it’s a new state or just a _Redo_. Implementing the suggested saved IDs for each open document seems like overkill ☹

I’ll leave for now BP calls and see how it goes while testing, but a flag would be a proper solution IMO

---

<div class="post-metadata">

**Author:** ![Jarda](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/jarda/32/1909_2.png) [@Jarda](https://forums.creativeclouddeveloper.com/u/Jarda)\
**Post date:** [May 25, 2022, 5:02pm UTC](https://forums.creativeclouddeveloper.com/t/feature-request-how-to-tell-if-historystatechanged-event-is-for-new-state/4725/5 "2022-05-25T17:02:03Z")

</div>

You should also take snapshots into consideration. And possibility to manually remove history states. Also you can set number of history states down to 1. Then you can also have linear and non-linear history so ID numbers of history steps might not have only incremental order. But each history ID should be unique within PS session. Also you can have multiple documents so you need to keep track in which document event happened.

---

<div class="post-metadata">

**Author:** ![Karmalakas](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/karmalakas/32/1048_2.png) [@Karmalakas](https://forums.creativeclouddeveloper.com/u/Karmalakas)\
**Post date:** [May 25, 2022, 5:19pm UTC](https://forums.creativeclouddeveloper.com/t/feature-request-how-to-tell-if-historystatechanged-event-is-for-new-state/4725/6 "2022-05-25T17:19:48Z")

</div>

I think I’ve got it all covered with 3 events:

- `historyStateChanged` (self explanatory)
- `layersFiltered` (trigers whenever active document changes)
- `modalJavaScriptScopeExit` (trigers when some plugin changes state)

On all 3 I still check documents `selection` property 🤔  
`historyStateChanged` is not triggered when other two happen.

BTW, any info what exactly `layersFiltered` is?
