Coder Social home page Coder Social logo

angular-live-set's Introduction

Angular Live Set

Display changes as they are made to a collection of objects.

View the examples

Requirements

  • AngularJS
  • ChangeStream server (eg. LoopBack)

Use

Install Angular Live Set with Bower:

bower install angular-live-set

This will copy the angular-live-set files into a bower_components folder, along with its dependencies. Load the script files in your application:

<script type="text/javascript" src="bower_components/angular/angular.js"></script>
<script type="text/javascript" src="bower_components/angular-live-set/angular-live-set.js"></script>

Add the module to your dependencies:

angular.module('myApp', ['ls.LiveSet', 'ls.ChangeStream'])

Concurrency

Change streams are applied in order. This means the set can only be modified synchronously. The last change wins.

API

LiveSet(data, changes, options)

A LiveSet applies a ChangeStream, or a continuous stream of changes, to an array of objects. The set itself is read only and can only be modified by sending a change through the change stream.

Note: Updates to the LiveSet will trigger a $rootScope.$apply().

data

An Array of initial data.

changes

A ChangeStream to be applied continuously to the set.

options

Customize the LiveSet.

options.id

A String defining the id property for objects in the set. Default: 'id'.

Example

// objects in the set must include some identifying property (customizable)
var data = [{id: 1, val: 'foo'}, {id: 2, val: 'bar'}];
var src = new EventSource('/changes');
var changes = createChangeStream(src);
var set = new LiveSet(data, changes);

// bind to the set from a template
// like you would an array
// note: the data in the array will be updated
// changes come in (from the ChangeStream)
$scope.values = set.toLiveArray();

ChangeStream(eventSource)

function MyController($scope, createChangeStream) {
  // create a change stream
  var changes = createChangeStream();
}

eventSource

An EventSource emitting the following events:

  • data - contains a Change object
  • error - contains an Error object
  • end - the stream has ended on the server

A continuous stream of Change objects. Each Change represents a modification to an object. Changes are applied to a set in the order they flow in the stream.

Change

A chunk in a ChangeStream.

change.target

This change applies to an object with this identifier.

change.type

  • create
  • update
  • remove

change.data

  • create - the entire object
  • update - the entire object
  • remove - null

change.optimistic

true when a change is likely to be made, but has not completed.

Only supported for changes of type update and remove.

A change has not been made to an object, but it has a very high likelyhood of being made. For example, a user modifies data locally and sends it to a server. This change has not actually been made to the definitave state on the server. Unless something unexpected happens, the change will be made and sent through a ChangeStream.

In cases like this, it is appropriate to send an "optimistic" change that will be immediately applied. These changes should be reverted after a specified period unless another (non-optmisitic) change with the same target is written to the ChangeStream.

Error

error.message

An error message.

error.status

An HTTP-like status code.

angular-live-set's People

Contributors

0candy avatar bajtos avatar crandmck avatar davidcheung avatar dhmlau avatar nabdelgadir avatar ritch avatar rmg avatar sam-github avatar siddhipai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

angular-live-set's Issues

Does not work with non-integer ids

It seems like this plugin does not work whenever the ID of an object is not an integer. Looked into the code and it seems like it's trying to create an numbered 1d array (not an object). All fine but I'm using uuid4s to identify my objects.

Whenever an object is updated the values all disappear and the whole object is replaced by just the updated key.

Error when testing change stream

Web server listening at: http://0.0.0.0:3000/
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11)
at ServerResponse.res.setHeader (/Users/stellanhaglund/Documents/oopy/node_modules/loopback-explorer/node_modules/express/node_modules/connect/lib/patch.js:134:22)
at ServerResponse.header (node_modules/loopback/node_modules/express/lib/response.js:718:10)
at ServerResponse.send (node_modules/loopback/node_modules/express/lib/response.js:163:12)
at ServerResponse.json (node_modules/loopback/node_modules/express/lib/response.js:249:15)
at ServerResponse.send (node_modules/loopback/node_modules/express/lib/response.js:151:21)
at defaultHandler (node_modules/loopback/node_modules/strong-remoting/lib/rest-adapter.js:380:11)
at restErrorHandler (node_modules/loopback/node_modules/strong-remoting/lib/rest-adapter.js:347:14)
at Layer.handle_error (node_modules/loopback/node_modules/express/lib/router/layer.js:71:5)
at trim_prefix (node_modules/loopback/node_modules/express/lib/router/index.js:310:13)

PATCH / updates to attributes not caught in LiveArrays

I have a var set = new LiveSet(data, changes) instantiated, where changes is from createChangeStream for a loopback change-stream.

I'm manually patching through the REST API, and I get the updated object in my change-stream url, as well as a proper log from changes.on('data', function(event){ console.log(data); })

But my bound $scope.data = set.toLiveArray();'s data does not reflect on my scope. The $scope.data values are still the old ones.

Is it working on Ionic? Mine is not

Is this live-set module working on ionic? because mine is not working.
and i don't get any error.

here is my code:

on Main App

angular.module('starter', ['ionic', 'starter.controllers', 'lbServices', 'ls.LiveSet', 'ls.ChangeStream'])
//etc............

on Controller

angular.module('starter.controllers', [])
.controller('ProductsCtrl', function($scope, $ionicModal, Product, createChangeStream, LiveSet) {
            var src = new EventSource('http://localhost:3000/api/products/change-stream?_format=event-source');
            var changes = createChangeStream(src);
            var set;

            Product.find().$promise.then(function(results) {
                set = new LiveSet(results, changes);
                $scope.products = set.toLiveArray();
            });

            changes.on('data', function(update) {
                // add the new data to the chart
                console.log('something happened')
                console.log(update)
            });
//etc.................

My data is shown like expected, but the realtime thing is not working.
Please help

Optimistic create

I'd like to start a conversation about implementing optimistic create. The feature is very important for building a responsive application. The issue with implementing this feature is that its without a target there is no way of knowing that data that a create event in the EventStream corresponds with an item that was added optimistically.

A couple of thoughts:

  • Perhaps the strongloop change stream implementation should not echo the changes back to the client that made them. The client already gets the server response via the HTTP request and can update the optimistically created resource on the HTTP response.
  • If the client were able to reserve an ID then we could associate created items from EventStream with items optimistically created on the client. It seems like strongloop's synchronization efforts do something similar where they use a guid as the primary key.

Docs contain the wrong file name

Description/Steps to reproduce

Install angular-live-set as mentioned in docs.

Link to reproduction sandbox

Expected result

The readme doc says to add following script file in application:

<script type="text/javascript" src="bower_components/angular-live-set/angular-live-set.js"></script>

But this file is not available.
The file path should be changed to

<script type="text/javascript" src="bower_components/angular-live-set/dist/live-set.js"></script>

Additional information

darwin x64 6.9.5

├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] -> /Users/kamal/dev/loopback-reset-password-mixin

Internet Explorer Comptability Solved

I solved Internet Explorer compatibility early on when I was using this and thought to document it here.

There is a polyfill that you can get on bower that solves it. From my bower.json

"event-source-polyfill": "^0.0.7"

The magic of LiveSet stop whenever I call the method twice !!!

Hi,

I tried to use the google groups to solve my issue but I didn't have any helpful answer.,, it is simply the LiveSet not updated even that the events is received... this odd behaviour start when I call the following code twice

set=new LiveSet(result,changes)
$scope.list=set.toLiveArray()

the $scope.list in the second call is updated with the new result, however, the $scope.list not notified whenever new event occur...

https://groups.google.com/forum/#!searchin/loopbackjs/liveset/loopbackjs/NrtbSZwWiQo/RYcod9N9AwAJ

I really appreciate any kind of help,

Thanks in advance

CI failure

Below are the failure. See #34 for the error messages.

[cis-jenkins] PR Builder Build finished.
[cis-jenkins] x64 && linux && nvm,0.10 Failed! (af6cebf)
[cis-jenkins] x64 && linux && nvm,0.12 Failed! (af6cebf)
[cis-jenkins] x64 && linux && nvm,8 Failed! (af6cebf)
angular-live-set Failed! (af6cebf)
angular-live-set/node=0.10,os=amazon Failed! (af6cebf)
angular-live-set/node=0.10,os=macos Failed! (af6cebf)
angular-live-set/node=0.10,os=ubuntu Failed! (af6cebf)
angular-live-set/node=0.10,os=windows Failed! (af6cebf)
angular-live-set/node=0.12,os=amazon Failed! (af6cebf)
angular-live-set/node=0.12,os=macos Failed! (af6cebf)
angular-live-set/node=0.12,os=ubuntu Failed! (af6cebf)
angular-live-set/node=0.12,os=windows Failed! (af6cebf)
angular-live-set/node=4.x,os=windows Failed! (af6cebf)
angular-live-set/node=6.x,os=macos Failed! (af6cebf)
angular-live-set/node=6.x,os=ubuntu Failed! (af6cebf)
angular-live-set/node=6.x,os=windows Failed! (af6cebf)

'stream is not defined' when loaded as npm module

I'm wondering if this has to do with the AMD modules in the dist?

``new stream.PassThrough` is what throws this on line 19, though I can see that Stream.PassThrough is defined later on.

I'm running Angular with Babel and ES6 in an electron.js runtime, in the renderer context, so bascially a chromium browser. Maybe I should try loading directly from the modules/ directory?

What's the best practice for monitoring a filtered stream?

I need to stream changes to a specified user, not to all users.

In my below code I can successfully initially query messages for a specific user, but any updates to the stream is not filtered. So it gives me updates to any change to the Message model regardless of the filter.

  1. Is there a way to filter a subset of a stream? Most specifically the authenticated user?
  2. I have a belongsTo relation between my Message model and my user model (which has User as the base model). Can I monitor a stream specific to user.Message, i.e. to just the relation?

Here's my current code:

   var url = '/api/messages/change-stream' +
      '?_format=event-source' +
      '&access_token=' + AuthService.getAccessToken();

    var src = new EventSource(url);
    var changes = createChangeStream(src);
    var set;

    Message.find({
      filter: {
        where: {
          userId: '55f73db4b3a03aa495c0aef2'
        },
        order: 'created DESC',
        limit: 5
      }
    }).$promise.then(function(results) {
      set = new LiveSet(results, changes);
      vm.messages = set.toLiveArray();
    });

I can see here that you can set a where clause in creating a change-stream: https://apidocs.strongloop.com/loopback/#persistedmodel-createchangestream, but I'm struggling on how to do this with the LiveSet approach. I suspect somehow from the Angular client I can create a change stream, pass in an option 'where clause'.

I'm also struggling to understand how the lbServices createChangeStream factors into all of this, like Message.createChangeStream(), versus the newly released factory createChangeStream() that's in angular-live-set.

_format=event-source ?

Hi there,
i try to understand the backend side. I didnt find any doc or code about _format=event-source ?
Where is the implementation in loopback?

Thanks

Limit and Skip, how to use LiveSet with paging?

Hi All,

I can't understand how to use LiveSet with limit and skip?

Actually, exactly like Facebook, notifications dropdown .. which is realtime notifications + paging

Any ideas?

Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.