@@ -21,3 +21,2 @@ !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.Fux=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){ | ||
var ACTION_UID = Symbol("the actions uid name"); | ||
var BOOTSTRAP_FLAG = VariableSymbol("have you bootstrapped yet?"); | ||
var EE = Symbol("event emitter instance"); | ||
@@ -171,2 +170,22 @@ var LISTENERS = Symbol("stores action listeners storage"); | ||
var bootstrap = function (instance, data) { | ||
var obj = JSON.parse(data); | ||
Object.keys(obj).forEach(function (key) { | ||
Object.assign(instance[STORES_STORE][key][STATE_CONTAINER], obj[key]); | ||
if (instance[STORES_STORE][key][STORE_BOOTSTRAP]) { | ||
instance[STORES_STORE][key][STORE_BOOTSTRAP](); | ||
} | ||
}); | ||
}; | ||
var snapshot = function (instance) { | ||
return JSON.stringify(Object.keys(instance[STORES_STORE]).reduce(function (obj, key) { | ||
if (instance[STORES_STORE][key][STORE_SNAPSHOT]) { | ||
instance[STORES_STORE][key][STORE_SNAPSHOT](); | ||
} | ||
obj[key] = instance[STORES_STORE][key].getState(); | ||
return obj; | ||
}, {})); | ||
}; | ||
var Alt = (function () { | ||
@@ -176,3 +195,2 @@ var Alt = function Alt() { | ||
this[STORES_STORE] = {}; | ||
this[BOOTSTRAP_FLAG] = false; | ||
}; | ||
@@ -201,2 +219,6 @@ | ||
if (this[STORES_STORE][key]) { | ||
throw new ReferenceError("A store named " + key + " already exists, double check your store names or pass in\nyour own custom identifier for each store"); | ||
} | ||
return this[STORES_STORE][key] = Object.assign(new AltStore(this.dispatcher, store), getInternalMethods(StoreModel, builtIns)); | ||
@@ -247,10 +269,3 @@ }; | ||
Alt.prototype.takeSnapshot = function () { | ||
var _this5 = this; | ||
var state = JSON.stringify(Object.keys(this[STORES_STORE]).reduce(function (obj, key) { | ||
if (_this5[STORES_STORE][key][STORE_SNAPSHOT]) { | ||
_this5[STORES_STORE][key][STORE_SNAPSHOT](); | ||
} | ||
obj[key] = _this5[STORES_STORE][key].getState(); | ||
return obj; | ||
}, {})); | ||
var state = snapshot(this); | ||
this._lastSnapshot = state; | ||
@@ -261,19 +276,20 @@ return state; | ||
Alt.prototype.rollback = function () { | ||
this[BOOTSTRAP_FLAG] = false; | ||
this.bootstrap(this._lastSnapshot); | ||
bootstrap(this, this._lastSnapshot); | ||
}; | ||
Alt.prototype.bootstrap = function (data) { | ||
var _this6 = this; | ||
if (this[BOOTSTRAP_FLAG]) { | ||
throw new ReferenceError("Stores have already been bootstrapped"); | ||
var _this5 = this; | ||
var state = snapshot(this); | ||
if (typeof window === "undefined") { | ||
bootstrap(this, data); | ||
setTimeout(function () { | ||
return bootstrap(_this5, state); | ||
}); | ||
} else { | ||
bootstrap(this, data); | ||
this.bootstrap = function () { | ||
throw new ReferenceError("Stores have already been bootstrapped"); | ||
}; | ||
} | ||
var obj = JSON.parse(data); | ||
Object.keys(obj).forEach(function (key) { | ||
Object.assign(_this6[STORES_STORE][key][STATE_CONTAINER], obj[key]); | ||
if (_this6[STORES_STORE][key][STORE_BOOTSTRAP]) { | ||
_this6[STORES_STORE][key][STORE_BOOTSTRAP](); | ||
} | ||
}); | ||
this[BOOTSTRAP_FLAG] = true; | ||
}; | ||
@@ -280,0 +296,0 @@ |
{ | ||
"name": "alt", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "A flux implementation", | ||
@@ -5,0 +5,0 @@ "main": "dist/alt.js", |
@@ -22,8 +22,8 @@ # alt | ||
```txt | ||
╔═══════════════╗ ╔══════════════╗ ╔════════════╗ | ||
║ Actions ║ ═══════> ║ Stores ║ ═══════> ║ View ║ | ||
╚═══════════════╝ ╚══════════════╝ ╚════════════╝ | ||
▲ ║ | ||
║ ║ | ||
╚════════════════════════════════════════════════════════╝ | ||
╔═══════════════╗ ╔══════════════╗ ╔════════════╗ | ||
║ Actions ║ ═══════> ║ Stores ║ ═══════> ║ View ║ | ||
╚═══════════════╝ ╚══════════════╝ ╚════════════╝ | ||
▲ ║ | ||
║ ║ | ||
╚════════════════════════════════════════════════════════╝ | ||
``` | ||
@@ -213,3 +213,3 @@ | ||
`alt.createStore :: Class -> Store` | ||
`alt.createStore :: Class, String -> Store` | ||
@@ -510,2 +510,8 @@ ```js | ||
If you're bootstrapping then it is recommended you pass in a unique Identifier, name of the class is good enough, to createStore so that it can be referenced later for bootstrapping. | ||
```js | ||
alt.createStore(LocationStore, 'LocationStore') | ||
``` | ||
### Rollback | ||
@@ -535,6 +541,25 @@ | ||
Each store has a reference to the dispatcher as well | ||
```js | ||
alt.createStore(class MyStore { | ||
constructor() { | ||
this.dispatcher.register(console.log) | ||
} | ||
}) | ||
``` | ||
## Examples | ||
* [todomvc](https://github.com/goatslacker/alt/blob/master/examples/todomvc) | ||
* [chat](https://github.com/goatslacker/alt/tree/master/examples/chat) | ||
## Converting a flux application to alt | ||
1. [Importing the chat project](https://github.com/goatslacker/alt/commit/1a54de1064fe5bd252979380e47b0409d1306773). | ||
2. [Adding alt and removing boilerplate](https://github.com/goatslacker/alt/commit/75ffdb53420dc32bdc2d99b5cf534cd0949331d8). | ||
3. [Converting some actions](https://github.com/goatslacker/alt/commit/6f8cf22ba6b36c6ae91a794fad75473c9436b683) and [the last action](https://github.com/goatslacker/alt/commit/58ea1418ecd2af25b578cd0f4b77c3d4d8631518). | ||
4. Converting the stores [MessageStore](https://github.com/goatslacker/alt/commit/f4c7bb4bb9027b53c380f98ed99a2e1b6ba5fa0b), [ThreadStore](https://github.com/goatslacker/alt/commit/bce2aadbb52981f934b4281b3a6244d4f2c4a7a9), and [UnreadThreadStore](https://github.com/goatslacker/alt/commit/0129baa5bd505ef26228e30cfa15a6ac4503a22d). | ||
5. [Finishing touches](https://github.com/goatslacker/alt/commit/e05a4e02f3f13831361136a21cd757416b69b4d8). | ||
## Differences Example | ||
@@ -550,2 +575,8 @@ | ||
```js | ||
var keyMirror = require('keymirror') | ||
var actionConstants = keyMirror({ | ||
HANDLE_ACTION: null | ||
}) | ||
var action = { | ||
@@ -552,0 +583,0 @@ foo() { |
39
test.js
@@ -47,2 +47,4 @@ var Alt = require('./src/coverage-alt') | ||
this.dontEmitEventCalled = false | ||
this._dispatcher = this.dispatcher | ||
} | ||
@@ -156,2 +158,4 @@ | ||
assert.equal(typeof myStore.getState()._dispatcher, 'object', 'the dispatcher is exposed internally') | ||
assert.equal(lifecycleStore.getState().bootstrapped, false, 'bootstrap has not been called yet') | ||
@@ -236,4 +240,17 @@ assert.equal(lifecycleStore.getState().snapshotted, false, 'takeSnapshot has not been called yet') | ||
// Store on server side bootstraps and then returns to previous state | ||
alt.bootstrap('{"MyStore":{"name":"elk"}}') | ||
assert.equal(myStore.getState().name, 'elk', 'temporary store state') | ||
setTimeout(() => { | ||
assert.equal(myStore.getState().name, 'badger', 'the state is reset to last state though') | ||
}) | ||
try { | ||
// Creating a global window object so we are in browser mode. | ||
global.window = {} | ||
// Attempting to bootstrap more than once | ||
alt.bootstrap('{}') | ||
alt.bootstrap('{"MyStore":{"name":"elk"}}') | ||
assert.equal(true, false, 'I was able bootstrap more than once which is bad') | ||
@@ -245,3 +262,3 @@ } catch (e) { | ||
assert.equal(myStore.getState().name, 'badger', 'store state still the same') | ||
// assert.equal(myStore.getState().name, 'badger', 'store state still the same') | ||
@@ -404,2 +421,22 @@ myActions.updateTwo(4, 2) | ||
assert.equal(myStore.getState().dontEmitEventCalled, true, 'dont emit event was called successfully and event was not emitted') | ||
try { | ||
var MyStore = (function () { | ||
return function MyStore() { } | ||
}()) | ||
alt.createStore(MyStore) | ||
assert.equal(true, false, 'I was able to create a store with the same name') | ||
} catch (e) { | ||
assert.equal(e instanceof ReferenceError, true, 'error was thrown for store with same name') | ||
} | ||
try { | ||
var mystore = (function () { | ||
return function mystore() { } | ||
}()) | ||
alt.createStore(mystore, 'MyStore') | ||
assert.equal(true, false, 'I was able to create a store with the same name by passing in an identifier') | ||
} catch (e) { | ||
assert.equal(e instanceof ReferenceError, true, 'error was thrown for store with same name') | ||
} | ||
}() |
67007
5.65%8
14.29%1199
4.81%631
5.17%