{"id":22374842,"url":"https://github.com/qubitproducts/uv-api","last_synced_at":"2025-07-30T22:31:23.835Z","repository":{"id":26524556,"uuid":"29977545","full_name":"QubitProducts/uv-api","owner":"QubitProducts","description":"Universal Variable API","archived":false,"fork":false,"pushed_at":"2022-01-24T18:23:42.000Z","size":281,"stargazers_count":3,"open_issues_count":1,"forks_count":1,"subscribers_count":45,"default_branch":"master","last_synced_at":"2024-11-20T13:54:13.984Z","etag":null,"topics":["ceh","implement"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/QubitProducts.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-01-28T16:48:43.000Z","updated_at":"2023-06-02T12:08:00.000Z","dependencies_parsed_at":"2022-09-19T02:10:41.417Z","dependency_job_id":null,"html_url":"https://github.com/QubitProducts/uv-api","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QubitProducts%2Fuv-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QubitProducts%2Fuv-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QubitProducts%2Fuv-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QubitProducts%2Fuv-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/QubitProducts","download_url":"https://codeload.github.com/QubitProducts/uv-api/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228154024,"owners_count":17877704,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ceh","implement"],"created_at":"2024-12-04T21:18:41.766Z","updated_at":"2024-12-04T21:18:42.468Z","avatar_url":"https://github.com/QubitProducts.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003c!--\n  This file was generated by 'make-readme.js', edit README.tmpl.md and run 'make build' instead\n--\u003e\nUniversal Variable API\n----------------------\n\n[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)\n\n[ ![Codeship Status for qubitdigital/uv-api](https://codeship.com/projects/f8884a40-8ad8-0132-dedc-76c1126cf0b3/status?branch=master)](https://codeship.com/projects/60163)\n\n\n_The Universal Variable API is a global event emitter used to pass underlying page or view information to third party scripts._\n\nMethodology\n===========\n\nHistorically, 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.\n\nUsing the UV API all data is exposed as events using `window.uv`. All events conform to schemas defined in the QProtocol schemas.\n\nSetting up and using the UV API\n===============================\n\nThe UV API should be included as an inline script on the webpage at the top of head, immediately after the `\u003cmeta charset... /\u003e` 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.\n\n```html\n\u003cscript\u003e\n!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)\u0026\u0026e.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\u0026\u0026p.info(\"No more events to process\"),u.length\u003e0\u0026\u0026a\u003e0\u0026\u0026p.info(\"Event will be processed later\"),u.length\u003e0\u0026\u00260===a){p.info(\"Processing event\");var n=u.shift();v.events.push(n),t(function(){s(v.listeners,function(e){if(!e.disposed\u0026\u0026l(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\u003co;t++)e(n[t],t)}function c(n){var e={};for(var o in n)n.hasOwnProperty(o)\u0026\u0026(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\u003co;r++)e(n[r])\u0026\u0026t.push(n[r]);return t}var u=[],a=0,p={info:function(){p.level\u003en.INFO||console\u0026\u0026console.info\u0026\u0026console.info.apply(console,arguments)},error:function(){p.level\u003en.ERROR||console\u0026\u0026console.error\u0026\u0026console.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\u0026\u0026module.exports?module.exports=n:window\u0026\u0026void 0===window.uv\u0026\u0026(window.uv=n())}();\n\u003c/script\u003e\n```\n\nCompatibility\n=============\n\nTested in IE8+, Firefox, Opera, Chrome and Safari.\n\nAPI\n===\n\n### Emit\n\n`uv.emit(type, [data])`\n\nEmits 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`.\n\n```js\nuv.emit('ecProduct', {\n  product: {\n    id: '112-334-a',\n    price: 6.99,\n    name: '18th Birthday Baloon',\n    category: ['Party Accessories', 'Birthday Parties']\n  },\n  color: 'red',\n  stock: 6,\n  eventType: 'detail'\n})\n// =\u003e emits an ecProduct event\n```\n\nThe emitted event will have meta attached.\n\n```json\n{\n  \"meta\": {\n    \"type\": \"ecProduct\"\n  },\n  \"product\": {\n    \"id\": \"112-334-a\",\n    \"price\": 6.99,\n    \"name\": \"18th Birthday Baloon\",\n    \"category\": [\"Party Accessories\", \"Birthday Parties\"]\n  },\n  \"color\": \"red\",\n  \"stock\": 6,\n  \"eventType\": \"detail\"\n}\n```\n\n\n### On\n\n`uv.on(type, handler, [context])`\n\nAttaches 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.\n\n```js\nuv.on('ecProduct', function (data) {\n  console.log(data)\n})\n// =\u003e logs data when an `ecProduct` event is emitted\n\nvar subscription = uv.on(/.*/, function (data) {\n  console.log(data)\n})\n// =\u003e logs data for all events\n```\n\nThe 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.\n\n```js\nsubscription.dispose()\n// =\u003e detatches the event handler\n\nsubscription.replay()\n// =\u003e calls the handler for all events currently in uv.events\n```\n\n\n### Once\n\n`uv.once(type, handler, [context])`\n\nAttaches 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.\n\n\n```js\nuv.once('ecProduct', function (data) {\n  console.log(data)\n})\nemit('ecProduct')\n// =\u003e logs data\n\nemit('ecProduct')\n// =\u003e does not log\n```\n\n### Events\n\nThe 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.\n\n### NPM module\n\nThe uv-api is available on the NPM registry.\n\n```bash\nnpm i uv-api --save\n```\n\nThe module exports a createUv function if required using commonjs.\n\n```js\nvar createUv = require('uv-api')\nvar uv = createUv()\nuv.emit('ecView')\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqubitproducts%2Fuv-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fqubitproducts%2Fuv-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqubitproducts%2Fuv-api/lists"}