{"id":16253675,"url":"https://github.com/develephant/propsaware","last_synced_at":"2025-04-08T12:46:32.469Z","repository":{"id":57107854,"uuid":"84800549","full_name":"develephant/PropsAware","owner":"develephant","description":"Properties as dispatchers. A \"living\" global properties data store.","archived":false,"fork":false,"pushed_at":"2017-03-15T05:19:54.000Z","size":62,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-06T12:06:25.748Z","etag":null,"topics":["data-store","dispatcher","events","globals","nodejs","property-store","property-watcher","pub-sub"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/develephant.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-13T08:14:51.000Z","updated_at":"2017-03-15T05:06:41.000Z","dependencies_parsed_at":"2022-08-20T17:11:11.870Z","dependency_job_id":null,"html_url":"https://github.com/develephant/PropsAware","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/develephant%2FPropsAware","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/develephant%2FPropsAware/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/develephant%2FPropsAware/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/develephant%2FPropsAware/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/develephant","download_url":"https://codeload.github.com/develephant/PropsAware/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247846628,"owners_count":21006078,"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":["data-store","dispatcher","events","globals","nodejs","property-store","property-watcher","pub-sub"],"created_at":"2024-10-10T15:18:12.612Z","updated_at":"2025-04-08T12:46:32.446Z","avatar_url":"https://github.com/develephant.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `PropsAware`\n\n### Properties as dispatchers. A \"living\" global properties data store.\n\n![props-aware](http://develephant.com/projects/pa.png)\n\n## Install\n\n```\nnpm i @develephant/props-aware --save\n```\n\n## Usage\n\n```js\nconst PropsAware = require('@develephant/props-aware')\n```\n\n# Overview\n\n__PropsAware__ (or `PA`) is a global property object that emits events on property changes.\n\nYou can listen for these changes virtually anywhere in your program. \n\n_Events are only emitted when the underlying property data changes._\n\nIn code it looks like this:\n\n```js\n//file-one.js\n\n/* Pull in PropsAware as PA */\nconst PA = require('@develephant/props-aware')\n\nlet props = PA.props()\n\nPA.on('score', (val) =\u003e {\n  console.log(val)\n})\n\nprops.score = 300\n\n/* outputs 300 */\n\n``` \n\nLets create another JS file, and add the following:\n\n```js\n//file-two.js\n\n/* Pull in PropsAware as PA */\nconst PA = require('@develephant/props-aware')\n\nprops = PA.props()\n\nprops.score = 500 //will trigger an update\n\n/* file-one.js will output 500 */\n\nprops.score = 500 //will not trigger an update, same data\n\n```\n\n# Discussion\n\nSome things to consider before, or when using __PropsAware__:\n\n  - Only properties are managed; strings, numbers, booleans, arrays, and \"data\" objects.\n  - Properties are stored as a flat tree. Only root keys trigger update events. [1]\n  - When you set a `PA` property, its immediately available to all listeners.\n  - Callbacks are set on the `PropsAware` (`PA`) object directly, _not_ the property itself. [2]\n  - To change a property without emitting an update event, use `PA.set(p, v)`.\n  - Limit yourself to a handful of base `PA` properties, and pass primitives for flow control.\n  - There are no guarantees on delivery order or timing. it will get there though.\n  - When creating object instances with `PA` properties, each instance will have its own listener. [3]\n  - Dont set `PA` properties in an `onAll` handler. [4]\n\n### Footnotes\n\n#### 1) Only root keys trigger an update:\n\n```js\n//example PA props object\n...\nlet props = {\n  score: 200\n  user: {\n    name: 'User',\n    color: 'green'\n  }\n}\n```\n\nIn the object above, when the `score` property is updated with a _new_ value, an event will be emitted.\n\nThe `user` key holds an object. When the key is set with an updated _object_, the `user` key will trigger:\n\n```js\n//only root keys emit\nPA.on('user', (val) =\u003e {\n  console.log(val.color) //blue\n})\n\nprops.user = { color: 'blue' }\n\n```\n\nChanging `user.color` directly will not trigger an event. Additionally, in the code above, the `name` key will be stripped away. This may be fine if thats whats intended.\n\nAn easy way to handle this, is pulling the object first:\n\n```js\nlet obj = props.user //by ref\nobj.color = 'yellow'\nprops.user = obj //will trigger\n\n//yellow\n```\n\nOr, if you want to be super fancy...\n\n```js\nlet obj = Object.assign({}, props.user) //cloned\nobj.color = 'yellow'\nprops.user = obj //will trigger\n```\n\n\u003e ___The above holds true for Arrays too.___\n\n#### 2) Callbacks are set on the `PA` object, _not_ the property:\n\n```js\n...\n//works!\nPA.on('score', (val) =\u003e {\n  console.log(val)\n})\n\n//does NOT work\nscore.on((val) =\u003e {\n  console.log(val)\n}\n\n```\n\n#### 3) Created instances of a Class will all emit:\n\n```js\nclass MyClass {\n  constructor() {\n    this.props = PA.props()\n    PA.on('score', (val) =\u003e {\n      console.log(val)\n    })\n  }\n}\n\nconst instance1 = new MyClass()\nconst instance2 = new MyClass()\nconst instance3 = new MyClass()\n\ninstance1.props.score = 100\n\n/* instance1 outputs 100 */\n/* instance2 outputs 100 */\n/* instance3 outputs 100 */\n```\n\n#### 4) Dont set properties in an `onAll` handler:\n\n```js\nlet props = PA.props()\nPA.onAll((val, prop) =\u003e { //infinite loop\n  props[prop] = val //dont do it!\n  props.dontsetme = 'oops' //think of the puppies!!\n})\n```\n\nIf you _really, really_ need/want to set a `PA` property in an `onAll` handler you can either:\n\n___Set it silently, without triggering an update___\n\n```js\nlet props = PA.props()\nPA.onAll((val, prop) =\u003e {\n  PA.set(prop, val) //does not emit\n})\n```\n\nOr, you can use this workaround/hack:\n\n```js\nlet props = PA.props()\nPA.onAll((val, prop) =\u003e {\n  setTimeout(() =\u003e { props[prop] = val }, 1)\n  //will run until your computer starts smoking\n})\n```\n\nBut, at the end of the day, thats an infinite loop. Its not advisable.\n\n# Keep it simple\n\nTry to keep the amount of __PropsAware__ properties to a minimum. Pass strings, numbers, and booleans to handle messaging and state.\n\nConsider the following:\n\n```js\nconst PA = require('@develephant/props-aware')\nlet props = PA.props()\n\n//this is okay...\nprops.walking = true\nprops.running = false\n\n//but this is better!\nprops.pace = 'walking'\n//OR\nprops.pace = 'running'\n\n```\n\n\u003e _In most basic programs, you shouldnt need more than 4-5 `PA` properties._\n\n# API\n\n## `props() -\u003e PropsAware_properties`\n\nRetreives the __PropsAware__ property object. All properties on this object emit when set.\n\n```js\nconst PA = require('@develephant/props-aware')\n\nlet props = PA.props()\n```\n\n## `on(prop, cb)`\n\n__Callback receives:__\n\n|Name|Purpose|\n|----|-------|\n|`val`|The property value|\n|`prop`|The name of the property|\n\nListen for a property update.\n\n```js\nconst PA = require('@develephant/props-aware')\n\nPA.on('score', (val, prop) =\u003e {\n  console.log(val)\n})\n```\n\n\n## `has(prop) -\u003e success_bool`\n\nChecks to see if a specific __root__ property is housed in the __PropsAware__ property table.\n\n```js\nconst PA = require('@develephant/props-aware')\n\nlet has_username = PA.has('username')\n```\n\n## `del(prop) -\u003e success_bool`\n\nRemoves a properties update listeners. This destroys __all__ update listeners for the property, except the global `onAll`.\n\n```js\nlet success = PA.del('score')\n```\n\n## `onAll(cb)`\n\n__Callback receives:__\n\n|Name|Purpose|\n|----|-------|\n|`val`|The property value|\n|`prop`|The name of the property|\n\nAny part of the program can listen for __all__ `PA` property changes. When using this method, you need to filter the messages with control statements.\n\n```js\nPA.onAll((val, prop) =\u003e {\n  console.log('changed', prop, val)\n})\n```\n\n_Property based flow control:_\n\n```js\nPA.onAll((val, prop) =\u003e {\n  if (prop === 'goback') {\n    console.log('go back')\n  } else if (prop === 'goforward') {\n    console.log('go forward')\n  }\n})\n\n//...somewhere else\n\n/* PA Props reference */\nlet props = PA.props()\nprops.goback = true\n//or\nprops.goforward = true\n\n```\n\n_State based flow control (using `on`):_\n\n```js\nPA.on('state', (val) =\u003e {\n  if (val === 'goback') {\n    console.log('go back')\n  } else if (val === 'goforward') {\n    console.log('go forward')\n  }\n})\n\n//...somewhere else\n\n/* PA Props reference */\nlet props = PA.props()\nprops.state = 'goback'\n//...or\nprops.state = 'goforward'\n```\n\n\u003e _State based flow control is generally the better choice._ \n\u003e \n\u003e With the pattern above, its entirely possible to manipulate the bulk of your program with one `PA` property!\n\n## `onDel(cb)`\n\n__Callback receives:__\n\n|Name|Purpose|\n|----|-------|\n|`prop`|The name of the property|\n\nFired when the `del` method is used in the __PropsAware__ object\n\n```js\nPA.onDel((prop) =\u003e {\n  console.log('del', prop)\n})\n```\n\n## `sync() --\u003e success_bool`\n\nDispatch all properties, to all listeners. Should not be used often, especially with large property objects.\n\n\u003e _Note:_ Any `onAll` listeners will be called with __all__ emitted properties.\n\n```js\nconst PA = require('@develephant/props-aware')\n\n//Set a property \"silently\"\nPA.set('prop', 'val')\n\n//Nevermind, resend everything\nPA.sync()\n```\n\n# Examples\n\n### In Classes\n\n```js\n//ClassA.js\n\nconst PA = require('@develephant/props-aware')\n\nclass ClassA {\n  constructor() {\n    this.props = PA.props()\n\n    PA.on('score', (val) =\u003e {\n      console.log(val)\n    })\n  }\n}\nmodule.exports = ClassA\n```\n\n```js\n//ClassB.js\n\nconst PA = require('@develephant/props-aware')\n\nclass ClassB {\n  constructor() {\n    this.props = PA.props()\n    this.props.score = 100\n  }\n}\nmodule.exports = ClassB\n```\n\n__Run__\n\n```js\n//main.js\n\nconst ClassA = require('./ClassA')\nconst ClassB = require('./ClassB')\n\nconst A = new ClassA()\nconst B = new ClassB() /* Class A will output \"100\" */\n```\n\n### In Objects\n\n```js\n//obj-one.js\n\n/* Pull in PropsAware as PA */\nconst PA = require('@develephant/parse-aware')\n\nconst ObjA = {\n  props: PA.props(),\n  listen() {\n    PA.on('greeting', (val) =\u003e {\n      console.log(greeting)\n    })\n  }\n}\nmodule.exports = ObjA\n```\n\n```js\n//obj-two.js\n\n/* Pull in PropsAware as PA */\nconst PA = require('@develephant/props-aware')\n\nconst ObjB = {\n  props: PA.props()\n}\nmodule.exports = ObjB\n```\n\n__Run__\n\n```js\n//main.js\n\nconst objA = require('./obj-one')\nconst objB = require('./obj-two')\n\nobjA.listen()\n\nobjA.props.greeting = 'Hola' /* ObjA will output \"Hola\" */\n\n//Props object is \"global\" so this works via objB:\n\nobjB.props.greeting = 'Hello' /* ObjA will output \"Hello\" */\n\n```\n\n### A Function\n\n```js\nconst PA = require('@develephant/props-aware')\n\nmodules.export = function() {\n  this.props = PA.props()\n  this.props.greeting = 'Howdy'\n  PA.on('score', (val) =\u003e {\n    console.log(val)\n  })\n}\n\n// Assuming the Objects above...\n\n/* ObjA will output 'Howdy' */\n\n// Call score through Class B above\nclassB.props.score = 1000\n\n/* This Function will output 1000 *\\\n/* ObjA will output 1000 */\n\n```\n\n# Next Steps\n\n - Immutability\n\n^_^\n\n---\n\n#### `PropsAware` \u0026Star; \u0026copy; 2017 develephant \u0026Star; Apache-2.0 license\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevelephant%2Fpropsaware","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevelephant%2Fpropsaware","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevelephant%2Fpropsaware/lists"}