{"id":15542910,"url":"https://github.com/jelhan/ember-local-storage-decorator","last_synced_at":"2025-05-08T04:25:36.677Z","repository":{"id":40309467,"uuid":"323678858","full_name":"jelhan/ember-local-storage-decorator","owner":"jelhan","description":"Decorator for Ember.js to read and persist data in localStorage","archived":false,"fork":false,"pushed_at":"2025-04-01T11:27:19.000Z","size":599,"stargazers_count":23,"open_issues_count":17,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-28T21:58:14.811Z","etag":null,"topics":["ember","ember-addon","hacktoberfest"],"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/jelhan.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","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-12-22T16:32:22.000Z","updated_at":"2024-12-17T20:13:54.000Z","dependencies_parsed_at":"2023-01-24T00:45:45.246Z","dependency_job_id":"80717eb8-9d10-45d4-950a-f614a5154a32","html_url":"https://github.com/jelhan/ember-local-storage-decorator","commit_stats":{"total_commits":58,"total_committers":7,"mean_commits":8.285714285714286,"dds":0.4137931034482759,"last_synced_commit":"abe304b1bc7dfd23c8d39764c436bb72adfe27cb"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jelhan%2Fember-local-storage-decorator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jelhan%2Fember-local-storage-decorator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jelhan%2Fember-local-storage-decorator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jelhan%2Fember-local-storage-decorator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jelhan","download_url":"https://codeload.github.com/jelhan/ember-local-storage-decorator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252998305,"owners_count":21837989,"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":["ember","ember-addon","hacktoberfest"],"created_at":"2024-10-02T12:24:52.483Z","updated_at":"2025-05-08T04:25:36.648Z","avatar_url":"https://github.com/jelhan.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Ember Local Storage Decorator\n==============================================================================\n\nDecorator to use `localStorage` in Ember Octane.\n\n\nCompatibility\n------------------------------------------------------------------------------\n\n* Ember.js v3.28 or above\n* Ember CLI v3.28 or above\n* Node.js v14 or above\n\n\nInstallation\n------------------------------------------------------------------------------\n\n```\nember install ember-local-storage-decorator\n```\n\n\nUsage\n------------------------------------------------------------------------------\n\n```js\nimport localStorage from 'ember-local-storage-decorator';\nimport Component from '@glimmer/component';\n\nexport default class MyComponent extends Component {\n  @localStorage foo\n}\n```\n\nDecorate a class property with `@localStorage` to bind it to `localStorage`.\nIt will attach a getter to read the value from `localStorage` and a setter\nto write changes to `localStorage`.\n\n```js\nconst Klass = class {\n  @localStorage foo;\n}\nconst klass = new Klass();\n\nklass.foo = 'baz';\nwindow.localStorage.getItem('foo'); // '\"baz\"'\n```\n\nYou may specify another key to be used in local storage as an argument to the\ndecorator.\n\n```js\nconst Klass = class {\n  @localStorage('bar') foo;\n};\nconst klass = new Klass();\n\nklass.foo = 'baz';\nwindow.localStorage.getItem('bar'); // '\"baz\"'\n```\n\nThe value is stored as a JSON string in `localStorage`. Therefore only values\nwhich can be serialized to JSON are supported.\n\nObjects (and arrays) are deep frozen to avoid leaking state. Getter returns a\nfrozen copy after setting a value.\n\n```js\nwindow.localStorage.setItem('foo', [{ a: 'b' }]);\n\nconst Klass = class {\n  @localStorage foo;\n};\nconst klass = new Klass();\n\nObject.isFrozen(klass.foo); // true\nObject.isFrozen(klass.foo[0]); // true\n\nconst newValue = {};\nklass.foo = newValue;\n\nObject.isFrozen(klass.foo); // true\nObject.isFrozen(newValue); // false\n```\n\nIt observes changes caused by other classes or by other instances:\n\n```js\nconst KlassA = class {\n  @localStorage foo;\n};\nconst KlassB = class {\n  @localStorage foo;\n}\nconst klassA = new KlassA();\nconst klassB = new KlassB();\n\nklassA.foo = 'bar';\nklassB.foo; // 'bar'\n\nwindow.dispatchEvent(\n  new StorageEvent('storage', { key: 'foo', newValue: 'baz', oldValue: 'bar' })\n);\nklassA.foo; // 'baz'\nklassB.foo; // 'baz'\n```\n\nDue to limitations of `localStorage` direct changes of the value bypassing\n`@localStorage` decorator can not be observed. Therefore you _should not_\nmanipulate the `localStorage` directly.\n\n## Testing\n\n`window.localStorage` is a global state, which is shared between test runs.\nThe decorator uses a global cache, which is also shared between instances.\nBoth are not reset automatically between test jobs.\n\nTo avoid leaking state between test jobs it's recommended to clear the cache\nof `@localStorage` decorator before each test. A `clearLocalStorageCache`\nhelper function is exported from `ember-local-storage-decorator` to do so.\n\nAdditionally `window.localStorage` should be either cleared before each test\nrun or mocked.\n\n```js\nimport { module, test } from 'qunit';\nimport { setupRenderingTest } from 'ember-qunit';\nimport { clearLocalStorageCache } from 'ember-local-storage-decorator';\n\nmodule('Integration | Component | my-component', function (hooks) {\n  setupRenderingTest(hooks);\n\n  hooks.beforeEach(function () {\n    clearLocalStorageCache();\n    window.localStorage.clear();\n  });\n});\n```\n\n`@localStorage` decorator performs some initialization work when a property\nis decorated. This includes picking up the current value from local storage\nand adding it to its internal cache. Manual changes to local storage _after_\na property has been decorated are _not_ picked up. As class instances are\noften shared between test jobs, you need to manual reinitialize a local\nstorage key in tests.\n\n```js\nimport { initalizeLocalStorageKey } from 'ember-local-storage-decorator';\n\ntest('some code relying on a value in local storage', function() {\n  window.localStorage.setItem('foo', 'bar');\n  initalizeLocalStorageKey('foo');\n});\n```\n\nContributing\n------------------------------------------------------------------------------\n\nSee the [Contributing](CONTRIBUTING.md) guide for details.\n\n\nLicense\n------------------------------------------------------------------------------\n\nThis project is licensed under the [MIT License](LICENSE.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjelhan%2Fember-local-storage-decorator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjelhan%2Fember-local-storage-decorator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjelhan%2Fember-local-storage-decorator/lists"}