# Using DefineMap's \`value\` behavior to produce "middle tier" events

**URL:** <https://forums.bitovi.com/t/using-definemaps-value-behavior-to-produce-middle-tier-events/910>\
**Category:** Uncategorized\
**Created:** [June 13, 2018, 3:47pm UTC](https://forums.bitovi.com/t/using-definemaps-value-behavior-to-produce-middle-tier-events/910 "2018-06-13T15:47:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![justinbmeyer](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/justinbmeyer/32/179_2.png) [@justinbmeyer](https://forums.bitovi.com/u/justinbmeyer)\
**Post date:** [June 13, 2018, 3:47pm UTC](https://forums.bitovi.com/t/using-definemaps-value-behavior-to-produce-middle-tier-events/910/1 "2018-06-13T15:47:53Z")

</div>

DefineMap’s [value](https://canjs.com/doc/can-define.types.value.html) behavior adds a lot of event-stream power to DefineMap properties. For example, I can use it here to switch a `streamIsOn` property to false after a user leaves the page for 1hr:

```auto
streamIsOn: {
    value({resolve, listenTo}) {
        resol
        var timer;
        listenTo(window,"blur", function(){
            timer = setTimeout(function(){
                resolve(false);
            },1000*60*60)
        })
        listenTo(document,"keypress", function(){
            clearTimeout(timer);
        })
        listenTo(document, "click", function(){
            clearTimeout(timer);
        });
    }
}

```

Lately, I’ve been wanting to produce “middle tier” events that other properties could listen to. The following shows how I can create a `userInteraction` property that I’m using to dispatch `"userInteraction"` events:

```auto
userInteraction: {
    value({resolve, listenTo}) {
        listenTo(document, "click", function(){
            resolve({})
        });
        listenTo(document,"keypress", function(){
            resolve({})
        })
    }
},
streamIsOn: {
    value({resolve, listenTo}) {
        var timer;
        listenTo("userInteraction", function(){
            clearTimeout(timer);
        });
        listenTo(window,"blur", function(){
            timer = setTimeout(function(){
                resolve(false);
            },1000*60*60)
        })
    }
}

```

The secret is `resolve({})`. By passing a new object every time, `resolve()` will always change the value of the `userInteraction` so it will dispatch a new event every time.

---

<div class="post-metadata">

**Author:** ![cherif\_b](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/cherif_b/32/31_2.png) [@cherif\_b](https://forums.bitovi.com/u/cherif_b)\
**Post date:** [June 14, 2018, 1:51am UTC](https://forums.bitovi.com/t/using-definemaps-value-behavior-to-produce-middle-tier-events/910/2 "2018-06-14T01:51:51Z")

</div>

@justinbmeyer this is awesome, do you recommend this approach or the reactive one using `can-kefir` to handle side-effects?

---

<div class="post-metadata">

**Author:** ![justinbmeyer](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/justinbmeyer/32/179_2.png) [@justinbmeyer](https://forums.bitovi.com/u/justinbmeyer)\
**Post date:** [June 14, 2018, 2:51am UTC](https://forums.bitovi.com/t/using-definemaps-value-behavior-to-produce-middle-tier-events/910/3 "2018-06-14T02:51:36Z")

</div>

I think it depends on the app. Many apps have lots of basic logic, that getters and value() can easily solve. Once things get really hairy, I’d probably pull in kefir. It’s hard to say.

---

<div class="post-metadata">

**Author:** ![cherif\_b](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/cherif_b/32/31_2.png) [@cherif\_b](https://forums.bitovi.com/u/cherif_b)\
**Post date:** [June 14, 2018, 11:02am UTC](https://forums.bitovi.com/t/using-definemaps-value-behavior-to-produce-middle-tier-events/910/4 "2018-06-14T11:02:29Z")

</div>

Yes it makes sens, thank you!
