QubitProducts / uv-api

Universal Variable API
3 stars 1 forks source link
ceh implement

Universal Variable API

js-standard-style

Codeship Status for qubitdigital/uv-api

The Universal Variable API is a global event emitter used to pass underlying page or view information to third party scripts.

Methodology

Historically, a global variable named window.universal_variable was used for exposing web page or view information to third party scripts however this state based model was too basic and did not fit well with applications that did not do a full page reload.

Using the UV API all data is exposed as events using window.uv. All events conform to schemas defined in the QProtocol schemas.

Setting up and using the UV API

The UV API should be included as an inline script on the webpage at the top of head, immediately after the <meta charset... /> tag. By embedding the API in the head synchronously, any script in the page is able to emit or handle events without polling or waiting for asynchronous scripts to load.

<script>
!function(){function n(){function n(n){p.level=n}function e(n,e){p.info(n,"event emitted"),e=c(e||{}),e.meta=e.meta||{},e.meta.type=n,u.push(e),r(),v.listeners=f(v.listeners,function(n){return!n.disposed})}function o(n,e,o){function r(){return p.info("Replaying events"),t(function(){s(v.events,function(t){c.disposed||l(n,t.meta.type)&&e.call(o,t)})}),f}function i(){return p.info("Disposing event handler"),c.disposed=!0,f}p.info("Attaching event handler for",n);var c={type:n,callback:e,disposed:!1,context:o||window};v.listeners.push(c);var f={replay:r,dispose:i};return f}function t(n){p.info("Calling event handlers"),a++;try{n()}catch(n){p.error("UV API Error",n.stack)}a--,r()}function r(){if(0===u.length&&p.info("No more events to process"),u.length>0&&a>0&&p.info("Event will be processed later"),u.length>0&&0===a){p.info("Processing event");var n=u.shift();v.events.push(n),t(function(){s(v.listeners,function(e){if(!e.disposed&&l(e.type,n.meta.type))try{e.callback.call(e.context,n)}catch(n){p.error("Error emitting UV event",n.stack)}})})}}function i(n,e,o){var t=v.on(n,function(){e.apply(o||window,arguments),t.dispose()});return t}function s(n,e){for(var o=n.length,t=0;t<o;t++)e(n[t],t)}function c(n){var e={};for(var o in n)n.hasOwnProperty(o)&&(e[o]=n[o]);return e}function l(n,e){return"string"==typeof n?n===e:n.test(e)}function f(n,e){for(var o=n.length,t=[],r=0;r<o;r++)e(n[r])&&t.push(n[r]);return t}var u=[],a=0,p={info:function(){p.level>n.INFO||console&&console.info&&console.info.apply(console,arguments)},error:function(){p.level>n.ERROR||console&&console.error&&console.error.apply(console,arguments)}};n.ALL=0,n.INFO=1,n.ERROR=2,n.OFF=3,n(n.ERROR);var v={on:o,emit:e,once:i,events:[],listeners:[],logLevel:n};return v}"object"==typeof module&&module.exports?module.exports=n:window&&void 0===window.uv&&(window.uv=n())}();
</script>

Compatibility

Tested in IE8+, Firefox, Opera, Chrome and Safari.

API

Emit

uv.emit(type, [data])

Emits an event with the type and data specified. The data should conform to the schema for the event type emitted. All events that are emitted are given a meta property with the event type.

uv.emit('ecProduct', {
  product: {
    id: '112-334-a',
    price: 6.99,
    name: '18th Birthday Baloon',
    category: ['Party Accessories', 'Birthday Parties']
  },
  color: 'red',
  stock: 6,
  eventType: 'detail'
})
// => emits an ecProduct event

The emitted event will have meta attached.

{
  "meta": {
    "type": "ecProduct"
  },
  "product": {
    "id": "112-334-a",
    "price": 6.99,
    "name": "18th Birthday Baloon",
    "category": ["Party Accessories", "Birthday Parties"]
  },
  "color": "red",
  "stock": 6,
  "eventType": "detail"
}

On

uv.on(type, handler, [context])

Attaches an event handler to be called when a certain event type is emitted. The handler will be passed the event data and will be bound to the context object, if one is passed. If a regex is passed, the handler will execute on events that match the regex.

uv.on('ecProduct', function (data) {
  console.log(data)
})
// => logs data when an `ecProduct` event is emitted

var subscription = uv.on(/.*/, function (data) {
  console.log(data)
})
// => logs data for all events

The on method returns a subscription object which can detatch the handler using the dispose method and can also be used to replay events currently in the event array. Note that subscription that have been disposed will not call the handler when replay is called.

subscription.dispose()
// => detatches the event handler

subscription.replay()
// => calls the handler for all events currently in uv.events

Once

uv.once(type, handler, [context])

Attaches an event handler that will be called once, only on the next event emitted that matches the type specified. The handler will be passed the event data and will be bound to the context object, if one is passed. Returns a subscription object which can detatch the handler using the dispose method. If a regex is passed, the handler will execute on the next event to match the regex.

uv.once('ecProduct', function (data) {
  console.log(data)
})
emit('ecProduct')
// => logs data

emit('ecProduct')
// => does not log

Events

The events array is a cache of events emitted since the last page load. By iterating over the array it is possible to interpret the user journey or the current state of the page.

NPM module

The uv-api is available on the NPM registry.

npm i uv-api --save

The module exports a createUv function if required using commonjs.

var createUv = require('uv-api')
var uv = createUv()
uv.emit('ecView')