{"id":16773830,"url":"https://github.com/ziflex/observable-mixin","last_synced_at":"2025-03-16T17:21:28.931Z","repository":{"id":57313082,"uuid":"78560905","full_name":"ziflex/observable-mixin","owner":"ziflex","description":"Makes your types observable","archived":false,"fork":false,"pushed_at":"2017-01-10T18:58:49.000Z","size":29,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-24T06:19:20.790Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/ziflex.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-01-10T18:19:41.000Z","updated_at":"2017-01-10T18:54:25.000Z","dependencies_parsed_at":"2022-09-20T23:22:08.254Z","dependency_job_id":null,"html_url":"https://github.com/ziflex/observable-mixin","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/ziflex%2Fobservable-mixin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziflex%2Fobservable-mixin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziflex%2Fobservable-mixin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziflex%2Fobservable-mixin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ziflex","download_url":"https://codeload.github.com/ziflex/observable-mixin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243903166,"owners_count":20366434,"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":[],"created_at":"2024-10-13T06:47:09.128Z","updated_at":"2025-03-16T17:21:28.907Z","avatar_url":"https://github.com/ziflex.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# observable-mixin\n\n\u003e Just observe\n\nMixin that makes your types observable without exposing emitter.\n\n[![npm version](https://badge.fury.io/js/observable-mixin.svg)](https://www.npmjs.com/package/observable-mixin)\n[![Build Status](https://secure.travis-ci.org/ziflex/observable-mixin.svg?branch=master)](http://travis-ci.org/ziflex/observable-mixin)\n[![Coverage Status](https://coveralls.io/repos/github/ziflex/observable-mixin/badge.svg?branch=master)](https://coveralls.io/github/ziflex/observable-mixin)\n\n````sh\n    npm install --save observable-mixin\n````\n\n## Usage\n\n``observable-mixin`` brings 1 method:\n- ``subscribe(eventName: String, handler: Function, [once: Boolean]): Function`` returns a function that unsubscribes a given listener\n\nThe mixin comes as a factory function i.e. in order to get mixin it needs to invoke exported function.\nIt is done intentionally in order to be able to pass any kind of implementation of EventEmitter.\n\n````javascript\n\n    import Symbol from 'es6-symbol';\n    import composeClass from 'compose-class';\n    import ObservableMixin from 'observable-mixin';\n    import { EventEmitter } from 'events';\n\n    const FIELDS = {\n        emitter: Symbol('emitter'),\n        name: Symbol('name')\n    };\n\n    const Person = composeClass({\n        mixins: [\n            ObservableMixin(FIELDS.emitter)\n        ],\n\n        constructor(name) {\n            this[FIELDS.emitter] = new EventEmitter();\n            this[FIELDS.name] = name;\n        },\n\n        name(value) {\n            if (value) {\n                this[FIELDS.name] = value;\n                this[FIELDS.emitter].emit('change', 'name', value);\n            }\n\n            return this[FIELDS.name];\n        }\n    });\n\n````\n\n````javascript\nimport Person from './person';\n\nconst person = new Person('Mike Wazowski');\n\nconst unsubscribe = person.subscribe('change', (field, value) =\u003e {\n    console.log('Person\\'s', field, 'was changed to', value);\n});\n\nperson.name('James P. Sullivan'); // Person's name was changed to 'James P. Sullivan'\n\nunsubscribe();\n\nperson.name('Randall Boggs'); // Nothing\n\n````\n\nMixin supports ``once`` subscription:\n\n````javascript\nimport Person from './person';\n\nconst person = new Person('Mike Wazowski');\n\nperson.subscribe('change', (field, value) =\u003e {\n    console.log('Person\\'s', field, 'was changed to', value);\n}, true);\n\nperson.name('James P. Sullivan'); // Person's name was changed to 'James P. Sullivan'\n\nperson.name('Randall Boggs'); // Nothing\n\n````\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fziflex%2Fobservable-mixin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fziflex%2Fobservable-mixin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fziflex%2Fobservable-mixin/lists"}