|
| 1 | +<!doctype html> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <meta charset="utf8" /> |
| 5 | + <title>Simple localForage-observable example</title> |
| 6 | + </head> |
| 7 | + <body> |
| 8 | + <script src="../node_modules/localforage/dist/localforage.js"></script> |
| 9 | + <script src="../node_modules/zen-observable/zen-observable.js"></script> |
| 10 | + <script src="../dist/localforage-observable.js"></script> |
| 11 | + <script> |
| 12 | + function runTests() { |
| 13 | + console.clear(); |
| 14 | + |
| 15 | + localforage = localforage.createInstance(); |
| 16 | + localforage.setDriver([ |
| 17 | + localforage.INDEXEDDB, |
| 18 | + localforage.WEBSQL, |
| 19 | + localforage.LOCALSTORAGE |
| 20 | + ]) |
| 21 | + .then(() => localforage.ready()) |
| 22 | + .then(() => localforage.clear()) |
| 23 | + .then(function() { |
| 24 | + // localforageObservable.extendPrototype(localforage); |
| 25 | + |
| 26 | + var observable = localforage.newObservable(); |
| 27 | + |
| 28 | + var observableLogs = []; |
| 29 | + |
| 30 | + var subscription = observable.subscribe({ |
| 31 | + next: function(args) { |
| 32 | + observableLogs.push(args.methodName + '(\'' + args.key + '\', ' + '\'' + args.newValue + '\')'); |
| 33 | + console.log('I observe everything', args); |
| 34 | + }, |
| 35 | + error: function(err) { |
| 36 | + console.log('Found an error!', err); |
| 37 | + }, |
| 38 | + complete: function() { |
| 39 | + console.log('Observable destroyed!'); |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + localforage.setItem('test1', 'value1').then(function() { |
| 44 | + console.log('setItem(\'test1\', \'value1\')'); |
| 45 | + return localforage.setItem('test2', 'value2'); |
| 46 | + }).then(function() { |
| 47 | + console.log('setItem(\'test2\', \'value2\')'); |
| 48 | + return localforage.setItem('test2', 'value2b'); |
| 49 | + }).then(function() { |
| 50 | + console.log('setItem(\'test2\', \'value2b\')'); |
| 51 | + return localforage.setItem('test2', 'value2b'); |
| 52 | + }).then(function() { |
| 53 | + console.log('setItem(\'test2\', \'value2b\')'); |
| 54 | + return localforage.setItem('test3', 'value3'); |
| 55 | + }).then(function() { |
| 56 | + console.log('setItem(\'test3\', \'value3\')'); |
| 57 | + subscription.unsubscribe(); |
| 58 | + return localforage.setItem('notObservedKey', 'notObservedValue'); |
| 59 | + }).then(function() { |
| 60 | + console.log('setItem(\'notObservedKey\', \'notObservedValue\')'); |
| 61 | + return localforage.clear(); |
| 62 | + }).then(function() { |
| 63 | + checkExpectations(); |
| 64 | + }); |
| 65 | + |
| 66 | + var expectedLogs = [ |
| 67 | + 'setItem(\'test1\', \'value1\')', |
| 68 | + 'setItem(\'test2\', \'value2\')', |
| 69 | + 'setItem(\'test2\', \'value2b\')', |
| 70 | + 'setItem(\'test3\', \'value3\')' |
| 71 | + ]; |
| 72 | + |
| 73 | + function checkExpectations() { |
| 74 | + if (expectedLogs.length !== observableLogs.length) { |
| 75 | + console.error('Tests: Unexpected log length'); |
| 76 | + return; |
| 77 | + } |
| 78 | + for (var i = 0; i < expectedLogs.length; i++) { |
| 79 | + if (expectedLogs[i] !== observableLogs[i]) { |
| 80 | + console.error('Tests: Unexpected logs'); |
| 81 | + return; |
| 82 | + } |
| 83 | + } |
| 84 | + console.info('Tests: OK'); |
| 85 | + return true; |
| 86 | + } |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + runTests(); |
| 91 | + </script> |
| 92 | + |
| 93 | + <p> |
| 94 | + Check your console log. |
| 95 | + </p> |
| 96 | + </body> |
| 97 | +</html> |
0 commit comments