Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var runAcyncOperationWithRunActionExample = () => {
var myModel = {
foo:0,
backgroundOperations: 0
};
var router = new esp.Router();
router.addModel('myModelId', myModel);
router.getEventObservable('myModelId', 'getAsyncDataEvent').subscribe((e, c, m) => {
console.log('About to do async work');
m.backgroundOperations++;
setTimeout(() => {
router.runAction('myModelId', m2 => { // you could close over m here if you prefer
m2.backgroundOperations--;
console.log('Async work received. Updating model');
m2.foo = 1;
});
}, 2000);
});
router.publishEvent('myModelId', 'getAsyncDataEvent', { request: "someRequest" });
router.getModelObservable('myModelId').subscribe(m => {
console.log('Update, background operation count is: %s. foo is %s', m.backgroundOperations, m.foo);
});
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// notice_end
import esp from 'esp-js';
// note there are several concerns here that would exist in different areas of your architecture
// then are all together to demo the concepts.
// bootstrapping code
var router = new esp.Router();
router.registerModel(
"modelId",
{
staticDataInitialised: false,
price: 0,
staticData: { }
}
);
// price event processor code
router
.getEventObservable('modelId', 'priceChanged')
.do((model, event, eventContext) => console.log("price tick received"))
.where((model, event, eventContext) => model.staticDataInitialised)
.observe((model, event, eventContext)=> {
console.log("Price tick received and static data loaded, applying margin");
}
_listenForStaticDataReceivedEvent() {
// note you could wire up more advanced disposal of this stream (i.e. write
// a .takeUntilInclusive() extension method, you could also leave it
// open if you were to later expect events matching its eventType
this.addDisposable(this._router
.getEventObservable('modelId', 'userStaticReceivedEvent')
.subscribe((event, eventContext, model) => {
console.log("Adding static data [" + event + "] to model");
model.staticData.push(event);
})
);
}
}
var router = new esp.Router();
router.addModel("modelId", { staticData:[]});
var staticDataEventProcessor = new StaticDataEventProcessor(router);
staticDataEventProcessor.initialise();
console.log("Sending initialiseEvent");
router.publishEvent('modelId', 'initialiseEvent', {});
};
// esp dev tools is in beta ATM, bit heavy on the performance side of things.
// However feel free to uncomment these 2 lines and check it out.
// ctrl+alt+d brings up the tool window
//import espDevTools from 'esp-js-devtools';
//espDevTools.registerDevTools();
var _log:logger.Logger = logger.create('UnhandledModelError');
// The application uses a single esp router.
// Views can take the router instance it via import/require, however for model entities it's best not to rely on sourcing 'instance' style objects from import/require.
// This is because it makes the code a little less flexable, less portable and a little more magic.
// I tend to think of anything 'imported' as a static object, hard to test these.
// Typically you don't test the jsx/view and you don't really have any other options to get them the router.
// This is because they are created on a very different code path (i.e. via React `render`) and it's a bit over kill to pass the router via props (using the React context could be an alternative).
let router = new Router();
router.addOnErrorHandler(err => {
_log.error('Unhandled error in model', err);
});
export default router;
constructor(props) {
super(props);
// create an app wide router
this.router = new Router();
// create a model responsible for displaying app wide modal windows
let modal = new Modal(this.router);
modal.observeEvents();
this.modalModelId = modal.modelId;
// create the main model
let workspace = new Workspace(this.router, modal);
this.workspaceModelId = workspace.modelId;
workspace.observeEvents();
}
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// notice_end
import esp from 'esp-js';
import ModelBootstrapper from './model/ModelBootstrapper';
import MainController from './controllers/MainController';
var router = new esp.Router();
var mainController = new MainController(router);
var modelBootstrapper = new ModelBootstrapper(router);
mainController.start();
modelBootstrapper.start();
var runModelRouter = () => {
var myModel = {
foo:0
};
var router = new esp.Router();
router.addModel('myModel', myModel);
var modelRouter = router.createModelRouter('myModel');
modelRouter.getEventObservable('fooEvent').subscribe((e, c, m) => {
m.foo = e.theFoo;
});
modelRouter.getModelObservable().subscribe(m => {
console.log('Update, foo is: %s', m.foo);
});
modelRouter.publishEvent('fooEvent', { theFoo: 1});
modelRouter.publishEvent('fooEvent', { theFoo: 2});
};
var runModelObserveExample = () => {
var router = new esp.Router();
router.addModel("modelId", { foo: 1 });
router
.getEventObservable('modelId', 'fooChanged')
.subscribe((event, eventContext, model)=> {
model.foo = event.newFoo;
});
router
.getModelObservable('modelId')
.subscribe(model => {
console.log("Foo is " + model.foo);
});
router.publishEvent('modelId', 'fooChanged', { newFoo: 2 });
};
var runModelRouter = () => {
var myModel = {
foo:0
};
var router = new esp.Router();
router.addModel('myModel', myModel);
var modelRouter = router.createModelRouter('myModel');
modelRouter.getEventObservable('fooEvent').subscribe((e, c, m) => {
m.foo = e.theFoo;
});
modelRouter.getModelObservable().subscribe(m => {
console.log('Update, foo is: %s', m.foo);
});
modelRouter.publishEvent('fooEvent', { theFoo: 1});
modelRouter.publishEvent('fooEvent', { theFoo: 2});
};
var runModelObserveExample = () => {
var router = new esp.Router();
router.addModel("modelId", { foo: 1 });
router
.getEventObservable('modelId', 'fooChanged')
.subscribe((event, eventContext, model)=> {
model.foo = event.newFoo;
});
router
.getModelObservable('modelId')
.subscribe(model => {
console.log("Foo is " + model.foo);
});
router.publishEvent('modelId', 'fooChanged', { newFoo: 2 });
};