├── .gitignore ├── CHANGELOG.md ├── LICENSE.txt ├── README.md ├── lib └── persistent_session.js ├── package.js ├── tests └── client │ └── persistent_session.js └── versions.json /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .env 3 | .build* 4 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ### Master [changes](https://github.com/okgrow/meteor-persistent-session/compare/v0.4.4...master) 2 | 3 | ### [0.4.4](https://github.com/okgrow/meteor-persistent-session/compare/v0.4.3...v0.4.4) 4 | 5 | * Fix #32 - setDefaultPersistent with an object doesn't persist data (Reported by [rslonik](https://github.com/rslonik) 6 | 7 | ### [0.4.3](https://github.com/okgrow/meteor-persistent-session/compare/v0.4.2...v0.4.3) 8 | 9 | * Fix #48 - console.log messages (Reported by [themeteorchef](https://github.com/themeteorchef) 10 | 11 | ### [0.4.2](https://github.com/okgrow/meteor-persistent-session/compare/v0.4.1...v0.4.2) 12 | 13 | * Fix `Session.clear` issues, #42/#44 [PR #46](https://github.com/okgrow/meteor-persistent-session/pull/46) by [mhinton](https://github.com/mhinton) 14 | * Fix #37 - Values turned into strings (Reported by [lorensr](https://github.com/lorensr)) 15 | * Fix #45 - 0.3.5 and 0.4.\* are incompatible (Reported by [freiit](https://github.com/freiit)) 16 | 17 | ### [0.4.1](https://github.com/okgrow/meteor-persistent-session/compare/v0.4.0...v0.4.1) 18 | 19 | * Fix reference error in `PersistentSession.equals` [PR #39](https://github.com/okgrow/meteor-persistent-session/pull/39) by [ryepdx](https://github.com/ryepdx) 20 | * Changed quotes to valid JSON [PR #40](https://github.com/okgrow/meteor-persistent-session/pull/40) by [aramk](https://github.com/aramk) 21 | 22 | ### [0.4.0](https://github.com/okgrow/meteor-persistent-session/compare/v0.3.4...v0.4.0) 23 | 24 | * Revert 6e4b58d 25 | * Add a weak dependency on `accounts-base`, fixes #9 (Reported by [aadamsx](https://github.com/aadamsx) and [afoda](https://github.com/afoda)) 26 | * Added tests [PR #33](https://github.com/okgrow/meteor-persistent-session/pull/33) by [RobertLowe](https://github.com/RobertLowe) 27 | * Use `Tracker.autorun` to detect logout, rather than monkey-patching `Meteor.logout` [PR #33](https://github.com/okgrow/meteor-persistent-session/pull/33) by [RobertLowe](https://github.com/RobertLowe) 28 | * Permit namespacing and instantiation of custom PersistentSessions [PR #33](https://github.com/okgrow/meteor-persistent-session/pull/33) by [RobertLowe](https://github.com/RobertLowe) 29 | * Add support for `ReactiveDict`'s `all` and `equals` [PR #33](https://github.com/okgrow/meteor-persistent-session/pull/33) by [RobertLowe](https://github.com/RobertLowe) 30 | * Do not stringify values (amplify already does), fixes #31 [PR #35](https://github.com/okgrow/meteor-persistent-session/pull/35) by [mike182uk](https://github.com/mike182uk) 31 | 32 | ### [0.3.4](https://github.com/okgrow/meteor-persistent-session/compare/v0.3.3...v0.3.4) 33 | 34 | * Don't try to parse values that weren't explicitly set through Session, fixes #24 (Reported by [tcastelli](https://github.com/tcastelli)) 35 | 36 | ### [0.3.3](https://github.com/okgrow/meteor-persistent-session/compare/v0.3.2...v0.3.3) 37 | 38 | * Support `Session.set` taking an object parameter [PR #25](https://github.com/okgrow/meteor-persistent-session/pull/25) 39 | 40 | ### [0.3.2](https://github.com/okgrow/meteor-persistent-session/compare/v0.3.1...v0.3.2) 41 | 42 | * Add dependance on ejson package, fixes #22 [PR #23](https://github.com/okgrow/meteor-persistent-session/pull/23) by [djhi](https://github.com/djhi) 43 | 44 | ### [0.3.1](https://github.com/okgrow/meteor-persistent-session/compare/v0.3.0...v0.3.1) 45 | 46 | * Fix issue with trying to evaluate unparsed EJSON value 47 | 48 | ### [0.3.0](https://github.com/okgrow/meteor-persistent-session/compare/v0.2.2...v0.3.0) 49 | 50 | * Changed `default_method` to "temporary", fixes #15 ([pull request #16](https://github.com/okgrow/meteor-persistent-session/pull/16) by [aramk](https://github.com/aramk)) 51 | * Store values as EJSON, fixes #17 (Reported by [jamesgibson14](https://github.com/jamesgibson14)) 52 | 53 | ### [0.2.2](https://github.com/okgrow/meteor-persistent-session/compare/v0.2.1...v0.2.2) 54 | 55 | * Handle case where a persistent value is set to 0 (Reported by [IndigoStarfish](https://github.com/IndigoStarfish)) 56 | 57 | ### [0.2.1](https://github.com/okgrow/meteor-persistent-session/compare/v0.2.0...v0.2.1) 58 | 59 | * Fix typos 60 | 61 | ### [0.2.0](https://github.com/okgrow/meteor-persistent-session/compare/v0.1.4...v0.2.0) 62 | 63 | * Complete Overhaul 64 | * Convert package format to 0.9.0 Unipackage 65 | 66 | ### [0.1.4](https://github.com/okgrow/meteor-persistent-session/releases/tag/v0.1.4) 67 | 68 | * Fixed `Session.getDefault` (Reported by [valZho](https://github.com/valZho)) 69 | * Fixed `Session.equals` (Reported by [manuelpaulo](https://github.com/manuelpaulo)) 70 | * Fixed configuration example in README.md 71 | 72 | ### [0.1.3](https://github.com/okgrow/meteor-persistent-session/releases/tag/v0.1.3) 73 | 74 | * Initial release 75 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | ======================================== 2 | Persistent Session is licensed under the MIT License 3 | ======================================== 4 | 5 | Copyright (C) 2018 Cult of Coders 6 | Copyright (C) 2014-2015 OK GROW! 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 11 | 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This project is supported by Cult of Coders 2 | ======= 3 | 4 | Purpose 5 | ======= 6 | Make Meteor's `Session` object persist its values locally and across page 7 | refreshes. Meteor's default implementation loses values whenever the page is 8 | refreshed. 9 | 10 | Uses [amplifyjs's store](http://amplifyjs.com/api/store/) library to save 11 | values in the browsers `localStorage`, falling back to other solutions if it's 12 | not available. 13 | 14 | Upgrading from 0.2.x to 0.3.x 15 | ============================= 16 | 17 | The default behaviour of `Session.set` has been changed. `default_method` now 18 | defaults to `temporary` (as was mentioned in the docs), rather than 19 | `persistent`, which was what it was set to in the code. 20 | 21 | This means that to keep the behaviour the same, you should set `default_method` 22 | to `persistent`: 23 | 24 | `config/settings.json` file: 25 | ```json 26 | { 27 | "public": { 28 | "persistent_session": { 29 | "default_method": "persistent" 30 | } 31 | } 32 | } 33 | ``` 34 | 35 | Installation 36 | ============ 37 | ``` 38 | meteor add cultofcoders:persistent-session 39 | ``` 40 | **Note:** To use persistent-session, your project must have Session already installed. You can add Meteor's Session package by `meteor add session`. 41 | 42 | That's it! Now you can use `Session.setPersistent` to set a session variable 43 | that will save after a refresh. 44 | 45 | If you'd like, you can have `Session.set` do this as well. See the Options 46 | section below. 47 | 48 | For Meteor 1.3+ 49 | --------------- 50 | 51 | If your app is using the imports syntax, the persistent-session package will work by simply importing Session where it is used. e.g - `import { Session } from 'meteor/session'` 52 | 53 | Types 54 | ===== 55 | 56 | 1. Temporary Session Variable 57 | * matches current Meteor implementation 58 | * are not available after a page reload 59 | 60 | 2. Persistent Session Variable 61 | * content is stored in the localstorage until it is cleared 62 | 63 | 3. Authenticated Session Variable 64 | * content is stored in the localstorage AND is cleared when a user logs out 65 | 66 | Usage 67 | ===== 68 | 69 | Setting Session Values 70 | ---------------------- 71 | 72 | * `Session.set(key, value)` 73 | * stores a session var according to the default_method (see Options) 74 | * `Session.setTemp(key, value)` 75 | * stores a temporary session variable (non-persistent) 76 | * `Session.setPersistent(key, value)` 77 | * store a persistent session variable (persistent) 78 | * `Session.setAuth(key, value)` 79 | * stores a authenticated session variable (persistent + automatic deletion) 80 | 81 | As of 3.3, you can use an object to set multiple values at once: 82 | 83 | ```javasript 84 | Session.setPersistent({foo: "foo", bar: "bar"}); 85 | ``` 86 | 87 | This works with all of the `set*` methods. All key/values set as an object 88 | will have the same type of scoping (persistent/auth/temporary). 89 | 90 | Updating Session Values 91 | ----------------------- 92 | 93 | You can update the value of an existing session variable without changing or knowing its type. 94 | Note: If you call update on an non-existent variable, it will be created as a temporary variable. 95 | 96 | * `Session.update(key, value)` 97 | 98 | Set Default 99 | ----------- 100 | 101 | All of the `set()` functions have a `setDefault()` counterpart where the session variable will only be created if one doesn't already exist. 102 | Note: None of the `setDefault()` commands will change the type of an existing session variable. 103 | 104 | * `Session.setDefault(key, value)` 105 | * `Session.setDefaultTemp(key, value)` 106 | * `Session.setDefaultPersistent(key, value)` 107 | * `Session.setDefaultAuth(key, value)` 108 | 109 | Change Types 110 | ------------ 111 | 112 | Use these commands to change a session variable into a particular type. 113 | 114 | * `Session.makeTemp(key)` 115 | * `Session.makePersistent(key)` 116 | * `Session.makeAuth(key)` 117 | 118 | Clear Values 119 | ------------ 120 | 121 | * `Session.clear()` 122 | * destroys all session variables of all types 123 | * `Session.clear(key)` 124 | * destroys a single session variable 125 | * `Session.clearTemp()` 126 | * destroys all temporary session variables 127 | * `Session.clearPersistent()` 128 | * destroys all persistent session variables 129 | * `Session.clearAuth()` 130 | * destroys all authenticated session variables 131 | 132 | Other 133 | ----- 134 | 135 | These work the same as the current Meteor implementation: 136 | 137 | * `Session.get(key)` 138 | * `Session.equals(key, value)` 139 | 140 | `ReactiveDict`'s `all` method is also supported. 141 | 142 | Options 143 | ======= 144 | 145 | To define the default type for session variables, set `persistent_session.default_method` to your preferred type in your 146 | `config/settings.json` file: 147 | 148 | ```json 149 | { 150 | "public": { 151 | "persistent_session": { 152 | "default_method": "your-preferred-type" 153 | } 154 | } 155 | } 156 | ``` 157 | 158 | `persistent_session.default_method` can take one of the following values: 159 | * `persistent` 160 | * `authenticated` 161 | 162 | In any other case the `default_method` will fall back to `temporary` 163 | 164 | Original from: https://github.com/okgrow/meteor-persistent-session 165 | -------------------------------------------------------------------------------- /lib/persistent_session.js: -------------------------------------------------------------------------------- 1 | // This file uses code direct from Meteor's reactive-dict package, mostly from 2 | // this file: https://github.com/meteor/meteor/blob/0ef65cc/packages/reactive-dict/reactive-dict.js 3 | // 4 | // helpers: https://github.com/meteor/meteor/blob/0ef65cc/packages/reactive-dict/reactive-dict.js#L1-L16 5 | var stringify = function (value) { 6 | if (value === undefined) 7 | return 'undefined'; 8 | return EJSON.stringify(value); 9 | }; 10 | var parse = function (serialized) { 11 | if (serialized === undefined || serialized === 'undefined') 12 | return undefined; 13 | return EJSON.parse(serialized); 14 | }; 15 | 16 | var changed = function (v) { 17 | v && v.changed(); 18 | }; 19 | 20 | const oldSession = new ReactiveDict('_session'); 21 | 22 | PersistentSession = function (dictName) { 23 | if (_.isString(dictName)) { 24 | this._dictName = dictName; 25 | 26 | // when "session", use the existing dict 27 | if (dictName == "session") { 28 | this._dictName = "" // we don't need a name for session 29 | this._dict = oldSession; // we also want to use the global (incase something was set previously) 30 | 31 | // not session? create a new dict 32 | } else { 33 | this._dict = new ReactiveDict(dictName); 34 | } 35 | 36 | } else { 37 | throw new Error("dictName must be a string"); 38 | } 39 | 40 | 41 | /* 42 | * Used to determine if we need to migrate how the data is stored. 43 | * Each time the data format changes, change this number. 44 | * 45 | * It should match the current major + minor version: 46 | * EG: 0.3 = 3, 1.2 = 12, 2.0 = 20, or for 0.3.x: 3, or 1.x: 10 47 | * 48 | */ 49 | var PSA_DATA_VERSION = 4; 50 | 51 | // === INITIALIZE KEY TRACKING === 52 | this.psKeys = {}; 53 | this.psKeyList = []; 54 | this.psaKeys = {}; 55 | this.psaKeyList = []; 56 | 57 | // initialize default method setting 58 | this.default_method = 'temporary'; // valid options: 'temporary', 'persistent', 'authenticated' 59 | if (Meteor.settings && 60 | Meteor.settings.public && 61 | Meteor.settings.public.persistent_session) { 62 | this.default_method = Meteor.settings.public.persistent_session.default_method; 63 | } 64 | 65 | 66 | var self = this; 67 | 68 | // === HOUSEKEEPING === 69 | /* 70 | * Converts previously stored values into EJSON compatible formats. 71 | */ 72 | function migrateToEJSON() { 73 | if (amplify.store('__PSDATAVERSION__' + self._dictName) >= 1) { 74 | return; 75 | } 76 | 77 | var psKeyList = amplify.store('__PSKEYS__' + self._dictName); 78 | var psaKeyList = amplify.store('__PSAKEYS__' + self._dictName); 79 | 80 | _.each([psKeyList, psaKeyList], function(list) { 81 | _.each(list, function(key) { 82 | amplify.store(key, EJSON.stringify(amplify.store(key))); 83 | }); 84 | }); 85 | 86 | amplify.store('__PSDATAVERSION__' + self._dictName, 2); 87 | }; 88 | 89 | function migrate3Xto4X() { 90 | if (amplify.store('__PSDATAVERSION__' + self._dictName) >= PSA_DATA_VERSION) { 91 | return; 92 | } 93 | 94 | var psKeyList = amplify.store('__PSKEYS__' + self._dictName); 95 | var psaKeyList = amplify.store('__PSAKEYS__' + self._dictName); 96 | 97 | _.each([psKeyList, psaKeyList], function(list) { 98 | _.each(list, function(key) { 99 | var invalid = false; 100 | try { 101 | EJSON.parse(amplify.store(self._dictName+key)); 102 | } catch (error) { 103 | //The data is already in the format that we expect 104 | //Unfortunately there is no EJSON.canParse method 105 | invalid = true; 106 | } 107 | if (!invalid) { 108 | var parsed = EJSON.parse(amplify.store(self._dictName+key)); 109 | var jsoned = EJSON.toJSONValue(parsed); 110 | amplify.store(self._dictName+key, jsoned); 111 | } 112 | }); 113 | }); 114 | 115 | amplify.store('__PSDATAVERSION__' + self._dictName, 4); 116 | } 117 | 118 | if (Meteor.isClient) { 119 | 120 | // --- on startup, load persistent data back into meteor session --- 121 | Meteor.startup(function(){ 122 | var val; 123 | 124 | migrateToEJSON(); 125 | migrate3Xto4X(); 126 | 127 | // persistent data 128 | var psList = amplify.store('__PSKEYS__' + self._dictName); 129 | if ( typeof psList == "object" && psList.length!==undefined ) { 130 | for (var i=0; i= 0 ) { auth = true; } 447 | if ( auth || _.indexOf(this.psKeyList, key) >= 0 ) { persist = true; } 448 | this.set(key, value, persist, auth); 449 | }; 450 | 451 | // === SET DEFAULT === 452 | PersistentSession.prototype.old_setDefault = function (/* arguments */){ 453 | return this._dict.setDefault.apply(this._dict, arguments); 454 | }; 455 | PersistentSession.prototype.setDefault = function _psSetDefault(keyOrObject, value, persist, auth) { 456 | var self = this; 457 | 458 | if (_.isObject(keyOrObject)) { 459 | _.each(keyOrObject, function(value, key) { 460 | self.setDefault(key, value, persist, auth); 461 | }); 462 | return; 463 | } 464 | 465 | if ( this.get(keyOrObject) === undefined) { 466 | this.set(keyOrObject, value, persist, auth); 467 | } 468 | }; 469 | 470 | // === SET DEFAULT TEMP === 471 | PersistentSession.prototype.setDefaultTemp = function _psSetDefaultTemp(keyOrObject, value) { 472 | 473 | if (_.isObject(keyOrObject)) { 474 | value = undefined; 475 | } 476 | 477 | this.setDefault(keyOrObject, value, false, false); 478 | }; 479 | 480 | // === SET DEFAULT PERSISTENT === 481 | PersistentSession.prototype.setDefaultPersistent = function _psSetDefaultPersistent(keyOrObject, value) { 482 | 483 | if (_.isObject(keyOrObject)) { 484 | value = undefined; 485 | } 486 | 487 | this.setDefault(keyOrObject, value, true, false); 488 | }; 489 | 490 | // === SET DEFAULT AUTH === 491 | PersistentSession.prototype.setDefaultAuth = function _psSetDefaultAuth(keyOrObject, value) { 492 | 493 | if (_.isObject(keyOrObject)) { 494 | value = undefined; 495 | } 496 | 497 | this.setDefault(keyOrObject, value, true, true); 498 | }; 499 | 500 | // automatically apply PersistentSession to Session 501 | // var oldSession = _.clone(Session); 502 | 503 | // console.log(Session); 504 | _.extend(Session, new PersistentSession("session")) 505 | -------------------------------------------------------------------------------- /package.js: -------------------------------------------------------------------------------- 1 | Package.describe({ 2 | name: "cultofcoders:persistent-session", 3 | version: "0.4.5", 4 | summary: "Persistently store Session data on the client", 5 | git: "https://github.com/cult-of-coders/meteor-persistent-session" 6 | }); 7 | 8 | Package.onUse(function(api) { 9 | api.versionsFrom('0.9.1'), 10 | api.use(['jquery', 'amplify', 'tracker', 'reactive-dict', 'session', 'underscore', 'ejson']); 11 | // If `accounts-base` is loaded, we have to make sure that this package is 12 | // loaded after `accounts-base` is, so we specify `weak: true` here 13 | api.use('accounts-base', { weak: true }); 14 | api.addFiles('lib/persistent_session.js', 'client'); 15 | api.export('PersistentSession', ['client']); 16 | }); 17 | 18 | Package.onTest(function (api) { 19 | api.use("tinytest"); 20 | api.use("amplify"); 21 | api.use("random"); 22 | api.use("underscore"); 23 | api.use("reactive-dict"); // we only need this exposed for testing 24 | api.use("u2622:persistent-session"); 25 | 26 | // expose for derping around in console 27 | api.export('PersistentSession', ['client']); 28 | api.export('ReactiveDict', ['client']); 29 | 30 | api.addFiles("tests/client/persistent_session.js", "client"); 31 | }); 32 | -------------------------------------------------------------------------------- /tests/client/persistent_session.js: -------------------------------------------------------------------------------- 1 | Tinytest.add("defaults to temporary", function(test) { 2 | var TestSession = new PersistentSession(Random.id()); 3 | 4 | test.equal('temporary', TestSession.default_method); 5 | }); 6 | 7 | // this isnt testing anything yet... 8 | Tinytest.add("alternate mode", function(test) { 9 | var TestSession = new PersistentSession(Random.id()); 10 | 11 | // TODO: This should probably be a reactive var, just for sanity now 12 | TestSession.default_method = 'authenticated'; 13 | test.equal('authenticated', TestSession.default_method); 14 | 15 | // reset to default 16 | TestSession.default_method = 'temporary'; 17 | test.equal('temporary', TestSession.default_method); 18 | }); 19 | 20 | Tinytest.add("clear all keys", function(test) { 21 | var TestSession = new PersistentSession(Random.id()); 22 | 23 | test.equal(_.keys(TestSession._dict.keys).length, 0); 24 | 25 | TestSession.set('foobar', 'woo'); 26 | var result = TestSession.get('foobar'); 27 | test.equal('woo', result); 28 | 29 | test.equal(_.keys(TestSession._dict.keys).length, 1); 30 | 31 | TestSession.clear(); 32 | 33 | test.equal(_.keys(TestSession._dict.keys).length, 0); 34 | 35 | var result = TestSession.get('foobar'); 36 | test.equal(undefined, result); 37 | }); 38 | 39 | Tinytest.add("clear auth keys", function(test) { 40 | var TestSession = new PersistentSession(Random.id()); 41 | test.equal(_.keys(TestSession._dict.keys).length, 0); 42 | 43 | TestSession.setAuth('foobar', 'bork'); 44 | test.equal('bork', TestSession.get('foobar')); 45 | 46 | TestSession.clearAuth(); 47 | 48 | test.equal(undefined, TestSession.get('foobar')); 49 | }); 50 | 51 | Tinytest.add("skip undefined keys", function(test) { 52 | var TestSession = new PersistentSession(Random.id()); 53 | test.equal(_.keys(TestSession._dict.keys).length, 0); 54 | 55 | TestSession.set(undefined, 'woo'); 56 | test.equal(_.keys(TestSession._dict.keys).length, 1); 57 | 58 | TestSession.clear(); 59 | 60 | test.equal(_.keys(TestSession._dict.keys).length, 0); 61 | 62 | }); 63 | 64 | Tinytest.add("clear single key", function(test) { 65 | var TestSession = new PersistentSession(Random.id()); 66 | 67 | test.equal(_.keys(TestSession._dict.keys).length, 0); 68 | 69 | TestSession.set('foobar', 'woo'); 70 | var result = TestSession.get('foobar'); 71 | test.equal('woo', result); 72 | 73 | TestSession.set('barfoo', 'oow'); 74 | var result = TestSession.get('barfoo'); 75 | test.equal('oow', result); 76 | 77 | test.equal(_.keys(TestSession._dict.keys).length, 2); 78 | 79 | TestSession.clear('foobar'); 80 | 81 | test.equal(_.keys(TestSession._dict.keys).length, 1); 82 | 83 | var result = TestSession.get('foobar'); 84 | test.equal(undefined, result); 85 | 86 | var result = TestSession.get('barfoo'); 87 | test.equal('oow', result); 88 | }); 89 | 90 | Tinytest.add("clear multiple keys", function(test) { 91 | var TestSession = new PersistentSession(Random.id()); 92 | 93 | test.equal(_.keys(TestSession._dict.keys).length, 0); 94 | 95 | TestSession.set('foobar', 'woo'); 96 | var result = TestSession.get('foobar'); 97 | test.equal('woo', result); 98 | 99 | TestSession.set('barfoo', 'oow'); 100 | var result = TestSession.get('barfoo'); 101 | test.equal('oow', result); 102 | 103 | test.equal(_.keys(TestSession._dict.keys).length, 2); 104 | 105 | TestSession.clear(undefined, ['foobar', 'barfoo']); 106 | 107 | test.equal(_.keys(TestSession._dict.keys).length, 0); 108 | 109 | var result = TestSession.get('foobar'); 110 | test.equal(undefined, result); 111 | 112 | var result = TestSession.get('barfoo'); 113 | test.equal(undefined, result); 114 | }); 115 | 116 | 117 | Tinytest.add("gets undefined", function(test) { 118 | var TestSession = new PersistentSession(Random.id()); 119 | 120 | var result = TestSession.get('foobar'); 121 | test.equal(void 0, result); 122 | }); 123 | 124 | Tinytest.add("sets & gets", function(test) { 125 | var TestSession = new PersistentSession(Random.id()); 126 | 127 | // set never returns anything although it probably should... 128 | var result = TestSession.set('something', 'amazing'); 129 | test.equal(void 0, result); 130 | // did it set? 131 | result = TestSession.get('something'); 132 | test.equal('amazing', result); 133 | }); 134 | 135 | Tinytest.add("sets defaults", function(test) { 136 | var TestSession = new PersistentSession(Random.id()); 137 | 138 | // set never returns anything although it probably should... 139 | var result = TestSession.setDefault('something', 'amazing'); 140 | test.equal(void 0, result); 141 | 142 | // did it set? 143 | result = TestSession.get('something'); 144 | test.equal('amazing', result); 145 | }); 146 | 147 | Tinytest.add("sets defaults with an object", function(test) { 148 | var TestSession = new PersistentSession(Random.id()); 149 | 150 | // set never returns anything although it probably should... 151 | var result = TestSession.setDefault({ something: 'amazing', foobar: 'awesome'}); 152 | test.equal(void 0, result); 153 | 154 | // did it set? 155 | result = TestSession.get('something'); 156 | test.equal('amazing', result); 157 | 158 | result = TestSession.get('foobar'); 159 | test.equal('awesome', result); 160 | }); 161 | 162 | Tinytest.add("sets defaults but doesn't change if set", function(test) { 163 | var TestSession = new PersistentSession(Random.id()); 164 | 165 | // set never returns anything although it probably should... 166 | TestSession.set('something', 'amazing'); 167 | 168 | var result = TestSession.setDefault('something', 'awesome'); 169 | test.equal(void 0, result); 170 | 171 | // did it set? 172 | result = TestSession.get('something'); 173 | test.equal('amazing', result); 174 | }); 175 | 176 | Tinytest.add("multiple sessions don't effect each other (never cross the streams)", function(test) { 177 | var TestSessionFoo = new PersistentSession(Random.id()); 178 | var TestSessionBar = new PersistentSession(Random.id()); 179 | 180 | TestSessionFoo.set('something', 'amazing'); 181 | var result = TestSessionFoo.get('something'); 182 | test.equal('amazing', result); 183 | 184 | TestSessionBar.set('something', 'awesome'); 185 | var result = TestSessionBar.get('something'); 186 | test.equal('awesome', result); 187 | 188 | var result = TestSessionFoo.get('something'); 189 | test.equal('amazing', result); 190 | }); 191 | 192 | 193 | Tinytest.add("store gets persisted value", function(test) { 194 | var dictName = Random.id(); 195 | amplify.store(dictName + 'foo', "awesome"); 196 | 197 | var TestSession = new PersistentSession(dictName); 198 | var result = TestSession.get('foo'); 199 | test.equal('awesome', result); 200 | }); 201 | 202 | 203 | Tinytest.add("setDefaultPersistent sets with an object", function(test) { 204 | var TestSession = new PersistentSession(Random.id()); 205 | 206 | TestSession.setDefaultPersistent({ 207 | 'id': 'foobarid', 208 | 'room_id': 'foobarroomid' 209 | }); 210 | 211 | TestSession._dict.clear(); 212 | 213 | var result = TestSession.get('id'); 214 | test.equal('foobarid', result); 215 | 216 | var result = TestSession.get('room_id'); 217 | test.equal('foobarroomid', result); 218 | }); 219 | 220 | Tinytest.add("setDefaultPersistent only sets unset keys (gh #32)", function(test) { 221 | var TestSession = new PersistentSession(Random.id()); 222 | 223 | TestSession.set('room_id', 'awesome'); 224 | var result = TestSession.get('room_id'); 225 | test.equal('awesome', result); 226 | 227 | TestSession.setDefaultPersistent({ 228 | 'id': 'foobarid', 229 | 'room_id': 'foobarroomid' 230 | }); 231 | 232 | var result = TestSession.get('id'); 233 | test.equal('foobarid', result); 234 | 235 | var result = TestSession.get('room_id'); 236 | test.equal('awesome', result); 237 | 238 | }); 239 | 240 | 241 | Tinytest.add("setDefaultPersistent should not override an existing persisted value", function(test) { 242 | var dictName = Random.id(); 243 | amplify.store(dictName + 'foo', "awesome"); 244 | 245 | var TestSession = new PersistentSession(dictName); 246 | 247 | var result = TestSession.get('foo'); 248 | test.equal('awesome', result); 249 | 250 | TestSession.setDefaultPersistent('foo', 'foobarid'); 251 | 252 | var result = TestSession.get('foo'); 253 | test.equal('awesome', result); 254 | }); 255 | 256 | 257 | Tinytest.add("equals works", function(test) { 258 | var dictName = Random.id(); 259 | amplify.store(dictName + 'foo', "awesome"); 260 | 261 | var TestSession = new PersistentSession(dictName); 262 | 263 | var result = TestSession.get('foo'); 264 | test.equal('awesome', result); 265 | 266 | var result = TestSession.equals('foo', 'awesome'); 267 | test.equal(true, result); 268 | }); 269 | 270 | Tinytest.add("all works", function(test) { 271 | var dictName = Random.id(); 272 | // default the session with some data before creating it 273 | amplify.store(dictName + 'foo', "awesome"); 274 | // since we set foo, we'll also need it's key to be set to `set` is called 275 | // and it ends up in the `dict.keys` 276 | amplify.store('__PSKEYS__' + dictName, ['foo']); 277 | amplify.store('__PSDATAVERSION__' + dictName, 4); 278 | 279 | var TestSession = new PersistentSession(dictName); 280 | 281 | var result = TestSession.get('foo'); 282 | test.equal('awesome', result); 283 | 284 | TestSession.set('bar', 'thing'); 285 | var result = TestSession.get('bar'); 286 | test.equal('thing', result); 287 | 288 | TestSession.setDefaultPersistent('foobar', 'stuff'); 289 | TestSession.setAuth('foobarfoo', 'fact'); 290 | TestSession.setPersistent('barfoobar', 'entity'); 291 | 292 | var result = TestSession.all(); 293 | 294 | test.equal({ 295 | "foo" : "awesome", 296 | "bar" : "thing", 297 | "foobar" : "stuff", 298 | "foobarfoo" : "fact", 299 | "barfoobar" : "entity" 300 | }, result); 301 | }); 302 | 303 | Tinytest.add("updates from 3.x to 4.x", function(test) { 304 | var dictName = Random.id(); 305 | localStorage.clear(); 306 | // Set up 3.x-format keys/values 307 | localStorage['__amplify__' + dictName + 'foo'] = '{"data":"[]","expires":null}'; 308 | localStorage['__amplify__' + dictName + 'bar'] = '{"data":"\\"noodol\\"","expires":null}'; 309 | localStorage['__amplify__' + dictName + 'obj'] = '{"data":"{\\"obj\\":\\"val\\"}","expires":null}'; 310 | // 4.x-format keys/values 311 | localStorage['__amplify__' + dictName + 'foo4'] = '{"data":[],"expires":null}'; 312 | localStorage['__amplify__' + dictName + 'bar4'] = '{"data":"noodol","expires":null}'; 313 | localStorage['__amplify__' + dictName + 'obj4'] = '{"data":{"obj":"val"},"expires":null}'; 314 | amplify.store('__PSKEYS__' + dictName, ['foo', 'bar', 'obj', 'foo4', 'bar4', 'obj4']); 315 | amplify.store('__PSDATAVERSION__' + dictName, 1); 316 | 317 | var TestSession = new PersistentSession(dictName); 318 | test.equal(amplify.store('__PSDATAVERSION__' + dictName), 4); 319 | test.equal(TestSession.get('foo'), []); 320 | test.equal(TestSession.get('bar'), "noodol"); 321 | test.equal(TestSession.get('obj'), { obj: "val" }); 322 | test.equal(TestSession.get('foo4'), []); 323 | test.equal(TestSession.get('bar4'), "noodol"); 324 | test.equal(TestSession.get('obj4'), { obj: "val" }); 325 | }); 326 | -------------------------------------------------------------------------------- /versions.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": [ 3 | [ 4 | "amplify", 5 | "1.0.0" 6 | ], 7 | [ 8 | "base64", 9 | "1.0.0" 10 | ], 11 | [ 12 | "ejson", 13 | "1.0.3" 14 | ], 15 | [ 16 | "jquery", 17 | "1.0.0" 18 | ], 19 | [ 20 | "json", 21 | "1.0.0" 22 | ], 23 | [ 24 | "meteor", 25 | "1.1.1" 26 | ], 27 | [ 28 | "reactive-dict", 29 | "1.0.3" 30 | ], 31 | [ 32 | "session", 33 | "1.0.2" 34 | ], 35 | [ 36 | "tracker", 37 | "1.0.2" 38 | ], 39 | [ 40 | "underscore", 41 | "1.0.0" 42 | ] 43 | ], 44 | "pluginDependencies": [], 45 | "toolVersion": "meteor-tool@1.0.33", 46 | "format": "1.0" 47 | } --------------------------------------------------------------------------------