{"id":21615833,"url":"https://github.com/valango/keep-tidy","last_synced_at":"2025-03-18T17:19:11.214Z","repository":{"id":87587087,"uuid":"301803069","full_name":"valango/keep-tidy","owner":"valango","description":"A tiny ES5 base class and helpers for maintainable code and avoiding memory leaks.","archived":false,"fork":false,"pushed_at":"2020-10-08T11:14:55.000Z","size":90,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"develop","last_synced_at":"2024-04-26T10:03:16.545Z","etag":null,"topics":["avoid","baseclass","debugging","memory-leak","resource"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/valango.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-10-06T17:23:08.000Z","updated_at":"2020-10-08T11:14:23.000Z","dependencies_parsed_at":"2023-03-13T18:43:46.927Z","dependency_job_id":null,"html_url":"https://github.com/valango/keep-tidy","commit_stats":{"total_commits":10,"total_committers":1,"mean_commits":10.0,"dds":0.0,"last_synced_commit":"78a64cb575d81782345add5acc1f88f14b551678"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valango%2Fkeep-tidy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valango%2Fkeep-tidy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valango%2Fkeep-tidy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valango%2Fkeep-tidy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/valango","download_url":"https://codeload.github.com/valango/keep-tidy/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244267475,"owners_count":20425835,"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":["avoid","baseclass","debugging","memory-leak","resource"],"created_at":"2024-11-24T22:12:57.232Z","updated_at":"2025-03-18T17:19:11.187Z","avatar_url":"https://github.com/valango.png","language":"JavaScript","readme":"# keep-tidy\n[![Build Status](https://travis-ci.org/valango/keep-tidy.svg?branch=master)](https://travis-ci.org/valango/keep-tidy)\n[![Code coverage](https://img.shields.io/codecov/c/gh/valango/keep-tidy?label=codecov\u0026logo=codecov)](https://codecov.io/gh/valango/keep-tidy)\n\nA tiny ES5 base class featuring clean disposal of bound resources.\nIt also provides a simple API for easier debugging and diagnostics.\nAll this works in both back-end and front-end environments.\n\nProcessing DOM events or similar in OO code implies correct set-up and releasing of event handlers.\nAlso, cross-referenced class instances and other objects need special care to prevent\nthe dreaded memory leaks. The _**`TidyOwner`**_ base class interface of\n_**`own`**_ property and _**`dispose()`**_ method may save you from writing boring boilerplate code\nand possibly making mistakes in those cases.\n\n## Installation\n`  npm install -S keep-tidy`\u003cbr /\u003eor\u003cbr /\u003e`  yarn add keep-tidy`\n\n## Usage\n```javascript\n  import Tidy from 'keep-tidy'\n\n  class A extends Tidy {\n    resizeHandler() { \n      this.debug('resized')           //  console output like: 'A#2 resized +0ms 3'\n    }\n  }\n\n  let superItem = new A() \n  //  Nested structures are OK.\n  superItem.own.item1 = new A().ownOn('resize', 'resizeHandler', window).debugOn(true)\n  superItem.own.somethingElse = [1, 2, 3]\n  //  Lots of interecting things in between...\n  superItem.dispose()                             //  Tidy up everything now...\n  superItem = undefined                           //  ... and yes, this is no C++ ;)\n```\n\nThere is also lightweight debugging and assertion helpers available even if you won't\nuse Javascript classes at all.\n\n## API\nThe package exports the following API:\n\n* **`assert`** - [assertion package](https://github.com/valango/assert-fine) used internally;\n* [**`debugMe`**](#debugging) - a debug function factory; \n* [**`debug`**](#debugging) - underlying [debug package](https://github.com/visionmedia/debug);\n* [**`TidyOwner`**](#baseclass) - the base class constructor, also available as default export;\n\nSome parts can be loaded individually: `'owner-class/debug'` and `'owner-class/helpers'`.\n\n### Baseclass\n`constructor TidyOwner([classNameOverride : string])`\n\nWhen using this baseclass, you'll avoid writing boilerplate code for releasing event handlers\nand freeing other resources. Its interface consists of:\n   * **`assertHook`**\u003cbr /\u003ea class (not instance) method - a convenience shortcut for \n   [`assert.hook()`](https://github.com/valango/assert-fine).\n   * **`debug`**`(...)`\u003cbr /\u003esee [debugging](#debugging) for details.\n   * **`debugOn`**`([*]): *`\u003cbr /\u003efor re-generating the `debug` method and returning _`this`_\n   for chaining, if argument supplied; otherwise returns _boolean_ showing if debugging is enabled.\n   * **`dispose`**`()`\u003cbr /\u003ecall this to free up all bound resources.\n   Base class method cleans the _**`own`**_ container, firing _`dispose`_ method of every\n   object instance having it. Then it _un-registers all handlers_ set by _`ownOn`_ method.\n   * **`ownOff`**`(event= : string, emitter= : Object) : this`\u003cbr /\u003e\n   un-registers handlers registered for matching (event, emitter) pairs.\n   It is called by dispose(), so in most cases you don't need to call it explicitly.\n   * **`ownOn`**`(event : string, handler, emitter, api=) : this`\u003cbr /\u003e\n   registers _`event`_ _`handler`_ with _`emitter`_ object.\n   If emitter API differs from `addEventListener/removeEventListener` or `$on/$off` or `on/off`,\n   then you need explicitly define the API, like `['listenTo', 'ignore']`.\n   The _`handler`_ parameter can be instance method name or a function.\n   * **`own`**`: Object`\u003cbr /\u003ea keyed container for private data, that should be gracefully cleaned up.\n   * **`ownClass`**`: string`\u003cbr /\u003eclass name (read-only).\n   * **`ownId`**`: number`\u003cbr /\u003eglobally unique class instance number(read-only).\n   * **`ownTag`**`: string`\u003cbr /\u003eis set to _`ownClass`_`+ '#' +`_`ownId`_ (read-only).\n   \nAssigning a value to any instance property will throw `TypeError`.\n\n### Debugging\nDebugging machinery uses [debug](https://github.com/visionmedia/debug])\nNPM package and is available only in development mode. In _**production mode**_ all its API\nmethods will be just empty functions.\n\n**`debugMe`**`( tag: string, [yesNo : boolean]) : {function(...)}`\u003cbr /\u003e\nexported from the main package is factory function returning the actual debug function.\n\nThe same factory function is default export from the sub-package (see example below).\n\n**Debugging plain javascript code:**\n```javascript\n  const D = require('keep-tidy/debug')\n  const myDebug = D(main.js, true), { debug } = D\n\n  myDebug('try(%d, %o)', 1, 2, 3)       // --\u003e 'main.js try(1, 2) +0ms 3'\n  debug.enable('*')\n  debug('natively')('yay!')             // --\u003e 'natively yay! +0ms'\n```\n\n## Compatibility\n\nThe package should be ECMAScript 2015 and it should run virtually anywhere.\nIf you find any issues, please do not hesitate to report.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvalango%2Fkeep-tidy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvalango%2Fkeep-tidy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvalango%2Fkeep-tidy/lists"}